Skip to content

Commit

Permalink
filterx: add scope generations to avoid having to clone the scope
Browse files Browse the repository at this point in the history
Instead of having to clone variables at every transition between filterx
scopes, add a "generation" counter which indicates if a previously
stored value is considered part of the scope or not.

At every transition we bump the generation counter of the scope (and at clone
we set it to 0). If our lookup returns a variable from a previous
generation, it is considered unset, just as we would unset it if
we were to sync() or clone() the scope.

Signed-off-by: Balazs Scheidler <[email protected]>
  • Loading branch information
bazsi committed Apr 19, 2024
1 parent 40fdb48 commit 9774bc4
Showing 1 changed file with 24 additions and 2 deletions.
26 changes: 24 additions & 2 deletions lib/filterx/filterx-scope.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
#include "scratch-buffers.h"

#define FILTERX_HANDLE_FLOATING_BIT (1UL << 31)
#define FILTERX_SCOPE_MAX_GENERATION ((1UL << 20) - 1)

struct _FilterXVariable
{
Expand All @@ -35,7 +36,8 @@ struct _FilterXVariable
* declared -- this variable is declared (e.g. retained for the entire input pipeline)
*/
guint32 assigned:1,
declared:1;
declared:1,
generation:20;
FilterXObject *value;
};

Expand Down Expand Up @@ -103,6 +105,7 @@ struct _FilterXScope
GPtrArray *weak_refs;
gboolean write_protected;
gboolean dirty;
gint generation;
};

static gboolean
Expand Down Expand Up @@ -166,7 +169,12 @@ filterx_scope_lookup_variable(FilterXScope *self, FilterXVariableHandle handle)
FilterXVariable *v;

if (_lookup_variable(self, handle, &v))
return v;
{
if (filterx_variable_handle_is_floating(handle) &&
!v->declared && v->generation != self->generation)
return NULL;
return v;
}
return NULL;
}

Expand All @@ -180,6 +188,16 @@ filterx_scope_register_variable(FilterXScope *self,
if (_lookup_variable(self, handle, &v_slot))
{
/* already present */
if (v_slot->generation != self->generation)
{
/* existing value is from a previous generation, override it as if
* it was a new value */

v_slot->generation = self->generation;
filterx_variable_set_value(v_slot, initial_value);
/* consider this to be unset just as an initial registration is */
v_slot->assigned = FALSE;
}
return v_slot;
}
/* turn v_slot into an index */
Expand All @@ -190,6 +208,7 @@ filterx_scope_register_variable(FilterXScope *self,
v.handle = handle;
v.assigned = FALSE;
v.value = filterx_object_ref(initial_value);
v.generation = self->generation;
g_array_insert_val(self->variables, v_index, v);

return &g_array_index(self->variables, FilterXVariable, v_index);
Expand Down Expand Up @@ -305,6 +324,7 @@ filterx_scope_clone(FilterXScope *other)
g_array_append_val(self->variables, *v);
FilterXVariable *v_clone = &g_array_index(self->variables, FilterXVariable, dst_index);

v_clone->generation = 0;
v_clone->value = filterx_object_clone(v->value);
dst_index++;
msg_trace("Filterx scope, cloning scope variable",
Expand Down Expand Up @@ -341,6 +361,8 @@ filterx_scope_make_writable(FilterXScope **pself)
filterx_scope_unref(*pself);
*pself = new;
}
(*pself)->generation++;
g_assert((*pself)->generation < FILTERX_SCOPE_MAX_GENERATION);
return *pself;
}

Expand Down

0 comments on commit 9774bc4

Please sign in to comment.