Skip to content

feat: use go-abi to replace go-ethereum abi library #789

@yihuang

Description

@yihuang

The go-ethereum/accounts/abi library use runtime reflection for convenience, but it's inefficient and introduce many runtime error and panic conditions (see #269 for example).

go-abi is developed just for precompile usage, it use code generation and generate statically typed structs and encoding/decoding routines. It's both safer to use and magnitudes faster than go-ethereum.

BTW, solidity's abi.encode/abi.decode functions are not implemented with go-ethereum/accounts/abi, they are also special treated by solc to generate code at compile time.

Benchmark

Benchmark shows that go-abi is magnitudes faster, and do exactly 1 allocation per operation.

$ go test -bench=. -benchmem -benchtime=5s ./tests/...
eth length 1028
goos: darwin
goarch: arm64
pkg: github.com/yihuang/go-abi/tests
cpu: Apple M3 Max
BenchmarkGoABI_ComplexDynamicTuples-16                           	14653149	      431.2 ns/op	   3072 B/op	      1 allocs/op
BenchmarkGoABI_NestedDynamicArrays-16                            	20507278	      285.6 ns/op	   1280 B/op	      1 allocs/op
BenchmarkGoABI_MixedTypes-16                                     	44948070	      134.7 ns/op	    896 B/op	      1 allocs/op
BenchmarkGoEthereum_ComplexDynamicTuples-16                      	 424966	    13945 ns/op	  49385 B/op	    372 allocs/op
BenchmarkGoEthereum_NestedDynamicArrays-16                       	1388450	     4330 ns/op	  13504 B/op	    151 allocs/op
BenchmarkGoEthereum_MixedTypes-16                                	1391089	     4302 ns/op	   9296 B/op	    116 allocs/op
BenchmarkGoABI_EncodeOnly_ComplexDynamicTuples-16                	13514184	      442.9 ns/op	   3072 B/op	      1 allocs/op
BenchmarkGoABI_EncodeTo_ComplexDynamicTuples-16                  	41147829	      147.8 ns/op	      0 B/op	      0 allocs/op
BenchmarkGoABI_MemoryAllocations_ComplexDynamicTuples-16         	13679475	      442.8 ns/op	   3072 B/op	      1 allocs/op
BenchmarkGoEthereum_MemoryAllocations_ComplexDynamicTuples-16    	 379189	    14353 ns/op	  49385 B/op	    372 allocs/op
PASS
ok  	github.com/yihuang/go-abi/tests	71.209s

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions