Skip to content

Commit 58ef204

Browse files
s1nafjl
authored andcommitted
eth/tracers: optimize goja buffer conversion (ethereum#25156)
This changes the []byte <-> Uint8Array conversion to use an ArrayBuffer, avoiding inefficient copying of the slice data in Goja. Co-authored-by: Felix Lange <[email protected]>
1 parent 8d94030 commit 58ef204

File tree

1 file changed

+4
-10
lines changed

1 file changed

+4
-10
lines changed

eth/tracers/js/goja.go

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -55,11 +55,7 @@ type fromBufFn = func(vm *goja.Runtime, buf goja.Value, allowString bool) ([]byt
5555

5656
func toBuf(vm *goja.Runtime, bufType goja.Value, val []byte) (goja.Value, error) {
5757
// bufType is usually Uint8Array. This is equivalent to `new Uint8Array(val)` in JS.
58-
res, err := vm.New(bufType, vm.ToValue(val))
59-
if err != nil {
60-
return nil, err
61-
}
62-
return vm.ToValue(res), nil
58+
return vm.New(bufType, vm.ToValue(vm.NewArrayBuffer(val)))
6359
}
6460

6561
func fromBuf(vm *goja.Runtime, bufType goja.Value, buf goja.Value, allowString bool) ([]byte, error) {
@@ -70,6 +66,7 @@ func fromBuf(vm *goja.Runtime, bufType goja.Value, buf goja.Value, allowString b
7066
break
7167
}
7268
return common.FromHex(obj.String()), nil
69+
7370
case "Array":
7471
var b []byte
7572
if err := vm.ExportTo(buf, &b); err != nil {
@@ -81,10 +78,7 @@ func fromBuf(vm *goja.Runtime, bufType goja.Value, buf goja.Value, allowString b
8178
if !obj.Get("constructor").SameAs(bufType) {
8279
break
8380
}
84-
var b []byte
85-
if err := vm.ExportTo(buf, &b); err != nil {
86-
return nil, err
87-
}
81+
b := obj.Get("buffer").Export().(goja.ArrayBuffer).Bytes()
8882
return b, nil
8983
}
9084
return nil, fmt.Errorf("invalid buffer type")
@@ -765,7 +759,7 @@ func (co *contractObj) GetValue() goja.Value {
765759
}
766760

767761
func (co *contractObj) GetInput() goja.Value {
768-
input := co.contract.Input
762+
input := common.CopyBytes(co.contract.Input)
769763
res, err := co.toBuf(co.vm, input)
770764
if err != nil {
771765
co.vm.Interrupt(err)

0 commit comments

Comments
 (0)