From b7a3754eff26f73c509671168c6814d0621f0fde Mon Sep 17 00:00:00 2001 From: Balazs Scheidler Date: Mon, 13 May 2024 18:37:29 +0200 Subject: [PATCH] filterx: skip FilterXScope sync if possible Signed-off-by: Balazs Scheidler --- lib/filterx/filterx-scope.c | 38 +++++++++++++++++++++++-------------- 1 file changed, 24 insertions(+), 14 deletions(-) diff --git a/lib/filterx/filterx-scope.c b/lib/filterx/filterx-scope.c index fbf757459b3..21c5b760e49 100644 --- a/lib/filterx/filterx-scope.c +++ b/lib/filterx/filterx-scope.c @@ -96,9 +96,7 @@ struct _FilterXScope { GAtomicCounter ref_cnt; GArray *variables; - gboolean write_protected; - gboolean dirty; - gint generation; + guint32 generation:20, write_protected, dirty, syncable; }; static gboolean @@ -215,6 +213,10 @@ filterx_scope_register_variable(FilterXScope *self, FilterXVariable *v = _register_variable(self, handle, initial_value); v->declared = FALSE; + /* the scope needs to be synced with the message if it holds a + * message-tied variable (e.g. $MSG) */ + if (!filterx_variable_handle_is_floating(handle)) + self->syncable = TRUE; return v; } @@ -245,6 +247,13 @@ filterx_scope_sync(FilterXScope *self, LogMessage *msg) evt_tag_printf("scope", "%p", self)); return; } + if (!self->syncable) + { + msg_trace("Filterx sync: not syncing as there are no variables to sync", + evt_tag_printf("scope", "%p", self)); + self->dirty = FALSE; + return; + } GString *buffer = scratch_buffers_alloc(); @@ -262,17 +271,16 @@ filterx_scope_sync(FilterXScope *self, LogMessage *msg) */ if (filterx_variable_is_floating(v)) { - if (!v->declared) - { - msg_trace("Filterx sync: undeclared floating variable, unsetting", - evt_tag_str("variable", log_msg_get_value_name(filterx_variable_get_nv_handle(v), NULL))); - filterx_variable_set_value(v, NULL); - } - else - { - msg_trace("Filterx sync: declared floating variable, keeping it", - evt_tag_str("variable", log_msg_get_value_name(filterx_variable_get_nv_handle(v), NULL))); - } + /* we could unset undeclared, floating values here as we are + * transitioning to the next filterx block. But this is also + * addressed by the variable generation counter mechanism. This + * means that we will unset those if some expr actually refers to + * it. Also, we skip the entire sync exercise, unless we have + * message tied values, so we need to work even if floating + * variables are not synced. + * + * With that said, let's not clear these. + */ } else if (v->value == NULL) { @@ -343,10 +351,12 @@ filterx_scope_clone(FilterXScope *other) if (other->variables->len > 0) self->dirty = other->dirty; + self->syncable = other->syncable; msg_trace("Filterx clone finished", evt_tag_printf("scope", "%p", self), evt_tag_printf("other", "%p", other), evt_tag_int("dirty", self->dirty), + evt_tag_int("syncable", self->syncable), evt_tag_int("write_protected", self->write_protected)); /* NOTE: we don't clone weak references, those only relate to mutable * objects, which we are cloning anyway */