From 3f96d79e962d625d20a818335bd0e20bb7598bd6 Mon Sep 17 00:00:00 2001 From: Balazs Scheidler Date: Sat, 28 Dec 2024 19:04:53 +0100 Subject: [PATCH] filterx/filterx-scope: don't move the variables array multiple times Optimize the removal of multiple elements. Signed-off-by: Balazs Scheidler --- lib/filterx/filterx-scope.c | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/lib/filterx/filterx-scope.c b/lib/filterx/filterx-scope.c index a00c7784a..9ec1f7b9f 100644 --- a/lib/filterx/filterx-scope.c +++ b/lib/filterx/filterx-scope.c @@ -377,15 +377,20 @@ 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) + for (gint 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); + { + ; + } 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++; + } } filterx_scope_clear_log_msg_has_changes(self);