Skip to content

Commit

Permalink
filterx: implement boolean, integer and null cache object caches
Browse files Browse the repository at this point in the history
Signed-off-by: Balazs Scheidler <balazs.scheidler@axoflow.com>
  • Loading branch information
bazsi committed Apr 21, 2024
1 parent 1453c46 commit 310e55f
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 26 deletions.
4 changes: 4 additions & 0 deletions lib/filterx/filterx-globals.c
Original file line number Diff line number Diff line change
Expand Up @@ -138,13 +138,17 @@ filterx_global_init(void)
filterx_type_init(&FILTERX_TYPE_NAME(datetime));
filterx_type_init(&FILTERX_TYPE_NAME(message_value));

filterx_primitive_global_init();
filterx_null_global_init();
filterx_builtin_functions_init();
}

void
filterx_global_deinit(void)
{
filterx_builtin_functions_deinit();
filterx_null_global_deinit();
filterx_primitive_global_deinit();
filterx_types_deinit();
}

Expand Down
24 changes: 22 additions & 2 deletions lib/filterx/object-null.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@
*
*/
#include "object-null.h"
#include "filterx-globals.h"

static FilterXObject *null_object;

static gboolean
_truthy(FilterXObject *s)
Expand Down Expand Up @@ -55,11 +58,16 @@ _null_repr(FilterXObject *s, GString *repr)
return null_repr(repr);
}

FilterXObject *
_null_wrap(void)
{
return filterx_object_new(&FILTERX_TYPE_NAME(null));
}

FilterXObject *
filterx_null_new(void)
{
FilterXObject *self = filterx_object_new(&FILTERX_TYPE_NAME(null));
return self;
return filterx_object_ref(null_object);
}

FILTERX_DEFINE_TYPE(null, FILTERX_TYPE_NAME(object),
Expand All @@ -68,3 +76,15 @@ FILTERX_DEFINE_TYPE(null, FILTERX_TYPE_NAME(object),
.repr = _null_repr,
.truthy = _truthy,
);

void
filterx_null_global_init(void)
{
filterx_cache_object(&null_object, _null_wrap());
}

void
filterx_null_global_deinit(void)
{
filterx_uncache_object(&null_object);
}
2 changes: 2 additions & 0 deletions lib/filterx/object-null.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,5 +30,7 @@ FILTERX_DECLARE_TYPE(null);
FilterXObject *filterx_null_new(void);

gboolean null_repr(GString *repr);
void filterx_null_global_init(void);
void filterx_null_global_deinit(void);

#endif
60 changes: 36 additions & 24 deletions lib/filterx/object-primitive.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,12 @@

#include "compat/json.h"

#define FILTERX_BOOL_CACHE_LIMIT 2
#define FILTERX_INTEGER_CACHE_LIMIT 100

static FilterXObject *bool_cache[FILTERX_BOOL_CACHE_LIMIT];
static FilterXObject *integer_cache[FILTERX_INTEGER_CACHE_LIMIT];

static gboolean
_truthy(FilterXObject *s)
{
Expand Down Expand Up @@ -72,14 +78,22 @@ _integer_marshal(FilterXObject *s, GString *repr, LogMessageValueType *t)
return integer_repr(gn_as_int64(&self->value), repr);
}

FilterXObject *
filterx_integer_new(gint64 value)
static inline FilterXObject *
_integer_wrap(gint64 value)
{
FilterXPrimitive *self = filterx_primitive_new(&FILTERX_TYPE_NAME(integer));
gn_set_int64(&self->value, value);
return &self->super;
}

FilterXObject *
filterx_integer_new(gint64 value)
{
if (value >= -1 && value < FILTERX_INTEGER_CACHE_LIMIT - 1)
return filterx_object_ref(integer_cache[value + 1]);
return _integer_wrap(value);
}

static gboolean
_double_map_to_json(FilterXObject *s, struct json_object **object)
{
Expand Down Expand Up @@ -142,7 +156,7 @@ _bool_map_to_json(FilterXObject *s, struct json_object **object)
}

FilterXObject *
_filterx_boolean_new(gboolean value)
_bool_wrap(gboolean value)
{
FilterXPrimitive *self = filterx_primitive_new(&FILTERX_TYPE_NAME(boolean));
gn_set_int64(&self->value, (gint64) value);
Expand All @@ -152,27 +166,7 @@ _filterx_boolean_new(gboolean value)
FilterXObject *
filterx_boolean_new(gboolean value)
{
static FilterXObject *true_object = NULL;
static FilterXObject *false_object = NULL;

if (value)
{
if (!true_object)
{
true_object = _filterx_boolean_new(TRUE);
filterx_object_freeze(true_object);
}
return filterx_object_ref(true_object);
}
else
{
if (!false_object)
{
false_object = _filterx_boolean_new(FALSE);
filterx_object_freeze(false_object);
}
return filterx_object_ref(false_object);
}
return filterx_object_ref(bool_cache[!!(value)]);
}

static gboolean
Expand Down Expand Up @@ -346,3 +340,21 @@ FILTERX_DEFINE_TYPE(boolean, FILTERX_TYPE_NAME(object),
.map_to_json = _bool_map_to_json,
.repr = _repr,
);

void
filterx_primitive_global_init(void)
{
filterx_cache_object(&bool_cache[FALSE], _bool_wrap(FALSE));
filterx_cache_object(&bool_cache[TRUE], _bool_wrap(TRUE));
for (guint64 i = 0; i < FILTERX_INTEGER_CACHE_LIMIT; i++)
filterx_cache_object(&integer_cache[i], _integer_wrap(i - 1));
}

void
filterx_primitive_global_deinit(void)
{
filterx_uncache_object(&bool_cache[FALSE]);
filterx_uncache_object(&bool_cache[TRUE]);
for (guint64 i = 0; i < FILTERX_INTEGER_CACHE_LIMIT; i++)
filterx_uncache_object(&integer_cache[i]);
}
4 changes: 4 additions & 0 deletions lib/filterx/object-primitive.h
Original file line number Diff line number Diff line change
Expand Up @@ -89,4 +89,8 @@ filterx_boolean_unwrap(FilterXObject *s, gboolean *value)
return TRUE;
}

void filterx_primitive_global_init(void);
void filterx_primitive_global_deinit(void);


#endif

0 comments on commit 310e55f

Please sign in to comment.