-
-
Notifications
You must be signed in to change notification settings - Fork 462
VM performance improvements in function calls #832
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
Open
diegommm
wants to merge
9
commits into
expr-lang:master
Choose a base branch
from
diegommm:vm-and-runtime-improvements
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
cf666b4
optimize vm allocation of function arguments
diegommm 60d09ef
make estimation faster by using a table
diegommm d030d1e
add benchmarks
diegommm 39e88fc
avoid all allocations if no arguments are needed
diegommm 64d5d1c
simplify code and gain 2% speed
diegommm 2fb1f53
add safety limit on preallocation
diegommm a3d86ee
Merge branch 'master' into vm-and-runtime-improvements
antonmedv 7ea86c9
Merge branch 'master' into vm-and-runtime-improvements
diegommm 8841c48
Merge branch 'master' into vm-and-runtime-improvements
antonmedv File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
package vm_test | ||
|
||
import ( | ||
"runtime" | ||
"testing" | ||
|
||
"github.com/expr-lang/expr" | ||
"github.com/expr-lang/expr/checker" | ||
"github.com/expr-lang/expr/compiler" | ||
"github.com/expr-lang/expr/conf" | ||
"github.com/expr-lang/expr/vm" | ||
) | ||
|
||
func BenchmarkVM(b *testing.B) { | ||
cases := []struct { | ||
name, input string | ||
}{ | ||
{"function calls", ` | ||
func( | ||
func( | ||
func(func(a, 'a', 1, nil), func(a, 'a', 1, nil), func(a, 'a', 1, nil)), | ||
func(func(a, 'a', 1, nil), func(a, 'a', 1, nil), func(a, 'a', 1, nil)), | ||
func(func(a, 'a', 1, nil), func(a, 'a', 1, nil), func(a, 'a', 1, nil)), | ||
), | ||
func( | ||
func(func(a, 'a', 1, nil), func(a, 'a', 1, nil), func(a, 'a', 1, nil)), | ||
func(func(a, 'a', 1, nil), func(a, 'a', 1, nil), func(a, 'a', 1, nil)), | ||
func(func(a, 'a', 1, nil), func(a, 'a', 1, nil), func(a, 'a', 1, nil)), | ||
), | ||
func( | ||
func(func(a, 'a', 1, nil), func(a, 'a', 1, nil), func(a, 'a', 1, nil)), | ||
func(func(a, 'a', 1, nil), func(a, 'a', 1, nil), func(a, 'a', 1, nil)), | ||
func(func(a, 'a', 1, nil), func(a, 'a', 1, nil), func(a, 'a', 1, nil)), | ||
) | ||
) | ||
`}, | ||
} | ||
|
||
a := new(recursive) | ||
for i, b := 0, a; i < 40*4; i++ { | ||
b.Inner = new(recursive) | ||
b = b.Inner | ||
} | ||
|
||
f := func(params ...any) (any, error) { return nil, nil } | ||
env := map[string]any{ | ||
"a": a, | ||
"b": true, | ||
"func": f, | ||
} | ||
config := conf.New(env) | ||
expr.Function("func", f, f)(config) | ||
config.Check() | ||
|
||
for _, c := range cases { | ||
tree, err := checker.ParseCheck(c.input, config) | ||
if err != nil { | ||
b.Fatal(c.input, "parse and check", err) | ||
} | ||
prog, err := compiler.Compile(tree, config) | ||
if err != nil { | ||
b.Fatal(c.input, "compile", err) | ||
} | ||
//b.Logf("disassembled:\n%s", prog.Disassemble()) | ||
//b.FailNow() | ||
runtime.GC() | ||
|
||
var vm vm.VM | ||
b.Run("name="+c.name, func(b *testing.B) { | ||
for i := 0; i < b.N; i++ { | ||
_, err = vm.Run(prog, env) | ||
} | ||
}) | ||
if err != nil { | ||
b.Fatal(err) | ||
} | ||
} | ||
} | ||
|
||
type recursive struct { | ||
Inner *recursive `expr:"a"` | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I initially used a
switch
inestimateFnArgsCount
but then tried with this table and got a 4% improvement in speed.However, you can see that I am making an array with 56 elements but I'm using only 6. I preferred it this way because I think the code looks clearer. But if you prefer to make the table use exactly the number of items it needs then we would just need to make the following change (you can just apply this suggestion as it is here if you want, I just tested this exact code and it is also correctly formatted with spaces :) ):