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

@ -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.