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
```
release/1.14
Martin HS 1 month ago
parent db03e01577
commit 26e1470393
  1. 17
      eth/tracers/js/goja.go

@ -22,6 +22,7 @@ import (
"fmt" "fmt"
"math/big" "math/big"
"slices" "slices"
"sync"
"github.com/dop251/goja" "github.com/dop251/goja"
"github.com/ethereum/go-ethereum/core/tracing" "github.com/ethereum/go-ethereum/core/tracing"
@ -59,9 +60,17 @@ func init() {
tracers.DefaultDirectory.RegisterJSEval(newJsTracer) tracers.DefaultDirectory.RegisterJSEval(newJsTracer)
} }
// bigIntProgram is compiled once and the exported function mostly invoked to convert var compiledBigInt *goja.Program
// hex strings into big ints. var compileOnce sync.Once
var bigIntProgram = goja.MustCompile("bigInt", bigIntegerJS, false)
// getBigIntProgram compiles the bigint library, if needed, and returns the compiled
// goja program.
func getBigIntProgram() *goja.Program {
compileOnce.Do(func() {
compiledBigInt = goja.MustCompile("bigInt", bigIntegerJS, false)
})
return compiledBigInt
}
type toBigFn = func(vm *goja.Runtime, val string) (goja.Value, error) type toBigFn = func(vm *goja.Runtime, val string) (goja.Value, error)
type toBufFn = func(vm *goja.Runtime, val []byte) (goja.Value, error) type toBufFn = func(vm *goja.Runtime, val []byte) (goja.Value, error)
@ -567,7 +576,7 @@ func (t *jsTracer) setBuiltinFunctions() {
func (t *jsTracer) setTypeConverters() error { func (t *jsTracer) setTypeConverters() error {
// Inject bigint logic. // Inject bigint logic.
// TODO: To be replaced after goja adds support for native JS bigint. // TODO: To be replaced after goja adds support for native JS bigint.
toBigCode, err := t.vm.RunProgram(bigIntProgram) toBigCode, err := t.vm.RunProgram(getBigIntProgram())
if err != nil { if err != nil {
return err return err
} }

Loading…
Cancel
Save