Skip to content

Commit

Permalink
test: add benchmark for compile ast node with cache
Browse files Browse the repository at this point in the history
  • Loading branch information
zhuliquan committed Apr 1, 2024
1 parent 9e80eec commit 3b2ddb9
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 11 deletions.
24 changes: 13 additions & 11 deletions ast/print.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import (
"github.com/expr-lang/expr/parser/utils"
)

var EnableCache bool

func (n *NilNode) String() string {
return "nil"
}
Expand Down Expand Up @@ -45,7 +47,7 @@ func (n *ConstantNode) String() string {
}

func (n *UnaryNode) String() string {
if n.strCache != "" {
if EnableCache && n.strCache != "" {
return n.strCache
}
op := n.Operator
Expand All @@ -61,7 +63,7 @@ func (n *UnaryNode) String() string {
}

func (n *BinaryNode) String() string {
if n.strCache != "" {
if EnableCache && n.strCache != "" {
return n.strCache
}
if n.Operator == ".." {
Expand Down Expand Up @@ -116,7 +118,7 @@ func (n *ChainNode) String() string {
}

func (n *MemberNode) String() string {
if n.strCache != "" {
if EnableCache && n.strCache != "" {
return n.strCache
}
if n.Optional {
Expand All @@ -138,7 +140,7 @@ func (n *MemberNode) String() string {
}

func (n *SliceNode) String() string {
if n.strCache != "" {
if EnableCache && n.strCache != "" {
return n.strCache
}
if n.From == nil && n.To == nil {
Expand All @@ -154,7 +156,7 @@ func (n *SliceNode) String() string {
}

func (n *CallNode) String() string {
if n.strCache != "" {
if EnableCache && n.strCache != "" {
return n.strCache
}
arguments := make([]string, len(n.Arguments))
Expand All @@ -166,7 +168,7 @@ func (n *CallNode) String() string {
}

func (n *BuiltinNode) String() string {
if n.strCache != "" {
if EnableCache && n.strCache != "" {
return n.strCache
}
arguments := make([]string, len(n.Arguments))
Expand All @@ -186,15 +188,15 @@ func (n *PointerNode) String() string {
}

func (n *VariableDeclaratorNode) String() string {
if n.strCache != "" {
if EnableCache && n.strCache != "" {
return n.strCache
}
n.strCache = fmt.Sprintf("let %s = %s; %s", n.Name, n.Value.String(), n.Expr.String())
return n.strCache
}

func (n *ConditionalNode) String() string {
if n.strCache != "" {
if EnableCache && n.strCache != "" {
return n.strCache
}
var cond, exp1, exp2 string
Expand All @@ -218,7 +220,7 @@ func (n *ConditionalNode) String() string {
}

func (n *ArrayNode) String() string {
if n.strCache != "" {
if EnableCache && n.strCache != "" {
return n.strCache
}
nodes := make([]string, len(n.Nodes))
Expand All @@ -230,7 +232,7 @@ func (n *ArrayNode) String() string {
}

func (n *MapNode) String() string {
if n.strCache != "" {
if EnableCache && n.strCache != "" {
return n.strCache
}
pairs := make([]string, len(n.Pairs))
Expand All @@ -242,7 +244,7 @@ func (n *MapNode) String() string {
}

func (n *PairNode) String() string {
if n.strCache != "" {
if EnableCache && n.strCache != "" {
return n.strCache
}
if str, ok := n.Key.(*StringNode); ok {
Expand Down
35 changes: 35 additions & 0 deletions compiler/compiler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import (
"github.com/stretchr/testify/require"

"github.com/expr-lang/expr"
"github.com/expr-lang/expr/ast"
"github.com/expr-lang/expr/conf"
"github.com/expr-lang/expr/test/mock"
"github.com/expr-lang/expr/test/playground"
"github.com/expr-lang/expr/vm"
Expand Down Expand Up @@ -589,3 +591,36 @@ func TestCompile_optimizes_jumps(t *testing.T) {
})
}
}

func BenchmarkCompileWithProfile(b *testing.B) {
withProfile := func() expr.Option {
return func(c *conf.Config) {
c.Profile = true
}
}

code := `let m = {"a": {"b": {"c": 1}}}; let n = {"x": {"y": 2}}; m?.a?.b?.c + n?.x?.y + testFunc(n?.x?.y, m?.a?.b?.c)`
ast.EnableCache = true
b.Run("enable print cache", func(b *testing.B) {
for i := 0; i < b.N; i++ {
_, _ = expr.Compile(code,
withProfile(),
expr.Function("testFunc", func(...interface{}) (interface{}, error) {
return nil, nil
}),
)
}
})

ast.EnableCache = false
b.Run("disable print cache", func(b *testing.B) {
for i := 0; i < b.N; i++ {
_, _ = expr.Compile(code,
withProfile(),
expr.Function("testFunc", func(...interface{}) (interface{}, error) {
return nil, nil
}),
)
}
})
}

0 comments on commit 3b2ddb9

Please sign in to comment.