mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-05-24 08:49:29 +00:00
accounts/abi: improve unpack performance (#31387)
Slightly improves performance of abi.Unpack ``` Before BenchmarkUnpack/0-14 5965714 210.9 ns/op 280 B/op 5 allocs/op BenchmarkUnpack/1-14 2148283 569.7 ns/op 688 B/op 16 allocs/op After: BenchmarkUnpack/0-14 7693365 151.2 ns/op 136 B/op 4 allocs/op BenchmarkUnpack/1-14 2261294 508.9 ns/op 544 B/op 15 allocs/op ``` replaces https://github.com/ethereum/go-ethereum/pull/31292 since I was unable to push to your branch @Exca-DK --------- Co-authored-by: Exca-DK <dawidk.info@gmail.com>
This commit is contained in:
parent
f9f1172d59
commit
e5758c606a
2 changed files with 60 additions and 13 deletions
|
|
@ -77,18 +77,18 @@ func (arguments Arguments) isTuple() bool {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Unpack performs the operation hexdata -> Go format.
|
// Unpack performs the operation hexdata -> Go format.
|
||||||
func (arguments Arguments) Unpack(data []byte) ([]interface{}, error) {
|
func (arguments Arguments) Unpack(data []byte) ([]any, error) {
|
||||||
if len(data) == 0 {
|
if len(data) == 0 {
|
||||||
if len(arguments.NonIndexed()) != 0 {
|
if len(arguments.NonIndexed()) != 0 {
|
||||||
return nil, errors.New("abi: attempting to unmarshal an empty string while arguments are expected")
|
return nil, errors.New("abi: attempting to unmarshal an empty string while arguments are expected")
|
||||||
}
|
}
|
||||||
return make([]interface{}, 0), nil
|
return make([]any, 0), nil
|
||||||
}
|
}
|
||||||
return arguments.UnpackValues(data)
|
return arguments.UnpackValues(data)
|
||||||
}
|
}
|
||||||
|
|
||||||
// UnpackIntoMap performs the operation hexdata -> mapping of argument name to argument value.
|
// UnpackIntoMap performs the operation hexdata -> mapping of argument name to argument value.
|
||||||
func (arguments Arguments) UnpackIntoMap(v map[string]interface{}, data []byte) error {
|
func (arguments Arguments) UnpackIntoMap(v map[string]any, data []byte) error {
|
||||||
// Make sure map is not nil
|
// Make sure map is not nil
|
||||||
if v == nil {
|
if v == nil {
|
||||||
return errors.New("abi: cannot unpack into a nil map")
|
return errors.New("abi: cannot unpack into a nil map")
|
||||||
|
|
@ -110,7 +110,7 @@ func (arguments Arguments) UnpackIntoMap(v map[string]interface{}, data []byte)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Copy performs the operation go format -> provided struct.
|
// Copy performs the operation go format -> provided struct.
|
||||||
func (arguments Arguments) Copy(v interface{}, values []interface{}) error {
|
func (arguments Arguments) Copy(v any, values []any) error {
|
||||||
// make sure the passed value is arguments pointer
|
// make sure the passed value is arguments pointer
|
||||||
if reflect.Ptr != reflect.ValueOf(v).Kind() {
|
if reflect.Ptr != reflect.ValueOf(v).Kind() {
|
||||||
return fmt.Errorf("abi: Unpack(non-pointer %T)", v)
|
return fmt.Errorf("abi: Unpack(non-pointer %T)", v)
|
||||||
|
|
@ -128,7 +128,7 @@ func (arguments Arguments) Copy(v interface{}, values []interface{}) error {
|
||||||
}
|
}
|
||||||
|
|
||||||
// copyAtomic copies ( hexdata -> go ) a single value
|
// copyAtomic copies ( hexdata -> go ) a single value
|
||||||
func (arguments Arguments) copyAtomic(v interface{}, marshalledValues interface{}) error {
|
func (arguments Arguments) copyAtomic(v any, marshalledValues any) error {
|
||||||
dst := reflect.ValueOf(v).Elem()
|
dst := reflect.ValueOf(v).Elem()
|
||||||
src := reflect.ValueOf(marshalledValues)
|
src := reflect.ValueOf(marshalledValues)
|
||||||
|
|
||||||
|
|
@ -139,7 +139,7 @@ func (arguments Arguments) copyAtomic(v interface{}, marshalledValues interface{
|
||||||
}
|
}
|
||||||
|
|
||||||
// copyTuple copies a batch of values from marshalledValues to v.
|
// copyTuple copies a batch of values from marshalledValues to v.
|
||||||
func (arguments Arguments) copyTuple(v interface{}, marshalledValues []interface{}) error {
|
func (arguments Arguments) copyTuple(v any, marshalledValues []any) error {
|
||||||
value := reflect.ValueOf(v).Elem()
|
value := reflect.ValueOf(v).Elem()
|
||||||
nonIndexedArgs := arguments.NonIndexed()
|
nonIndexedArgs := arguments.NonIndexed()
|
||||||
|
|
||||||
|
|
@ -181,11 +181,17 @@ func (arguments Arguments) copyTuple(v interface{}, marshalledValues []interface
|
||||||
// UnpackValues can be used to unpack ABI-encoded hexdata according to the ABI-specification,
|
// UnpackValues can be used to unpack ABI-encoded hexdata according to the ABI-specification,
|
||||||
// without supplying a struct to unpack into. Instead, this method returns a list containing the
|
// without supplying a struct to unpack into. Instead, this method returns a list containing the
|
||||||
// values. An atomic argument will be a list with one element.
|
// values. An atomic argument will be a list with one element.
|
||||||
func (arguments Arguments) UnpackValues(data []byte) ([]interface{}, error) {
|
func (arguments Arguments) UnpackValues(data []byte) ([]any, error) {
|
||||||
nonIndexedArgs := arguments.NonIndexed()
|
var (
|
||||||
retval := make([]interface{}, 0, len(nonIndexedArgs))
|
retval = make([]any, 0)
|
||||||
virtualArgs := 0
|
virtualArgs = 0
|
||||||
for index, arg := range nonIndexedArgs {
|
index = 0
|
||||||
|
)
|
||||||
|
|
||||||
|
for _, arg := range arguments {
|
||||||
|
if arg.Indexed {
|
||||||
|
continue
|
||||||
|
}
|
||||||
marshalledValue, err := toGoType((index+virtualArgs)*32, arg.Type, data)
|
marshalledValue, err := toGoType((index+virtualArgs)*32, arg.Type, data)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
|
|
@ -208,18 +214,19 @@ func (arguments Arguments) UnpackValues(data []byte) ([]interface{}, error) {
|
||||||
virtualArgs += getTypeSize(arg.Type)/32 - 1
|
virtualArgs += getTypeSize(arg.Type)/32 - 1
|
||||||
}
|
}
|
||||||
retval = append(retval, marshalledValue)
|
retval = append(retval, marshalledValue)
|
||||||
|
index++
|
||||||
}
|
}
|
||||||
return retval, nil
|
return retval, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// PackValues performs the operation Go format -> Hexdata.
|
// PackValues performs the operation Go format -> Hexdata.
|
||||||
// It is the semantic opposite of UnpackValues.
|
// It is the semantic opposite of UnpackValues.
|
||||||
func (arguments Arguments) PackValues(args []interface{}) ([]byte, error) {
|
func (arguments Arguments) PackValues(args []any) ([]byte, error) {
|
||||||
return arguments.Pack(args...)
|
return arguments.Pack(args...)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Pack performs the operation Go format -> Hexdata.
|
// Pack performs the operation Go format -> Hexdata.
|
||||||
func (arguments Arguments) Pack(args ...interface{}) ([]byte, error) {
|
func (arguments Arguments) Pack(args ...any) ([]byte, error) {
|
||||||
// Make sure arguments match up and pack them
|
// Make sure arguments match up and pack them
|
||||||
abiArgs := arguments
|
abiArgs := arguments
|
||||||
if len(args) != len(abiArgs) {
|
if len(args) != len(abiArgs) {
|
||||||
|
|
|
||||||
|
|
@ -31,6 +31,46 @@ import (
|
||||||
"github.com/stretchr/testify/require"
|
"github.com/stretchr/testify/require"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
func BenchmarkUnpack(b *testing.B) {
|
||||||
|
testCases := []struct {
|
||||||
|
def string
|
||||||
|
packed string
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
def: `[{"type": "uint32"}]`,
|
||||||
|
packed: "0000000000000000000000000000000000000000000000000000000000000001",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
def: `[{"type": "uint32[]"}]`,
|
||||||
|
packed: "0000000000000000000000000000000000000000000000000000000000000020" +
|
||||||
|
"0000000000000000000000000000000000000000000000000000000000000002" +
|
||||||
|
"0000000000000000000000000000000000000000000000000000000000000001" +
|
||||||
|
"0000000000000000000000000000000000000000000000000000000000000002",
|
||||||
|
},
|
||||||
|
}
|
||||||
|
for i, test := range testCases {
|
||||||
|
b.Run(strconv.Itoa(i), func(b *testing.B) {
|
||||||
|
def := fmt.Sprintf(`[{ "name" : "method", "type": "function", "outputs": %s}]`, test.def)
|
||||||
|
abi, err := JSON(strings.NewReader(def))
|
||||||
|
if err != nil {
|
||||||
|
b.Fatalf("invalid ABI definition %s: %v", def, err)
|
||||||
|
}
|
||||||
|
encb, err := hex.DecodeString(test.packed)
|
||||||
|
if err != nil {
|
||||||
|
b.Fatalf("invalid hex %s: %v", test.packed, err)
|
||||||
|
}
|
||||||
|
|
||||||
|
b.ResetTimer()
|
||||||
|
|
||||||
|
var result any
|
||||||
|
for range b.N {
|
||||||
|
result, _ = abi.Unpack("method", encb)
|
||||||
|
}
|
||||||
|
_ = result
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// TestUnpack tests the general pack/unpack tests in packing_test.go
|
// TestUnpack tests the general pack/unpack tests in packing_test.go
|
||||||
func TestUnpack(t *testing.T) {
|
func TestUnpack(t *testing.T) {
|
||||||
t.Parallel()
|
t.Parallel()
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue