Skip to content

Commit 26e1470

Browse files
committed
eth/tracers/js: avoid compiling js bigint when not needed (#30640)
While looking at some mem profiles from `evm` runs, I noticed that `goja` compilation of the bigint library was present. The bigint library compilation happens in a package `init`, whenever the package `eth/tracers/js` is loaded. This PR changes it to load lazily when needed. It becomes slightly faster with this change, and slightly less alloc:y. Non-scientific benchmark with 100 executions: ``` time for i in {1..100}; do ./evm --code 6040 run; done; ``` current `master`: ``` real 0m6.634s user 0m5.213s sys 0m2.277s ``` Without compiling bigint ``` real 0m5.802s user 0m4.191s sys 0m1.965s ```
1 parent db03e01 commit 26e1470

File tree

1 file changed

+13
-4
lines changed

1 file changed

+13
-4
lines changed

eth/tracers/js/goja.go

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import (
2222
"fmt"
2323
"math/big"
2424
"slices"
25+
"sync"
2526

2627
"github.com/dop251/goja"
2728
"github.com/ethereum/go-ethereum/core/tracing"
@@ -59,9 +60,17 @@ func init() {
5960
tracers.DefaultDirectory.RegisterJSEval(newJsTracer)
6061
}
6162

62-
// bigIntProgram is compiled once and the exported function mostly invoked to convert
63-
// hex strings into big ints.
64-
var bigIntProgram = goja.MustCompile("bigInt", bigIntegerJS, false)
63+
var compiledBigInt *goja.Program
64+
var compileOnce sync.Once
65+
66+
// getBigIntProgram compiles the bigint library, if needed, and returns the compiled
67+
// goja program.
68+
func getBigIntProgram() *goja.Program {
69+
compileOnce.Do(func() {
70+
compiledBigInt = goja.MustCompile("bigInt", bigIntegerJS, false)
71+
})
72+
return compiledBigInt
73+
}
6574

6675
type toBigFn = func(vm *goja.Runtime, val string) (goja.Value, error)
6776
type toBufFn = func(vm *goja.Runtime, val []byte) (goja.Value, error)
@@ -567,7 +576,7 @@ func (t *jsTracer) setBuiltinFunctions() {
567576
func (t *jsTracer) setTypeConverters() error {
568577
// Inject bigint logic.
569578
// TODO: To be replaced after goja adds support for native JS bigint.
570-
toBigCode, err := t.vm.RunProgram(bigIntProgram)
579+
toBigCode, err := t.vm.RunProgram(getBigIntProgram())
571580
if err != nil {
572581
return err
573582
}

0 commit comments

Comments
 (0)