From d3198c30820e8919bfb12071b7311f7b8577fee5 Mon Sep 17 00:00:00 2001 From: Roman Khimov Date: Fri, 20 Aug 2021 22:37:06 +0300 Subject: [PATCH] stackitem: avoid going through Value() in serialization MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Doesn't change much, but still simpler. name old time/op new time/op delta SerializeSimple-8 452ns ±10% 435ns ± 4% ~ (p=0.356 n=10+9) name old alloc/op new alloc/op delta SerializeSimple-8 432B ± 0% 432B ± 0% ~ (all equal) name old allocs/op new allocs/op delta SerializeSimple-8 7.00 ± 0% 7.00 ± 0% ~ (all equal) --- pkg/vm/stackitem/serialization.go | 13 +++++-------- pkg/vm/stackitem/serialization_test.go | 12 ++++++++++++ 2 files changed, 17 insertions(+), 8 deletions(-) diff --git a/pkg/vm/stackitem/serialization.go b/pkg/vm/stackitem/serialization.go index 8db760e37..bda4fbc80 100644 --- a/pkg/vm/stackitem/serialization.go +++ b/pkg/vm/stackitem/serialization.go @@ -95,14 +95,12 @@ func (w *serContext) serialize(item Item) error { switch t := item.(type) { case *ByteArray: w.data = append(w.data, byte(ByteArrayT)) - data := t.Value().([]byte) - w.appendVarUint(uint64(len(data))) - w.data = append(w.data, data...) + w.appendVarUint(uint64(len(*t))) + w.data = append(w.data, *t...) case *Buffer: w.data = append(w.data, byte(BufferT)) - data := t.Value().([]byte) - w.appendVarUint(uint64(len(data))) - w.data = append(w.data, data...) + w.appendVarUint(uint64(len(*t))) + w.data = append(w.data, *t...) case Bool: w.data = append(w.data, byte(BooleanT)) if t { @@ -112,10 +110,9 @@ func (w *serContext) serialize(item Item) error { } case *BigInteger: w.data = append(w.data, byte(IntegerT)) - v := t.Value().(*big.Int) ln := len(w.data) w.data = append(w.data, 0) - data := bigint.ToPreallocatedBytes(v, w.data[len(w.data):]) + data := bigint.ToPreallocatedBytes((*big.Int)(t), w.data[len(w.data):]) w.data[ln] = byte(len(data)) w.data = append(w.data, data...) case *Interop: diff --git a/pkg/vm/stackitem/serialization_test.go b/pkg/vm/stackitem/serialization_test.go index 139fbcbcf..9b496dd52 100644 --- a/pkg/vm/stackitem/serialization_test.go +++ b/pkg/vm/stackitem/serialization_test.go @@ -207,3 +207,15 @@ func BenchmarkEncodeBinary(b *testing.B) { } } } + +func BenchmarkSerializeSimple(b *testing.B) { + s := NewStruct(nil) + s.Append(Make(100500)) + s.Append(Make("1aada0032aba1ef6d1f0")) // Mimicking uint160. + for i := 0; i < b.N; i++ { + _, err := Serialize(s) + if err != nil { + b.FailNow() + } + } +}