Skip to content

Commit

Permalink
filterx/expr-function: avoid using GPtrArray in simple function imple…
Browse files Browse the repository at this point in the history
…mentation

Instead of passing the arguments in a GPtrArray, let's use a simple stack
allocated array, voiding a 2 mallocs and 2 frees in any simple function
invocations.

Signed-off-by: Balazs Scheidler <[email protected]>
  • Loading branch information
bazsi committed Dec 30, 2024
1 parent 7c7ef1f commit f2653e5
Show file tree
Hide file tree
Showing 32 changed files with 135 additions and 142 deletions.
52 changes: 23 additions & 29 deletions lib/filterx/expr-function.c
Original file line number Diff line number Diff line change
Expand Up @@ -88,49 +88,43 @@ _get_arg_object(FilterXSimpleFunction *self, guint64 index)
return filterx_expr_eval(expr);
}

static GPtrArray *
_simple_function_eval_args(FilterXSimpleFunction *self)
static gboolean
_simple_function_eval_args(FilterXSimpleFunction *self, FilterXObject **args, gsize *args_len)
{
GPtrArray *res = g_ptr_array_new_full(self->args->len, (GDestroyNotify) filterx_object_unref);

for (guint64 i = 0; i < self->args->len; i++)
gsize n = *args_len;
for (gsize i = 0; i < n; i++)
{
FilterXObject *obj = _get_arg_object(self, i);
if (obj == NULL)
goto error;

g_ptr_array_add(res, obj);
if ((args[i] = _get_arg_object(self, i)) == NULL)
{
*args_len = i;
return FALSE;
}
}
*args_len = n;
return TRUE;
}

return res;

error:
g_ptr_array_free(res, TRUE);
return NULL;
static void
_simple_function_free_args(FilterXObject *args[], gsize args_len)
{
for (gsize i = 0; i < args_len; i++)
filterx_object_unref(args[i]);
}

static FilterXObject *
_simple_eval(FilterXExpr *s)
{
FilterXSimpleFunction *self = (FilterXSimpleFunction *) s;
gsize args_len = self->args->len;
FilterXObject *args[self->args->len];
FilterXObject *res = NULL;

GPtrArray *args = NULL;

if (self->args->len)
if (_simple_function_eval_args(self, args, &args_len))
{
args = _simple_function_eval_args(self);
if (!args)
return NULL;
res = self->function_proto(s, args, args_len);
}

FilterXSimpleFunctionProto f = self->function_proto;

g_assert(f != NULL);
FilterXObject *res = f(s, args);

if (args != NULL)
g_ptr_array_free(args, TRUE);

_simple_function_free_args(args, args_len);
return res;
}

Expand Down
2 changes: 1 addition & 1 deletion lib/filterx/expr-function.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
#include "generic-number.h"
#include "plugin.h"

typedef FilterXObject *(*FilterXSimpleFunctionProto)(FilterXExpr *s, GPtrArray *);
typedef FilterXObject *(*FilterXSimpleFunctionProto)(FilterXExpr *s, FilterXObject *args[], gsize args_len);

void filterx_simple_function_argument_error(FilterXExpr *s, gchar *error_info, gboolean free_info);

Expand Down
7 changes: 3 additions & 4 deletions lib/filterx/filterx-globals.c
Original file line number Diff line number Diff line change
Expand Up @@ -277,14 +277,13 @@ filterx_global_deinit(void)
}

FilterXObject *
filterx_typecast_get_arg(FilterXExpr *s, GPtrArray *args)
filterx_typecast_get_arg(FilterXExpr *s, FilterXObject *args[], gsize args_len)
{
if (args == NULL || args->len != 1)
if (args == NULL || args_len != 1)
{
filterx_simple_function_argument_error(s, "Requires exactly one argument", FALSE);
return NULL;
}

FilterXObject *object = g_ptr_array_index(args, 0);
return object;
return args[0];
}
2 changes: 1 addition & 1 deletion lib/filterx/filterx-globals.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,6 @@ FilterXType *filterx_type_lookup(const gchar *type_name);
gboolean filterx_type_register(const gchar *type_name, FilterXType *fxtype);

// Helpers
FilterXObject *filterx_typecast_get_arg(FilterXExpr *s, GPtrArray *args);
FilterXObject *filterx_typecast_get_arg(FilterXExpr *s, FilterXObject *args[], gsize args_len);

#endif
6 changes: 3 additions & 3 deletions lib/filterx/func-len.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,15 @@
#define FILTERX_FUNC_LEN_USAGE "Usage: len(object)"

