Skip to content

Commit

Permalink
Merge pull request #429 from bazsi/filterx-scope-avoid-moving-variabl…
Browse files Browse the repository at this point in the history
…es-array-multiple-times

filterx/filterx-scope: don't move the variables array multiple times
  • Loading branch information
OverOrion authored Jan 2, 2025
2 parents c1ad6e3 + 2265ade commit 010230d
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 8 deletions.
27 changes: 21 additions & 6 deletions lib/filterx/filterx-scope.c
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,7 @@ filterx_scope_new(void)

g_atomic_counter_set(&self->ref_cnt, 1);
self->variables = g_array_sized_new(FALSE, TRUE, sizeof(FilterXVariable), 16);
g_array_set_clear_func(self->variables, (GDestroyNotify) filterx_variable_free_method);
g_array_set_clear_func(self->variables, (GDestroyNotify) filterx_variable_free);
return self;
}

Expand Down Expand Up @@ -377,16 +377,31 @@ filterx_scope_invalidate_log_msg_cache(FilterXScope *self)
{
g_assert(filterx_scope_has_log_msg_changes(self));

gint i = 0;
while (i < self->variables->len)
/* this is a bit hacky and the solution would be to get rid of the GArray
* wrapper. GArray does not allow us to remove multiple elements in a
* single loop, without moving the array multiple times. So we basically
* open code this instead of multiple calls to g_array_remove_index()
*/

gint src_index, dst_index;
for (src_index = 0, dst_index = 0; src_index < self->variables->len; src_index++)
{
FilterXVariable *v = &g_array_index(self->variables, FilterXVariable, i);
FilterXVariable *v = &g_array_index(self->variables, FilterXVariable, src_index);

if (!filterx_variable_is_floating(v) && self->syncable)
g_array_remove_index(self->variables, i);
{
/* skip this variable */
filterx_variable_free(v);
}
else
i++;
{
if (src_index != dst_index)
g_array_index(self->variables, FilterXVariable, dst_index) = g_array_index(self->variables, FilterXVariable, src_index);
dst_index++;
}
}
/* and this is the HACK: we reset the "len" member by poking that inside the GArray data structure */
self->variables->len = dst_index;

filterx_scope_clear_log_msg_has_changes(self);
}
2 changes: 1 addition & 1 deletion lib/filterx/filterx-variable.c
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ filterx_map_varname_to_handle(const gchar *name, FilterXVariableType type)
}

void
filterx_variable_free_method(FilterXVariable *v)
filterx_variable_free(FilterXVariable *v)
{
filterx_object_unref(v->value);
}
Expand Down
2 changes: 1 addition & 1 deletion lib/filterx/filterx-variable.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ typedef enum

void filterx_variable_init_instance(FilterXVariable *v, FilterXVariableHandle handle,
FilterXObject *initial_value, guint32 generation);
void filterx_variable_free_method(FilterXVariable *v);
void filterx_variable_free(FilterXVariable *v);

#define FILTERX_HANDLE_FLOATING_BIT (1UL << 31)

Expand Down

0 comments on commit 010230d

Please sign in to comment.