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

@ -88,8 +88,7 @@ func NewBlockFromTrimmedBytes(b []byte) (*Block, error) {
br := io.NewBinReaderFromBuf(b)
block.decodeHashableFields(br)
var padding uint8
br.ReadLE(&padding)
_ = br.ReadByte()
block.Script.DecodeBinary(br)
@ -97,7 +96,7 @@ func NewBlockFromTrimmedBytes(b []byte) (*Block, error) {
block.Transactions = make([]*transaction.Transaction, lenTX)
for i := 0; i < int(lenTX); i++ {
var hash util.Uint256
br.ReadLE(&hash)
hash.DecodeBinary(br)
block.Transactions[i] = transaction.NewTrimmedTX(hash)
}
@ -110,7 +109,7 @@ func NewBlockFromTrimmedBytes(b []byte) (*Block, error) {
func (b *Block) Trim() ([]byte, error) {
buf := io.NewBufBinWriter()
b.encodeHashableFields(buf.BinWriter)
buf.WriteBytes([]byte{1})
buf.WriteByte(1)
b.Script.EncodeBinary(buf.BinWriter)
buf.WriteVarUint(uint64(len(b.Transactions)))

View file

@ -114,24 +114,24 @@ func (b *BlockBase) createHash() {
// encodeHashableFields will only encode the fields used for hashing.
// see Hash() for more information about the fields.
func (b *BlockBase) encodeHashableFields(bw *io.BinWriter) {
bw.WriteLE(b.Version)
bw.WriteU32LE(b.Version)
bw.WriteBytes(b.PrevHash[:])
bw.WriteBytes(b.MerkleRoot[:])
bw.WriteLE(b.Timestamp)
bw.WriteLE(b.Index)
bw.WriteLE(b.ConsensusData)
bw.WriteU32LE(b.Timestamp)
bw.WriteU32LE(b.Index)
bw.WriteU64LE(b.ConsensusData)
bw.WriteBytes(b.NextConsensus[:])
}
// decodeHashableFields decodes the fields used for hashing.
// see Hash() for more information about the fields.
func (b *BlockBase) decodeHashableFields(br *io.BinReader) {
br.ReadLE(&b.Version)
b.Version = br.ReadU32LE()
br.ReadBytes(b.PrevHash[:])
br.ReadBytes(b.MerkleRoot[:])
br.ReadLE(&b.Timestamp)
br.ReadLE(&b.Index)
br.ReadLE(&b.ConsensusData)
b.Timestamp = br.ReadU32LE()
b.Index = br.ReadU32LE()
b.ConsensusData = br.ReadU64LE()
br.ReadBytes(b.NextConsensus[:])
// Make the hash of the block here so we dont need to do this

View file

@ -1451,7 +1451,7 @@ func (bc *Blockchain) verifyBlockWitnesses(block *Block, prevHeader *Header) err
func hashAndIndexToBytes(h util.Uint256, index uint32) []byte {
buf := io.NewBufBinWriter()
buf.WriteBytes(h.BytesLE())
buf.WriteLE(index)
buf.WriteU32LE(index)
return buf.Bytes()
}

View file

@ -449,8 +449,7 @@ func (dao *dao) GetTransaction(hash util.Uint256) (*transaction.Transaction, uin
}
r := io.NewBinReaderFromBuf(b)
var height uint32
r.ReadLE(&height)
var height = r.ReadU32LE()
tx := &transaction.Transaction{}
tx.DecodeBinary(r)
@ -476,9 +475,8 @@ func (dao *dao) PutCurrentHeader(hashAndIndex []byte) error {
func read2000Uint256Hashes(b []byte) ([]util.Uint256, error) {
r := bytes.NewReader(b)
br := io.NewBinReaderFromIO(r)
lenHashes := br.ReadVarUint()
hashes := make([]util.Uint256, lenHashes)
br.ReadLE(hashes)
hashes := make([]util.Uint256, 0)
br.ReadArray(&hashes)
if br.Err != nil {
return nil, br.Err
}
@ -502,12 +500,12 @@ func (dao *dao) StoreAsBlock(block *Block, sysFee uint32) error {
buf = io.NewBufBinWriter()
)
// sysFee needs to be handled somehow
// buf.WriteLE(sysFee)
// buf.WriteU32LE(sysFee)
b, err := block.Trim()
if err != nil {
return err
}
buf.WriteLE(b)
buf.WriteBytes(b)
if buf.Err != nil {
return buf.Err
}
@ -517,8 +515,9 @@ func (dao *dao) StoreAsBlock(block *Block, sysFee uint32) error {
// StoreAsCurrentBlock stores the given block witch prefix SYSCurrentBlock.
func (dao *dao) StoreAsCurrentBlock(block *Block) error {
buf := io.NewBufBinWriter()
buf.WriteLE(block.Hash().BytesLE())
buf.WriteLE(block.Index)
h := block.Hash()
h.EncodeBinary(buf.BinWriter)
buf.WriteU32LE(block.Index)
return dao.store.Put(storage.SYSCurrentBlock.Bytes(), buf.Bytes())
}
@ -526,7 +525,7 @@ func (dao *dao) StoreAsCurrentBlock(block *Block) error {
func (dao *dao) StoreAsTransaction(tx *transaction.Transaction, index uint32) error {
key := storage.AppendPrefix(storage.DataTransaction, tx.Hash().BytesLE())
buf := io.NewBufBinWriter()
buf.WriteLE(index)
buf.WriteU32LE(index)
tx.EncodeBinary(buf.BinWriter)
if buf.Err != nil {
return buf.Err

View file

@ -26,7 +26,7 @@ func NewSpentCoinState(hash util.Uint256, height uint32) *SpentCoinState {
// DecodeBinary implements Serializable interface.
func (s *SpentCoinState) DecodeBinary(br *io.BinReader) {
br.ReadBytes(s.txHash[:])
br.ReadLE(&s.txHeight)
s.txHeight = br.ReadU32LE()
s.items = make(map[uint16]uint32)
lenItems := br.ReadVarUint()
@ -35,8 +35,8 @@ func (s *SpentCoinState) DecodeBinary(br *io.BinReader) {
key uint16
value uint32
)
br.ReadLE(&key)
br.ReadLE(&value)
key = br.ReadU16LE()
value = br.ReadU32LE()
s.items[key] = value
}
}
@ -44,10 +44,10 @@ func (s *SpentCoinState) DecodeBinary(br *io.BinReader) {
// EncodeBinary implements Serializable interface.
func (s *SpentCoinState) EncodeBinary(bw *io.BinWriter) {
bw.WriteBytes(s.txHash[:])
bw.WriteLE(s.txHeight)
bw.WriteU32LE(s.txHeight)
bw.WriteVarUint(uint64(len(s.items)))
for k, v := range s.items {
bw.WriteLE(k)
bw.WriteLE(v)
bw.WriteU16LE(k)
bw.WriteU32LE(v)
}
}

View file

@ -39,9 +39,9 @@ func NewAccount(scriptHash util.Uint160) *Account {
// DecodeBinary decodes Account from the given BinReader.
func (s *Account) DecodeBinary(br *io.BinReader) {
br.ReadLE(&s.Version)
s.Version = uint8(br.ReadByte())
br.ReadBytes(s.ScriptHash[:])
br.ReadLE(&s.IsFrozen)
s.IsFrozen = br.ReadBool()
br.ReadArray(&s.Votes)
s.Balances = make(map[util.Uint256][]UnspentBalance)
@ -57,9 +57,9 @@ func (s *Account) DecodeBinary(br *io.BinReader) {
// EncodeBinary encodes Account to the given BinWriter.
func (s *Account) EncodeBinary(bw *io.BinWriter) {
bw.WriteLE(s.Version)
bw.WriteByte(byte(s.Version))
bw.WriteBytes(s.ScriptHash[:])
bw.WriteLE(s.IsFrozen)
bw.WriteBool(s.IsFrozen)
bw.WriteArray(s.Votes)
bw.WriteVarUint(uint64(len(s.Balances)))
@ -72,15 +72,15 @@ func (s *Account) EncodeBinary(bw *io.BinWriter) {
// DecodeBinary implements io.Serializable interface.
func (u *UnspentBalance) DecodeBinary(r *io.BinReader) {
u.Tx.DecodeBinary(r)
r.ReadLE(&u.Index)
r.ReadLE(&u.Value)
u.Index = r.ReadU16LE()
u.Value.DecodeBinary(r)
}
// EncodeBinary implements io.Serializable interface.
func (u *UnspentBalance) EncodeBinary(w *io.BinWriter) {
u.Tx.EncodeBinary(w)
w.WriteLE(u.Index)
w.WriteLE(u.Value)
w.WriteU16LE(u.Index)
u.Value.EncodeBinary(w)
}
// GetBalanceValues sums all unspent outputs and returns a map of asset IDs to

View file

@ -29,40 +29,40 @@ type Asset struct {
// DecodeBinary implements Serializable interface.
func (a *Asset) DecodeBinary(br *io.BinReader) {
br.ReadBytes(a.ID[:])
br.ReadLE(&a.AssetType)
a.AssetType = transaction.AssetType(br.ReadByte())
a.Name = br.ReadString()
br.ReadLE(&a.Amount)
br.ReadLE(&a.Available)
br.ReadLE(&a.Precision)
br.ReadLE(&a.FeeMode)
a.Amount.DecodeBinary(br)
a.Available.DecodeBinary(br)
a.Precision = uint8(br.ReadByte())
a.FeeMode = uint8(br.ReadByte())
br.ReadBytes(a.FeeAddress[:])
a.Owner.DecodeBinary(br)
br.ReadBytes(a.Admin[:])
br.ReadBytes(a.Issuer[:])
br.ReadLE(&a.Expiration)
br.ReadLE(&a.IsFrozen)
a.Expiration = br.ReadU32LE()
a.IsFrozen = br.ReadBool()
}
// EncodeBinary implements Serializable interface.
func (a *Asset) EncodeBinary(bw *io.BinWriter) {
bw.WriteBytes(a.ID[:])
bw.WriteLE(a.AssetType)
bw.WriteByte(byte(a.AssetType))
bw.WriteString(a.Name)
bw.WriteLE(a.Amount)
bw.WriteLE(a.Available)
bw.WriteLE(a.Precision)
bw.WriteLE(a.FeeMode)
a.Amount.EncodeBinary(bw)
a.Available.EncodeBinary(bw)
bw.WriteByte(byte(a.Precision))
bw.WriteByte(byte(a.FeeMode))
bw.WriteBytes(a.FeeAddress[:])
a.Owner.EncodeBinary(bw)
bw.WriteBytes(a.Admin[:])
bw.WriteBytes(a.Issuer[:])
bw.WriteLE(a.Expiration)
bw.WriteLE(a.IsFrozen)
bw.WriteU32LE(a.Expiration)
bw.WriteBool(a.IsFrozen)
}
// GetName returns the asset name based on its type.

View file

@ -26,8 +26,8 @@ type Contract struct {
func (cs *Contract) DecodeBinary(br *io.BinReader) {
cs.Script = br.ReadVarBytes()
br.ReadArray(&cs.ParamList)
br.ReadLE(&cs.ReturnType)
br.ReadLE(&cs.Properties)
cs.ReturnType = smartcontract.ParamType(br.ReadByte())
cs.Properties = smartcontract.PropertyState(br.ReadByte())
cs.Name = br.ReadString()
cs.CodeVersion = br.ReadString()
cs.Author = br.ReadString()
@ -40,8 +40,8 @@ func (cs *Contract) DecodeBinary(br *io.BinReader) {
func (cs *Contract) EncodeBinary(bw *io.BinWriter) {
bw.WriteVarBytes(cs.Script)
bw.WriteArray(cs.ParamList)
bw.WriteLE(cs.ReturnType)
bw.WriteLE(cs.Properties)
bw.WriteByte(byte(cs.ReturnType))
bw.WriteByte(byte(cs.Properties))
bw.WriteString(cs.Name)
bw.WriteString(cs.CodeVersion)
bw.WriteString(cs.Author)

View file

@ -39,9 +39,9 @@ func (ne *NotificationEvent) DecodeBinary(r *io.BinReader) {
// EncodeBinary implements the Serializable interface.
func (aer *AppExecResult) EncodeBinary(w *io.BinWriter) {
w.WriteBytes(aer.TxHash[:])
w.WriteLE(aer.Trigger)
w.WriteByte(aer.Trigger)
w.WriteString(aer.VMState)
w.WriteLE(aer.GasConsumed)
aer.GasConsumed.EncodeBinary(w)
w.WriteString(aer.Stack)
w.WriteArray(aer.Events)
}
@ -49,9 +49,9 @@ func (aer *AppExecResult) EncodeBinary(w *io.BinWriter) {
// DecodeBinary implements the Serializable interface.
func (aer *AppExecResult) DecodeBinary(r *io.BinReader) {
r.ReadBytes(aer.TxHash[:])
r.ReadLE(&aer.Trigger)
aer.Trigger = r.ReadByte()
aer.VMState = r.ReadString()
r.ReadLE(&aer.GasConsumed)
aer.GasConsumed.DecodeBinary(r)
aer.Stack = r.ReadString()
r.ReadArray(&aer.Events)
}

View file

@ -13,11 +13,11 @@ type StorageItem struct {
// EncodeBinary implements Serializable interface.
func (si *StorageItem) EncodeBinary(w *io.BinWriter) {
w.WriteVarBytes(si.Value)
w.WriteLE(si.IsConst)
w.WriteBool(si.IsConst)
}
// DecodeBinary implements Serializable interface.
func (si *StorageItem) DecodeBinary(r *io.BinReader) {
si.Value = r.ReadVarBytes()
r.ReadLE(&si.IsConst)
si.IsConst = r.ReadBool()
}

View file

@ -21,16 +21,16 @@ func (vs *Validator) RegisteredAndHasVotes() bool {
// EncodeBinary encodes Validator to the given BinWriter.
func (vs *Validator) EncodeBinary(bw *io.BinWriter) {
vs.PublicKey.EncodeBinary(bw)
bw.WriteLE(vs.Registered)
bw.WriteLE(vs.Votes)
bw.WriteBool(vs.Registered)
vs.Votes.EncodeBinary(bw)
}
// DecodeBinary decodes Validator from the given BinReader.
func (vs *Validator) DecodeBinary(reader *io.BinReader) {
vs.PublicKey = &keys.PublicKey{}
vs.PublicKey.DecodeBinary(reader)
reader.ReadLE(&vs.Registered)
reader.ReadLE(&vs.Votes)
vs.Registered = reader.ReadBool()
vs.Votes.DecodeBinary(reader)
}
// GetValidatorsWeightedAverage applies weighted filter based on votes for validator and returns number of validators.

View file

@ -16,7 +16,7 @@ type Attribute struct {
// DecodeBinary implements Serializable interface.
func (attr *Attribute) DecodeBinary(br *io.BinReader) {
br.ReadLE(&attr.Usage)
attr.Usage = AttrUsage(br.ReadByte())
// very special case
if attr.Usage == ECDH02 || attr.Usage == ECDH03 {
@ -35,8 +35,7 @@ func (attr *Attribute) DecodeBinary(br *io.BinReader) {
datasize = 20
case DescriptionURL:
// It's not VarUint as per C# implementation, dunno why
var urllen uint8
br.ReadLE(&urllen)
var urllen = br.ReadByte()
datasize = uint64(urllen)
case Description, Remark, Remark1, Remark2, Remark3, Remark4,
Remark5, Remark6, Remark7, Remark8, Remark9, Remark10, Remark11,
@ -52,7 +51,7 @@ func (attr *Attribute) DecodeBinary(br *io.BinReader) {
// EncodeBinary implements Serializable interface.
func (attr *Attribute) EncodeBinary(bw *io.BinWriter) {
bw.WriteLE(&attr.Usage)
bw.WriteByte(byte(attr.Usage))
switch attr.Usage {
case ECDH02, ECDH03:
bw.WriteBytes(attr.Data[1:])
@ -61,8 +60,7 @@ func (attr *Attribute) EncodeBinary(bw *io.BinWriter) {
Remark12, Remark13, Remark14, Remark15:
bw.WriteVarBytes(attr.Data)
case DescriptionURL:
var urllen = uint8(len(attr.Data))
bw.WriteLE(urllen)
bw.WriteByte(byte(len(attr.Data)))
fallthrough
case Script, ContractHash, Vote, Hash1, Hash2, Hash3, Hash4, Hash5, Hash6,
Hash7, Hash8, Hash9, Hash10, Hash11, Hash12, Hash13, Hash14, Hash15:

View file

@ -17,11 +17,11 @@ type Input struct {
// DecodeBinary implements Serializable interface.
func (in *Input) DecodeBinary(br *io.BinReader) {
br.ReadBytes(in.PrevHash[:])
br.ReadLE(&in.PrevIndex)
in.PrevIndex = br.ReadU16LE()
}
// EncodeBinary implements Serializable interface.
func (in *Input) EncodeBinary(bw *io.BinWriter) {
bw.WriteBytes(in.PrevHash[:])
bw.WriteLE(in.PrevIndex)
bw.WriteU16LE(in.PrevIndex)
}

View file

@ -37,7 +37,7 @@ func NewInvocationTX(script []byte, gas util.Fixed8) *Transaction {
func (tx *InvocationTX) DecodeBinary(br *io.BinReader) {
tx.Script = br.ReadVarBytes()
if tx.Version >= 1 {
br.ReadLE(&tx.Gas)
tx.Gas.DecodeBinary(br)
} else {
tx.Gas = util.Fixed8FromInt64(0)
}
@ -47,6 +47,6 @@ func (tx *InvocationTX) DecodeBinary(br *io.BinReader) {
func (tx *InvocationTX) EncodeBinary(bw *io.BinWriter) {
bw.WriteVarBytes(tx.Script)
if tx.Version >= 1 {
bw.WriteLE(tx.Gas)
tx.Gas.EncodeBinary(bw)
}
}

View file

@ -12,10 +12,10 @@ type MinerTX struct {
// DecodeBinary implements Serializable interface.
func (tx *MinerTX) DecodeBinary(r *io.BinReader) {
r.ReadLE(&tx.Nonce)
tx.Nonce = r.ReadU32LE()
}
// EncodeBinary implements Serializable interface.
func (tx *MinerTX) EncodeBinary(w *io.BinWriter) {
w.WriteLE(tx.Nonce)
w.WriteU32LE(tx.Nonce)
}

View file

@ -36,14 +36,14 @@ func NewOutput(assetID util.Uint256, amount util.Fixed8, scriptHash util.Uint160
// DecodeBinary implements Serializable interface.
func (out *Output) DecodeBinary(br *io.BinReader) {
br.ReadBytes(out.AssetID[:])
br.ReadLE(&out.Amount)
out.Amount.DecodeBinary(br)
br.ReadBytes(out.ScriptHash[:])
}
// EncodeBinary implements Serializable interface.
func (out *Output) EncodeBinary(bw *io.BinWriter) {
bw.WriteBytes(out.AssetID[:])
bw.WriteLE(out.Amount)
out.Amount.EncodeBinary(bw)
bw.WriteBytes(out.ScriptHash[:])
}

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)

View file

@ -30,12 +30,12 @@ type RegisterTX struct {
// DecodeBinary implements Serializable interface.
func (tx *RegisterTX) DecodeBinary(br *io.BinReader) {
br.ReadLE(&tx.AssetType)
tx.AssetType = AssetType(br.ReadByte())
tx.Name = br.ReadString()
br.ReadLE(&tx.Amount)
br.ReadLE(&tx.Precision)
tx.Amount.DecodeBinary(br)
tx.Precision = uint8(br.ReadByte())
tx.Owner.DecodeBinary(br)
@ -44,10 +44,10 @@ func (tx *RegisterTX) DecodeBinary(br *io.BinReader) {
// EncodeBinary implements Serializable interface.
func (tx *RegisterTX) EncodeBinary(bw *io.BinWriter) {
bw.WriteLE(tx.AssetType)
bw.WriteByte(byte(tx.AssetType))
bw.WriteString(tx.Name)
bw.WriteLE(tx.Amount)
bw.WriteLE(tx.Precision)
tx.Amount.EncodeBinary(bw)
bw.WriteByte(byte(tx.Precision))
bw.WriteBytes(tx.Owner.Bytes())
bw.WriteBytes(tx.Admin[:])
}

View file

@ -23,7 +23,7 @@ type StateDescriptor struct {
// DecodeBinary implements Serializable interface.
func (s *StateDescriptor) DecodeBinary(r *io.BinReader) {
r.ReadLE(&s.Type)
s.Type = DescStateType(r.ReadByte())
s.Key = r.ReadVarBytes()
s.Value = r.ReadVarBytes()
@ -32,7 +32,7 @@ func (s *StateDescriptor) DecodeBinary(r *io.BinReader) {
// EncodeBinary implements Serializable interface.
func (s *StateDescriptor) EncodeBinary(w *io.BinWriter) {
w.WriteLE(s.Type)
w.WriteByte(byte(s.Type))
w.WriteVarBytes(s.Key)
w.WriteVarBytes(s.Value)
w.WriteString(s.Field)

View file

@ -92,8 +92,8 @@ func (t *Transaction) AddInput(in *Input) {
// DecodeBinary implements Serializable interface.
func (t *Transaction) DecodeBinary(br *io.BinReader) {
br.ReadLE(&t.Type)
br.ReadLE(&t.Version)
t.Type = TXType(br.ReadByte())
t.Version = uint8(br.ReadByte())
t.decodeData(br)
br.ReadArray(&t.Attributes)
@ -151,8 +151,8 @@ func (t *Transaction) EncodeBinary(bw *io.BinWriter) {
// encodeHashableFields encodes the fields that are not used for
// signing the transaction, which are all fields except the scripts.
func (t *Transaction) encodeHashableFields(bw *io.BinWriter) {
bw.WriteLE(t.Type)
bw.WriteLE(t.Version)
bw.WriteByte(byte(t.Type))
bw.WriteByte(byte(t.Version))
// Underlying TXer.
if t.Data != nil {

View file

@ -25,7 +25,7 @@ func NewUnspentCoinState(n int) *UnspentCoinState {
func (s *UnspentCoinState) EncodeBinary(bw *io.BinWriter) {
bw.WriteVarUint(uint64(len(s.states)))
for _, state := range s.states {
bw.WriteBytes([]byte{byte(state)})
bw.WriteByte(byte(state))
}
}
@ -34,8 +34,6 @@ func (s *UnspentCoinState) DecodeBinary(br *io.BinReader) {
lenStates := br.ReadVarUint()
s.states = make([]state.Coin, lenStates)
for i := 0; i < int(lenStates); i++ {
var coinState uint8
br.ReadLE(&coinState)
s.states[i] = state.Coin(coinState)
s.states[i] = state.Coin(br.ReadByte())
}
}