Skip to content

Conversation

omerfirmak
Copy link
Contributor

@omerfirmak omerfirmak commented Jul 21, 2025

this is a PoC on how a code generator approach might work.

benchmarks suggest modest improvement compared to my previous attempts. I think the biggest problem is that gas/memory/exec functions are still written in isolation and only naively concatenated together to generate a single "function". This prohibits certain type of optimizations to be applied. Mainly the type that requires these functions to share some state. To give an example, all three functions still require to read from stack separately, which is just an artificial requirement that we introduce thru the choice of how the interpreter loop was originally implemented.

generated loop: https://gist.github.com/omerfirmak/53bde8232937fac56da1f241480f2327

omer@omer-ThinkPad-E14-Gen-3:~/Documents/go-ethereum$ benchstat old.txt new.txt 
goos: linux
goarch: amd64
pkg: github.com/ethereum/go-ethereum/core/vm/runtime
cpu: AMD Ryzen 7 5700U with Radeon Graphics         
                                       │   old.txt   │               new.txt               │
                                       │   sec/op    │   sec/op     vs base                │
SimpleLoop/staticcall-identity-100M-16   284.4m ± 1%   276.5m ± 1%   -2.76% (p=0.000 n=10)
SimpleLoop/call-identity-100M-16         345.2m ± 1%   322.6m ± 0%   -6.55% (p=0.000 n=10)
SimpleLoop/loop-100M-16                  286.2m ± 1%   279.5m ± 1%   -2.34% (p=0.000 n=10)
SimpleLoop/loop2-100M-16                 286.5m ± 1%   254.6m ± 2%  -11.15% (p=0.000 n=10)
SimpleLoop/call-nonexist-100M-16         662.2m ± 0%   658.6m ± 0%   -0.54% (p=0.000 n=10)
SimpleLoop/call-EOA-100M-16              287.4m ± 1%   256.3m ± 0%  -10.82% (p=0.000 n=10)
SimpleLoop/call-reverting-100M-16        547.5m ± 1%   470.3m ± 1%  -14.10% (p=0.000 n=10)
geomean                                  363.5m        338.0m        -7.02%

                                       │   old.txt    │               new.txt                │
                                       │     B/op     │     B/op      vs base                │
SimpleLoop/staticcall-identity-100M-16   67.95Mi ± 0%   67.95Mi ± 0%        ~ (p=0.134 n=10)
SimpleLoop/call-identity-100M-16         66.58Mi ± 0%   66.58Mi ± 0%   +0.00% (p=0.000 n=10)
SimpleLoop/loop-100M-16                  5.175Ki ± 2%   5.242Ki ± 0%        ~ (p=0.798 n=10)
SimpleLoop/loop2-100M-16                 5.273Ki ± 0%   5.242Ki ± 4%   -0.59% (p=0.000 n=10)
SimpleLoop/call-nonexist-100M-16         119.6Mi ± 0%   119.6Mi ± 0%   -0.00% (p=0.035 n=10)
SimpleLoop/call-EOA-100M-16              22.79Mi ± 0%   22.79Mi ± 0%        ~ (p=1.000 n=10)
SimpleLoop/call-reverting-100M-16        163.8Mi ± 0%   142.0Mi ± 0%  -13.33% (p=0.000 n=10)
geomean                                  4.725Mi        4.634Mi        -1.93%

                                       │   old.txt   │               new.txt               │
                                       │  allocs/op  │  allocs/op   vs base                │
SimpleLoop/staticcall-identity-100M-16   2.740M ± 0%   2.740M ± 0%   -0.00% (p=0.000 n=10)
SimpleLoop/call-identity-100M-16         2.685M ± 0%   2.685M ± 0%   -0.00% (p=0.001 n=10)
SimpleLoop/loop-100M-16                   40.50 ± 1%    39.00 ± 0%   -3.70% (p=0.000 n=10)
SimpleLoop/loop2-100M-16                  41.00 ± 0%    39.00 ± 3%   -4.88% (p=0.000 n=10)
SimpleLoop/call-nonexist-100M-16         2.985M ± 0%   2.985M ± 0%   -0.00% (p=0.000 n=10)
SimpleLoop/call-EOA-100M-16              746.3k ± 0%   746.3k ± 0%   -0.00% (p=0.000 n=10)
SimpleLoop/call-reverting-100M-16        2.858M ± 0%   1.429M ± 0%  -49.99% (p=0.000 n=10)
geomean                                  96.47k        86.29k       -10.55%

unfortunately additional potential of exploration with a codegenerator is rather limited as well, due to Go not supporting most of the features (tco, computed goto etc) that we need to improve the interpreter loop.

@omerfirmak omerfirmak closed this Aug 14, 2025
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