io: add type-specific read/write methods

This seriously improves the serialization/deserialization performance for
several reasons:
 * no time spent in `binary` reflection
 * no memory allocations being made on every read/write
 * uses fast ReadBytes everywhere it's appropriate

It also makes Fixed8 Serializable just for convenience.
This commit is contained in:
Roman Khimov 2019-12-12 18:52:23 +03:00
parent 89d7f6d26e
commit 54d888ba70
43 changed files with 441 additions and 205 deletions

View file

@ -47,7 +47,7 @@ func serializeItemTo(item StackItem, w *io.BinWriter, seen map[StackItem]bool) {
w.WriteVarBytes(t.value)
case *BoolItem:
w.WriteBytes([]byte{byte(booleanT)})
w.WriteLE(t.value)
w.WriteBool(t.value)
case *BigIntegerItem:
w.WriteBytes([]byte{byte(integerT)})
w.WriteVarBytes(t.Bytes())
@ -94,8 +94,7 @@ func deserializeItem(data []byte) (StackItem, error) {
// as a function because StackItem itself is an interface. Caveat: always check
// reader's error value before using the returned StackItem.
func DecodeBinaryStackItem(r *io.BinReader) StackItem {
var t byte
r.ReadLE(&t)
var t = r.ReadByte()
if r.Err != nil {
return nil
}
@ -105,8 +104,7 @@ func DecodeBinaryStackItem(r *io.BinReader) StackItem {
data := r.ReadVarBytes()
return NewByteArrayItem(data)
case booleanT:
var b bool
r.ReadLE(&b)
var b = r.ReadBool()
return NewBoolItem(b)
case integerT:
data := r.ReadVarBytes()