Skip to content

Commit

Permalink
filterx: optimize plus operator for literal operands
Browse files Browse the repository at this point in the history
Signed-off-by: László Várady <laszlo.varady@anno.io>
  • Loading branch information
MrAnno committed Nov 23, 2024
1 parent 54b934d commit 7426e9a
Showing 1 changed file with 32 additions and 2 deletions.
34 changes: 32 additions & 2 deletions lib/filterx/expr-plus.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,23 +23,28 @@
#include "expr-plus.h"
#include "object-string.h"
#include "filterx-eval.h"
#include "filterx/expr-literal.h"
#include "scratch-buffers.h"

typedef struct FilterXOperatorPlus
{
FilterXBinaryOp super;
FilterXObject *literal_lhs;
FilterXObject *literal_rhs;
} FilterXOperatorPlus;

static FilterXObject *
_eval(FilterXExpr *s)
{
FilterXOperatorPlus *self = (FilterXOperatorPlus *) s;

FilterXObject *lhs_object = filterx_expr_eval_typed(self->super.lhs);
FilterXObject *lhs_object = self->literal_lhs ? filterx_object_ref(self->literal_lhs)
: filterx_expr_eval_typed(self->super.lhs);
if (!lhs_object)
return NULL;

FilterXObject *rhs_object = filterx_expr_eval(self->super.rhs);
FilterXObject *rhs_object = self->literal_rhs ? filterx_object_ref(self->literal_rhs)
: filterx_expr_eval(self->super.rhs);
if (!rhs_object)
{
filterx_object_unref(lhs_object);
Expand All @@ -52,11 +57,36 @@ _eval(FilterXExpr *s)
return res;
}

static void
_filterx_operator_plus_free(FilterXExpr *s)
{
FilterXOperatorPlus *self = (FilterXOperatorPlus *) s;
filterx_object_unref(self->literal_lhs);
filterx_object_unref(self->literal_rhs);

filterx_binary_op_free_method(s);
}

FilterXExpr *
filterx_operator_plus_new(FilterXExpr *lhs, FilterXExpr *rhs)
{
FilterXOperatorPlus *self = g_new0(FilterXOperatorPlus, 1);
filterx_binary_op_init_instance(&self->super, lhs, rhs);
self->super.super.eval = _eval;
self->super.super.free_fn = _filterx_operator_plus_free;

if (filterx_expr_is_literal(lhs))
self->literal_lhs = filterx_expr_eval_typed(lhs);

if (filterx_expr_is_literal(rhs))
self->literal_rhs = filterx_expr_eval(rhs);

if (filterx_expr_is_literal(lhs) && filterx_expr_is_literal(rhs))
{
FilterXExpr *optimized = filterx_literal_new(_eval(&self->super.super));
filterx_expr_unref(&self->super.super);
return optimized;
}

return &self->super.super;
}

0 comments on commit 7426e9a

Please sign in to comment.