Skip to content

Call VM functions directly instead of through reflect.Call - #374

Merged
mattn merged 1 commit into
masterfrom
perf-direct-vmfunc-call
Jul 9, 2026
Merged

Call VM functions directly instead of through reflect.Call#374
mattn merged 1 commit into
masterfrom
perf-direct-vmfunc-call

Conversation

@mattn

@mattn mattn commented Jul 9, 2026

Copy link
Copy Markdown
Owner

Script-defined functions were created with reflect.MakeFunc and called with reflect.Value.Call, paying for argument frame allocation, double reflect.Value wrapping of every argument and return value, and the callReflect trampoline on every call.

Non-variadic functions with up to 4 parameters are now created as ordinary Go closures with the same structural signature that MakeFunc produced, and callExpr type-asserts to that signature and calls the closure directly. checkIfRunVMFunction, reflect.Call from Go interop, and error semantics are unchanged; variadic functions and 5+ parameters keep the MakeFunc path.

Benchmarks (AMD Ryzen 7 7735HS):

benchmark before after
Fibonacci 4.27ms, 31579 allocs 1.63ms, 9874 allocs
FuncCall 3.50us, 29 allocs 1.31us, 13 allocs
FibVM (fib 24) 3.77s 1.44s

~2.6x faster on function-call heavy scripts, -69% allocations.

@mattn
mattn merged commit 6949fab into master Jul 9, 2026
12 checks passed
@mattn
mattn deleted the perf-direct-vmfunc-call branch July 10, 2026 00:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant