Skip to content

Make expr.Eval faster #775

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Mar 24, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions bench_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,26 @@ func Benchmark_expr(b *testing.B) {
require.True(b, out.(bool))
}

func Benchmark_expr_eval(b *testing.B) {
params := make(map[string]any)
params["Origin"] = "MOW"
params["Country"] = "RU"
params["Adults"] = 1
params["Value"] = 100

var out any
var err error

b.ResetTimer()
for n := 0; n < b.N; n++ {
out, err = expr.Eval(`(Origin == "MOW" || Country == "RU") && (Value >= 100 || Adults == 1)`, params)
}
b.StopTimer()

require.NoError(b, err)
require.True(b, out.(bool))
}

func Benchmark_expr_reuseVm(b *testing.B) {
params := make(map[string]any)
params["Origin"] = "MOW"
Expand Down
12 changes: 9 additions & 3 deletions builtin/builtin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -246,9 +246,15 @@ func TestBuiltin_errors(t *testing.T) {
}
for _, test := range errorTests {
t.Run(test.input, func(t *testing.T) {
_, err := expr.Eval(test.input, nil)
assert.Error(t, err)
assert.Contains(t, err.Error(), test.err)
program, err := expr.Compile(test.input)
if err != nil {
assert.Error(t, err)
assert.Contains(t, err.Error(), test.err)
} else {
_, err = expr.Run(program, nil)
assert.Error(t, err)
assert.Contains(t, err.Error(), test.err)
}
})
}
}
Expand Down
16 changes: 10 additions & 6 deletions compiler/compiler.go
Original file line number Diff line number Diff line change
Expand Up @@ -776,12 +776,16 @@ func (c *compiler) CallNode(node *ast.CallNode) {
}
c.compile(node.Callee)

isMethod, _, _ := checker.MethodIndex(c.config.Env, node.Callee)
if index, ok := checker.TypedFuncIndex(node.Callee.Type(), isMethod); ok {
c.emit(OpCallTyped, index)
return
} else if checker.IsFastFunc(node.Callee.Type(), isMethod) {
c.emit(OpCallFast, len(node.Arguments))
if c.config != nil {
isMethod, _, _ := checker.MethodIndex(c.config.Env, node.Callee)
if index, ok := checker.TypedFuncIndex(node.Callee.Type(), isMethod); ok {
c.emit(OpCallTyped, index)
return
} else if checker.IsFastFunc(node.Callee.Type(), isMethod) {
c.emit(OpCallFast, len(node.Arguments))
} else {
c.emit(OpCall, len(node.Arguments))
}
} else {
c.emit(OpCall, len(node.Arguments))
}
Expand Down
8 changes: 7 additions & 1 deletion expr.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"github.com/expr-lang/expr/conf"
"github.com/expr-lang/expr/file"
"github.com/expr-lang/expr/optimizer"
"github.com/expr-lang/expr/parser"
"github.com/expr-lang/expr/patcher"
"github.com/expr-lang/expr/vm"
)
Expand Down Expand Up @@ -240,7 +241,12 @@ func Eval(input string, env any) (any, error) {
return nil, fmt.Errorf("misused expr.Eval: second argument (env) should be passed without expr.Env")
}

program, err := Compile(input)
tree, err := parser.Parse(input)
if err != nil {
return nil, err
}

program, err := compiler.Compile(tree, nil)
if err != nil {
return nil, err
}
Expand Down
2 changes: 1 addition & 1 deletion expr_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1684,7 +1684,7 @@ func TestEval_exposed_error(t *testing.T) {

fileError, ok := err.(*file.Error)
require.True(t, ok, "error should be of type *file.Error")
require.Equal(t, "integer divide by zero (1:3)\n | 1 % 0\n | ..^", fileError.Error())
require.Equal(t, "runtime error: integer divide by zero (1:3)\n | 1 % 0\n | ..^", fileError.Error())
require.Equal(t, 2, fileError.Column)
require.Equal(t, 1, fileError.Line)
}
Expand Down
14 changes: 7 additions & 7 deletions parser/lexer/lexer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -311,13 +311,13 @@ func TestLex_location(t *testing.T) {
tokens, err := Lex(source)
require.NoError(t, err)
require.Equal(t, []Token{
{Location: file.Location{From: 0, To: 1}, Kind: "Number", Value: "1"},
{Location: file.Location{From: 1, To: 3}, Kind: "Operator", Value: ".."},
{Location: file.Location{From: 3, To: 4}, Kind: "Number", Value: "2"},
{Location: file.Location{From: 5, To: 6}, Kind: "Number", Value: "3"},
{Location: file.Location{From: 6, To: 8}, Kind: "Operator", Value: ".."},
{Location: file.Location{From: 8, To: 9}, Kind: "Number", Value: "4"},
{Location: file.Location{From: 8, To: 9}, Kind: "EOF", Value: ""},
{Location: file.Location{From: 0, To: 1}, Kind: Number, Value: "1"},
{Location: file.Location{From: 1, To: 3}, Kind: Operator, Value: ".."},
{Location: file.Location{From: 3, To: 4}, Kind: Number, Value: "2"},
{Location: file.Location{From: 5, To: 6}, Kind: Number, Value: "3"},
{Location: file.Location{From: 6, To: 8}, Kind: Operator, Value: ".."},
{Location: file.Location{From: 8, To: 9}, Kind: Number, Value: "4"},
{Location: file.Location{From: 8, To: 9}, Kind: EOF, Value: ""},
}, tokens)
}

