stackitem: avoid going through Value() in serialization

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)
This commit is contained in:
Roman Khimov 2021-08-20 22:37:06 +03:00
parent 1a733ca456
commit d3198c3082
2 changed files with 17 additions and 8 deletions

View file

@ -95,14 +95,12 @@ func (w *serContext) serialize(item Item) error {
switch t := item.(type) { switch t := item.(type) {
case *ByteArray: case *ByteArray:
w.data = append(w.data, byte(ByteArrayT)) w.data = append(w.data, byte(ByteArrayT))
data := t.Value().([]byte) w.appendVarUint(uint64(len(*t)))
w.appendVarUint(uint64(len(data))) w.data = append(w.data, *t...)
w.data = append(w.data, data...)
case *Buffer: case *Buffer:
w.data = append(w.data, byte(BufferT)) w.data = append(w.data, byte(BufferT))
data := t.Value().([]byte) w.appendVarUint(uint64(len(*t)))
w.appendVarUint(uint64(len(data))) w.data = append(w.data, *t...)
w.data = append(w.data, data...)
case Bool: case Bool:
w.data = append(w.data, byte(BooleanT)) w.data = append(w.data, byte(BooleanT))
if t { if t {
@ -112,10 +110,9 @@ func (w *serContext) serialize(item Item) error {
} }
case *BigInteger: case *BigInteger:
w.data = append(w.data, byte(IntegerT)) w.data = append(w.data, byte(IntegerT))
v := t.Value().(*big.Int)
ln := len(w.data) ln := len(w.data)
w.data = append(w.data, 0) 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[ln] = byte(len(data))
w.data = append(w.data, data...) w.data = append(w.data, data...)
case *Interop: case *Interop:

View file

@ -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()
}
}
}