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

@ -27,17 +27,13 @@ func (tx *PublishTX) DecodeBinary(br *io.BinReader) {
lenParams := br.ReadVarUint()
tx.ParamList = make([]smartcontract.ParamType, lenParams)
for i := 0; i < int(lenParams); i++ {
var ptype uint8
br.ReadLE(&ptype)
tx.ParamList[i] = smartcontract.ParamType(ptype)
tx.ParamList[i] = smartcontract.ParamType(br.ReadByte())
}
var rtype uint8
br.ReadLE(&rtype)
tx.ReturnType = smartcontract.ParamType(rtype)
tx.ReturnType = smartcontract.ParamType(br.ReadByte())
if tx.Version >= 1 {
br.ReadLE(&tx.NeedStorage)
tx.NeedStorage = br.ReadBool()
} else {
tx.NeedStorage = false
}
@ -54,11 +50,11 @@ func (tx *PublishTX) EncodeBinary(bw *io.BinWriter) {
bw.WriteVarBytes(tx.Script)
bw.WriteVarUint(uint64(len(tx.ParamList)))
for _, param := range tx.ParamList {
bw.WriteLE(uint8(param))
bw.WriteByte(byte(param))
}
bw.WriteLE(uint8(tx.ReturnType))
bw.WriteByte(byte(tx.ReturnType))
if tx.Version >= 1 {
bw.WriteLE(tx.NeedStorage)
bw.WriteBool(tx.NeedStorage)
}
bw.WriteString(tx.Name)
bw.WriteString(tx.CodeVersion)