Skip to content

Commit

Permalink
Remove 20% of wasted memory allocations by not calling celast.Navigat…
Browse files Browse the repository at this point in the history
…eAST.
  • Loading branch information
tonyhb committed Jan 4, 2024
1 parent 49e3d60 commit 15b38ca
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 22 deletions.
10 changes: 3 additions & 7 deletions expr.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,13 +107,6 @@ func (a aggregator) ConstantLen() int {
}

func (a *aggregator) Evaluate(ctx context.Context, data map[string]any) ([]Evaluable, int32, error) {
// on event entered:
//
// 1. load pauses
// 2. pass event, pauses to aggregator
// 3. load nodes for pause, if none, run expression
// 4. evaluate tree nodes for pause against data, if ok, run expression

var (
err error
matched = int32(0)
Expand Down Expand Up @@ -171,6 +164,9 @@ func (a *aggregator) aggregateMatch(ctx context.Context, data map[string]any, pr
// TODO: Flip this. Instead of iterating through all fields in a potentially large input
// array, iterate through all known variables/idents in the aggregate tree to see if
// the data has those keys set.
//
// Also, we should iterate through the expression in a top-down order, ensuring that if
// any of the top groups fail to match we quit early.

result := []ExpressionPart{}
for k, v := range data {
Expand Down
30 changes: 15 additions & 15 deletions parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ func (p *parser) Parse(ctx context.Context, eval Evaluable) (*ParsedExpression,
node := newNode()
_, err := navigateAST(
expr{
NavigableExpr: celast.NavigateAST(ast.NativeRep()),
ast: ast.NativeRep().Expr(),
},
node,
vars,
Expand Down Expand Up @@ -318,7 +318,7 @@ func (p Predicate) TreeType() TreeType {

// expr is wrapper around the CEL AST which stores parsing-related data.
type expr struct {
celast.NavigableExpr
ast celast.Expr

// negated is true when this expr is part of a logical not branch,
// ie !($expr)
Expand All @@ -341,7 +341,7 @@ func navigateAST(nav expr, parent *Node, vars map[string]any) ([]*Node, error) {
item := stack[0]
stack = stack[1:]

switch item.Kind() {
switch item.ast.Kind() {
case celast.LiteralKind:
// This is a literal. Do nothing, as this is always true.
case celast.IdentKind:
Expand All @@ -357,17 +357,17 @@ func navigateAST(nav expr, parent *Node, vars map[string]any) ([]*Node, error) {
// what we're trying to parse, by taking the LHS and RHS of each opeartor then bringing
// this up into a tree.

fn := item.AsCall().FunctionName()
fn := item.ast.AsCall().FunctionName()

// Firstly, if this is a logical not, everything within this branch is negated:
// !(a == b). This flips the negated field, ie !(foo == bar) becomes foo != bar,
// whereas !(!(foo == bar)) stays the same.
if fn == operators.LogicalNot {
// Immediately navigate into this single expression.
child := item.Children()[0]
astChild := item.ast.AsCall().Args()[0]
stack = append(stack, expr{
NavigableExpr: child,
negated: !item.negated,
ast: astChild,
negated: !item.negated,
})
continue
}
Expand Down Expand Up @@ -396,7 +396,7 @@ func navigateAST(nav expr, parent *Node, vars map[string]any) ([]*Node, error) {

// For each &&, create a new child node in the .And field of the current
// high-level AST.
if item.AsCall().FunctionName() == operators.LogicalAnd {
if item.ast.AsCall().FunctionName() == operators.LogicalAnd {
stack = append(stack, peek(item, operators.LogicalAnd)...)
continue
}
Expand All @@ -407,7 +407,7 @@ func navigateAST(nav expr, parent *Node, vars map[string]any) ([]*Node, error) {
// We assume that this is being called with an ident as a comparator.
// Dependign on the LHS/RHS type, we want to organize the kind into
// a specific type of tree.
predicate := callToPredicate(item.NavigableExpr, item.negated, vars)
predicate := callToPredicate(item.ast, item.negated, vars)
if predicate == nil {
continue
}
Expand Down Expand Up @@ -437,17 +437,17 @@ func peek(nav expr, operator string) []expr {
item := stack[0]
stack = stack[1:]

if item.AsCall().FunctionName() == operator {
children := item.Children()
if item.ast.AsCall().FunctionName() == operator {
astChildren := item.ast.AsCall().Args()
stack = append(
stack,
expr{
NavigableExpr: children[0],
negated: nav.negated,
ast: astChildren[0],
negated: nav.negated,
},
expr{
NavigableExpr: children[1],
negated: nav.negated,
ast: astChildren[1],
negated: nav.negated,
},
)
continue
Expand Down

0 comments on commit 15b38ca

Please sign in to comment.