Expand Down
18 changes: 13 additions & 5 deletions parser/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,13 @@ type parser struct {

func (p *parser) checkNodeLimit() error {
p.nodeCount++
if p.config == nil {
if p.nodeCount > conf.DefaultMaxNodes {
p.error("compilation failed: expression exceeds maximum allowed nodes")
return nil
}
return nil
}
if p.config.MaxNodes > 0 && p.nodeCount > p.config.MaxNodes {
p.error("compilation failed: expression exceeds maximum allowed nodes")
return nil
Expand Down Expand Up @@ -91,9 +98,7 @@ type Tree struct {
}

func Parse(input string) (*Tree, error) {
return ParseWithConfig(input, &conf.Config{
Disabled: map[string]bool{},
})
return ParseWithConfig(input, nil)
}

func ParseWithConfig(input string, config *conf.Config) (*Tree, error) {
Expand Down Expand Up @@ -515,7 +520,10 @@ func (p *parser) toFloatNode(number float64) Node {
func (p *parser) parseCall(token Token, arguments []Node, checkOverrides bool) Node {
var node Node

isOverridden := p.config.IsOverridden(token.Value)
isOverridden := false
if p.config != nil {
isOverridden = p.config.IsOverridden(token.Value)
}
isOverridden = isOverridden && checkOverrides

if b, ok := predicates[token.Value]; ok && !isOverridden {
Expand Down Expand Up @@ -562,7 +570,7 @@ func (p *parser) parseCall(token Token, arguments []Node, checkOverrides bool) N
if node == nil {
return nil
}
} else if _, ok := builtin.Index[token.Value]; ok && !p.config.Disabled[token.Value] && !isOverridden {
} else if _, ok := builtin.Index[token.Value]; ok && (p.config == nil || !p.config.Disabled[token.Value]) && !isOverridden {
node = p.createNode(&BuiltinNode{
Name: token.Value,
Arguments: p.parseArguments(arguments),
Expand Down
63 changes: 63 additions & 0 deletions test/bench/bench_call_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package bench_test

import (
"testing"

"github.com/expr-lang/expr"
"github.com/expr-lang/expr/internal/testify/require"
"github.com/expr-lang/expr/vm"
)

type Env struct {
Fn func() bool
}

func BenchmarkCall_callTyped(b *testing.B) {
code := `Fn()`

p, err := expr.Compile(code, expr.Env(Env{}))
require.NoError(b, err)
require.Equal(b, p.Bytecode[1], vm.OpCallTyped)

env := Env{
Fn: func() bool {
return true
},
}

var out any

b.ResetTimer()
for n := 0; n < b.N; n++ {
program, _ := expr.Compile(code, expr.Env(env))
out, err = vm.Run(program, env)
}
b.StopTimer()

require.NoError(b, err)
require.True(b, out.(bool))
}

func BenchmarkCall_eval(b *testing.B) {
code := `Fn()`

p, err := expr.Compile(code)
require.NoError(b, err)
require.Equal(b, p.Bytecode[1], vm.OpCall)

env := Env{
Fn: func() bool {
return true
},
}

var out any
b.ResetTimer()
for n := 0; n < b.N; n++ {
out, err = expr.Eval(code, env)
}
b.StopTimer()

require.NoError(b, err)
require.True(b, out.(bool))
}
Loading