Skip to content

Commit 010230d

Browse files
authored
Merge pull request #429 from bazsi/filterx-scope-avoid-moving-variables-array-multiple-times
filterx/filterx-scope: don't move the variables array multiple times
2 parents c1ad6e3 + 2265ade commit 010230d

File tree

3 files changed

+23
-8
lines changed

3 files changed

+23
-8
lines changed

lib/filterx/filterx-scope.c

+21-6
Original file line numberDiff line numberDiff line change
@@ -284,7 +284,7 @@ filterx_scope_new(void)
284284

285285
g_atomic_counter_set(&self->ref_cnt, 1);
286286
self->variables = g_array_sized_new(FALSE, TRUE, sizeof(FilterXVariable), 16);
287-
g_array_set_clear_func(self->variables, (GDestroyNotify) filterx_variable_free_method);
287+
g_array_set_clear_func(self->variables, (GDestroyNotify) filterx_variable_free);
288288
return self;
289289
}
290290

@@ -377,16 +377,31 @@ filterx_scope_invalidate_log_msg_cache(FilterXScope *self)
377377
{
378378
g_assert(filterx_scope_has_log_msg_changes(self));
379379

380-
gint i = 0;
381-
while (i < self->variables->len)
380+
/* this is a bit hacky and the solution would be to get rid of the GArray
381+
* wrapper. GArray does not allow us to remove multiple elements in a
382+
* single loop, without moving the array multiple times. So we basically
383+
* open code this instead of multiple calls to g_array_remove_index()
384+
*/
385+
386+
gint src_index, dst_index;
387+
for (src_index = 0, dst_index = 0; src_index < self->variables->len; src_index++)
382388
{
383-
FilterXVariable *v = &g_array_index(self->variables, FilterXVariable, i);
389+
FilterXVariable *v = &g_array_index(self->variables, FilterXVariable, src_index);
384390

385391
if (!filterx_variable_is_floating(v) && self->syncable)
386-
g_array_remove_index(self->variables, i);
392+
{
393+
/* skip this variable */
394+
filterx_variable_free(v);
395+
}
387396
else
388-
i++;
397+
{
398+
if (src_index != dst_index)
399+
g_array_index(self->variables, FilterXVariable, dst_index) = g_array_index(self->variables, FilterXVariable, src_index);
400+
dst_index++;
401+
}
389402
}
403+
/* and this is the HACK: we reset the "len" member by poking that inside the GArray data structure */
404+
self->variables->len = dst_index;
390405

391406
filterx_scope_clear_log_msg_has_changes(self);
392407
}

lib/filterx/filterx-variable.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ filterx_map_varname_to_handle(const gchar *name, FilterXVariableType type)
3939
}
4040

4141
void
42-
filterx_variable_free_method(FilterXVariable *v)
42+
filterx_variable_free(FilterXVariable *v)
4343
{
4444
filterx_object_unref(v->value);
4545
}

lib/filterx/filterx-variable.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ typedef enum
5858

5959
void filterx_variable_init_instance(FilterXVariable *v, FilterXVariableHandle handle,
6060
FilterXObject *initial_value, guint32 generation);
61-
void filterx_variable_free_method(FilterXVariable *v);
61+
void filterx_variable_free(FilterXVariable *v);
6262

6363
#define FILTERX_HANDLE_FLOATING_BIT (1UL << 31)
6464

0 commit comments

Comments
 (0)