Save alloc

This commit is contained in:
Exca-DK 2025-03-01 02:09:32 +01:00
parent 4038c59845
commit c2d2404980
2 changed files with 53 additions and 3 deletions

View file

@ -182,10 +182,20 @@ func (arguments Arguments) copyTuple(v interface{}, marshalledValues []interface
// 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.
func (arguments Arguments) UnpackValues(data []byte) ([]interface{}, error) {
nonIndexedArgs := arguments.NonIndexed()
retval := make([]interface{}, 0, len(nonIndexedArgs))
size := 0
for _, arg := range arguments {
if arg.Indexed {
continue
}
size++
}
retval := make([]interface{}, 0, size)
virtualArgs := 0
for index, arg := range nonIndexedArgs {
for index, arg := range arguments {
if arg.Indexed {
continue
}
marshalledValue, err := toGoType((index+virtualArgs)*32, arg.Type, data)
if err != nil {
return nil, err

View file

@ -31,6 +31,46 @@ import (
"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
func TestUnpack(t *testing.T) {
t.Parallel()