FilterXObject *
filterx_simple_function_len(FilterXExpr *s, GPtrArray *args)
filterx_simple_function_len(FilterXExpr *s, FilterXObject *args[], gsize args_len)
{
if (args == NULL || args->len != 1)
if (args == NULL || args_len != 1)
{
filterx_simple_function_argument_error(s, "Requires exactly one argument", FALSE);
return NULL;
}

FilterXObject *object = g_ptr_array_index(args, 0);
FilterXObject *object = args[0];

guint64 len;
gboolean success = filterx_object_len(object, &len);
Expand Down
2 changes: 1 addition & 1 deletion lib/filterx/func-len.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,6 @@

#include "filterx/expr-function.h"

FilterXObject *filterx_simple_function_len(FilterXExpr *s, GPtrArray *args);
FilterXObject *filterx_simple_function_len(FilterXExpr *s, FilterXObject *args[], gsize args_len);

#endif
4 changes: 2 additions & 2 deletions lib/filterx/func-sdata.c
Original file line number Diff line number Diff line change
Expand Up @@ -118,9 +118,9 @@ filterx_function_is_sdata_from_enterprise_new(FilterXFunctionArgs *args, GError


FilterXObject *
filterx_simple_function_has_sdata(FilterXExpr *s, GPtrArray *args)
filterx_simple_function_has_sdata(FilterXExpr *s, FilterXObject *args[], gsize args_len)
{
if (args && args->len != 0)
if (args && args_len != 0)
{
filterx_simple_function_argument_error(s, "Incorrect number of arguments", FALSE);
return NULL;
Expand Down
2 changes: 1 addition & 1 deletion lib/filterx/func-sdata.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
#include "filterx/expr-function.h"

FilterXExpr *filterx_function_is_sdata_from_enterprise_new(FilterXFunctionArgs *args, GError **error);
FilterXObject *filterx_simple_function_has_sdata(FilterXExpr *s, GPtrArray *args);
FilterXObject *filterx_simple_function_has_sdata(FilterXExpr *s, FilterXObject *args[], gsize args_len);
FilterXExpr *filterx_generator_function_get_sdata_new(FilterXFunctionArgs *args, GError **error);
typedef struct FilterXGenFuncGetSdata_
{
Expand Down
14 changes: 7 additions & 7 deletions lib/filterx/func-str-transform.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,17 +28,17 @@
#include "filterx/filterx-eval.h"

static const gchar *
_extract_str_arg(FilterXExpr *s, GPtrArray *args, gssize *len)
_extract_str_arg(FilterXExpr *s, FilterXObject *args[], gsize args_len, gssize *len)
{
if (args == NULL || args->len != 1)
if (args == NULL || args_len != 1)
{
filterx_simple_function_argument_error(s, "Requires exactly one argument", FALSE);
return NULL;
}

const gchar *str;
gsize inner_len;
FilterXObject *object = g_ptr_array_index(args, 0);
FilterXObject *object = args[0];

if (!filterx_object_extract_string_ref(object, &str, &inner_len))
{
Expand All @@ -51,10 +51,10 @@ _extract_str_arg(FilterXExpr *s, GPtrArray *args, gssize *len)
}

FilterXObject *
filterx_simple_function_lower(FilterXExpr *s, GPtrArray *args)
filterx_simple_function_lower(FilterXExpr *s, FilterXObject *args[], gsize args_len)
{
gssize len;
const gchar *str = _extract_str_arg(s, args, &len);
const gchar *str = _extract_str_arg(s, args, args_len, &len);
if (!str)
return NULL;

Expand All @@ -66,10 +66,10 @@ filterx_simple_function_lower(FilterXExpr *s, GPtrArray *args)
}

FilterXObject *
filterx_simple_function_upper(FilterXExpr *s, GPtrArray *args)
filterx_simple_function_upper(FilterXExpr *s, FilterXObject *args[], gsize args_len)
{
gssize len;
const gchar *str = _extract_str_arg(s, args, &len);
const gchar *str = _extract_str_arg(s, args, args_len, &len);
if (!str)
return NULL;

Expand Down
4 changes: 2 additions & 2 deletions lib/filterx/func-str-transform.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@

#include "filterx/expr-function.h"

FilterXObject *filterx_simple_function_lower(FilterXExpr *s, GPtrArray *args);
FilterXObject *filterx_simple_function_upper(FilterXExpr *s, GPtrArray *args);
FilterXObject *filterx_simple_function_lower(FilterXExpr *s, FilterXObject *args[], gsize args_len);
FilterXObject *filterx_simple_function_upper(FilterXExpr *s, FilterXObject *args[], gsize args_len);

#endif
10 changes: 5 additions & 5 deletions lib/filterx/func-vars.c
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,9 @@ _add_to_dict(FilterXVariable *variable, gpointer user_data)
}

FilterXObject *
filterx_simple_function_vars(FilterXExpr *s, GPtrArray *args)
filterx_simple_function_vars(FilterXExpr *s, FilterXObject *args[], gsize args_len)
{
if (args && args->len != 0)
if (args && args_len != 0)
{
filterx_simple_function_argument_error(s, "Incorrect number of arguments", FALSE);
return NULL;
Expand Down Expand Up @@ -150,15 +150,15 @@ _load_from_dict(FilterXObject *key, FilterXObject *value, gpointer user_data)
}

FilterXObject *
filterx_simple_function_load_vars(FilterXExpr *s, GPtrArray *args)
filterx_simple_function_load_vars(FilterXExpr *s, FilterXObject *args[], gsize args_len)
{
if (!args || args->len != 1)
if (!args || args_len != 1)
{
filterx_simple_function_argument_error(s, "Incorrect number of arguments", FALSE);
return NULL;
}

FilterXObject *vars = g_ptr_array_index(args, 0);
FilterXObject *vars = args[0];
FilterXObject *vars_unwrapped = filterx_ref_unwrap_ro(vars);
FilterXObject *vars_unmarshalled = NULL;

Expand Down
4 changes: 2 additions & 2 deletions lib/filterx/func-vars.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@

#include "filterx/expr-function.h"

FilterXObject *filterx_simple_function_vars(FilterXExpr *s, GPtrArray *args);
FilterXObject *filterx_simple_function_load_vars(FilterXExpr *s, GPtrArray *args);
FilterXObject *filterx_simple_function_vars(FilterXExpr *s, FilterXObject *args[], gsize args_len);
FilterXObject *filterx_simple_function_load_vars(FilterXExpr *s, FilterXObject *args[], gsize args_len);

#endif
10 changes: 5 additions & 5 deletions lib/filterx/object-datetime.c
Original file line number Diff line number Diff line change
Expand Up @@ -121,9 +121,9 @@ filterx_datetime_get_value(FilterXObject *s)


FilterXObject *
filterx_typecast_datetime(FilterXExpr *s, GPtrArray *args)
filterx_typecast_datetime(FilterXExpr *s, FilterXObject *args[], gsize args_len)
{
FilterXObject *object = filterx_typecast_get_arg(s, args);
FilterXObject *object = filterx_typecast_get_arg(s, args, args_len);
if (!object)
return NULL;

Expand All @@ -147,13 +147,13 @@ filterx_typecast_datetime(FilterXExpr *s, GPtrArray *args)
return filterx_datetime_new(&ut);
}

return filterx_typecast_datetime_isodate(s, args);
return filterx_typecast_datetime_isodate(s, args, args_len);
}

FilterXObject *
filterx_typecast_datetime_isodate(FilterXExpr *s, GPtrArray *args)
filterx_typecast_datetime_isodate(FilterXExpr *s, FilterXObject *args[], gsize args_len)
{
FilterXObject *object = filterx_typecast_get_arg(s, args);
FilterXObject *object = filterx_typecast_get_arg(s, args, args_len);
if (!object)
return NULL;

Expand Down
4 changes: 2 additions & 2 deletions lib/filterx/object-datetime.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ FILTERX_DECLARE_TYPE(datetime);

FilterXObject *filterx_datetime_new(const UnixTime *ut);
UnixTime filterx_datetime_get_value(FilterXObject *s);
FilterXObject *filterx_typecast_datetime(FilterXExpr *s, GPtrArray *args);
FilterXObject *filterx_typecast_datetime_isodate(FilterXExpr *, GPtrArray *args);
FilterXObject *filterx_typecast_datetime(FilterXExpr *s, FilterXObject *args[], gsize args_len);
FilterXObject *filterx_typecast_datetime_isodate(FilterXExpr *, FilterXObject *args[], gsize args_len);
FilterXExpr *filterx_function_strptime_new(FilterXFunctionArgs *args, GError **error);
FilterXExpr *filterx_function_strftime_new(FilterXFunctionArgs *args, GError **error);

Expand Down
8 changes: 4 additions & 4 deletions lib/filterx/object-json-array.c
Original file line number Diff line number Diff line change
Expand Up @@ -316,18 +316,18 @@ filterx_json_array_new_from_syslog_ng_list(const gchar *repr, gssize repr_len)
}

FilterXObject *
filterx_json_array_new_from_args(FilterXExpr *s, GPtrArray *args)
filterx_json_array_new_from_args(FilterXExpr *s, FilterXObject *args[], gsize args_len)
{
if (!args || args->len == 0)
if (!args || args_len == 0)
return filterx_json_array_new_empty();

if (args->len != 1)
if (args_len != 1)
{
filterx_simple_function_argument_error(s, "Requires zero or one argument", FALSE);
return NULL;
}

FilterXObject *arg = (FilterXObject *) g_ptr_array_index(args, 0);
FilterXObject *arg = args[0];

FilterXObject *json_arr = filterx_ref_unwrap_ro(arg);
if (filterx_object_is_type(json_arr, &FILTERX_TYPE_NAME(json_array)))
Expand Down
8 changes: 4 additions & 4 deletions lib/filterx/object-json.c
Original file line number Diff line number Diff line change
Expand Up @@ -186,18 +186,18 @@ filterx_json_new_from_repr(const gchar *repr, gssize repr_len)
}

FilterXObject *
filterx_json_new_from_args(FilterXExpr *s, GPtrArray *args)
filterx_json_new_from_args(FilterXExpr *s, FilterXObject *args[], gsize args_len)
{
if (!args || args->len == 0)
if (!args || args_len == 0)
return filterx_json_object_new_empty();

if (args->len != 1)
if (args_len != 1)
{
filterx_eval_push_error("Too many arguments", s, NULL);
return NULL;
}

FilterXObject *arg = (FilterXObject *) g_ptr_array_index(args, 0);
FilterXObject *arg = args[0];

FilterXObject *arg_unwrapped = filterx_ref_unwrap_ro(arg);
if (filterx_object_is_type(arg_unwrapped, &FILTERX_TYPE_NAME(json_array)) ||
Expand Down
4 changes: 2 additions & 2 deletions lib/filterx/object-json.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@ FilterXObject *filterx_json_array_new_from_syslog_ng_list(const gchar *repr, gss
FilterXObject *filterx_json_object_new_empty(void);
FilterXObject *filterx_json_array_new_empty(void);

FilterXObject *filterx_json_new_from_args(FilterXExpr *s, GPtrArray *args);
FilterXObject *filterx_json_array_new_from_args(FilterXExpr *s, GPtrArray *args);
FilterXObject *filterx_json_new_from_args(FilterXExpr *s, FilterXObject *args[], gsize args_len);
FilterXObject *filterx_json_array_new_from_args(FilterXExpr *s, FilterXObject *args[], gsize args_len);

FilterXObject *filterx_json_new_from_object(struct json_object *object);

Expand Down
14 changes: 7 additions & 7 deletions lib/filterx/object-metrics-labels.c
Original file line number Diff line number Diff line change
Expand Up @@ -293,9 +293,9 @@ filterx_object_metrics_labels_new(guint reserved_size)
}

FilterXObject *
filterx_simple_function_metrics_labels(FilterXExpr *s, GPtrArray *args)
filterx_simple_function_metrics_labels(FilterXExpr *s, FilterXObject *args[], gsize args_len)
{
if (args && args->len)
if (args && args_len)
{
filterx_simple_function_argument_error(s, "unexpected argument.", FALSE);
return NULL;
Expand All @@ -305,16 +305,16 @@ filterx_simple_function_metrics_labels(FilterXExpr *s, GPtrArray *args)
}

static FilterXObject *
_dedup_extract_obj_arg(FilterXExpr *s, GPtrArray *args)
_dedup_extract_obj_arg(FilterXExpr *s, FilterXObject *args[], gsize args_len)
{
if (!args || args->len != 1)
if (!args || args_len != 1)
{
filterx_simple_function_argument_error(s, "unexpected number of arguments. "
DEDUP_METRICS_LABELS_USAGE, FALSE);
return NULL;
}

FilterXObject *obj = g_ptr_array_index(args, 0);
FilterXObject *obj = args[0];
FilterXObject *typed_obj = filterx_ref_unwrap_ro(obj);
if (!filterx_object_is_type(typed_obj, &FILTERX_TYPE_NAME(metrics_labels)))
{
Expand All @@ -327,9 +327,9 @@ _dedup_extract_obj_arg(FilterXExpr *s, GPtrArray *args)
}

FilterXObject *
filterx_simple_function_dedup_metrics_labels(FilterXExpr *s, GPtrArray *args)
filterx_simple_function_dedup_metrics_labels(FilterXExpr *s, FilterXObject *args[], gsize args_len)
{
FilterXObject *obj = _dedup_extract_obj_arg(s, args);
FilterXObject *obj = _dedup_extract_obj_arg(s, args, args_len);
if (!obj)
return NULL;

Expand Down
Loading

0 comments on commit f2653e5

Please sign in to comment.