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,18 +29,18 @@ func NewAddressAndTime(e *net.TCPAddr, t time.Time) *AddressAndTime {
// DecodeBinary implements Serializable interface.
func (p *AddressAndTime) DecodeBinary(br *io.BinReader) {
br.ReadLE(&p.Timestamp)
br.ReadLE(&p.Services)
p.Timestamp = br.ReadU32LE()
p.Services = br.ReadU64LE()
br.ReadBytes(p.IP[:])
br.ReadBE(&p.Port)
p.Port = br.ReadU16BE()
}
// EncodeBinary implements Serializable interface.
func (p *AddressAndTime) EncodeBinary(bw *io.BinWriter) {
bw.WriteLE(p.Timestamp)
bw.WriteLE(p.Services)
bw.WriteU32LE(p.Timestamp)
bw.WriteU64LE(p.Services)
bw.WriteBytes(p.IP[:])
bw.WriteBE(p.Port)
bw.WriteU16BE(p.Port)
}
// IPPortString makes a string from IP and port specified.

View file

@ -56,12 +56,12 @@ func NewInventory(typ InventoryType, hashes []util.Uint256) *Inventory {
// DecodeBinary implements Serializable interface.
func (p *Inventory) DecodeBinary(br *io.BinReader) {
br.ReadLE(&p.Type)
p.Type = InventoryType(br.ReadByte())
br.ReadArray(&p.Hashes)
}
// EncodeBinary implements Serializable interface.
func (p *Inventory) EncodeBinary(bw *io.BinWriter) {
bw.WriteLE(p.Type)
bw.WriteByte(byte(p.Type))
bw.WriteArray(p.Hashes)
}

View file

@ -55,25 +55,25 @@ func NewVersion(id uint32, p uint16, ua string, h uint32, r bool) *Version {
// DecodeBinary implements Serializable interface.
func (p *Version) DecodeBinary(br *io.BinReader) {
br.ReadLE(&p.Version)
br.ReadLE(&p.Services)
br.ReadLE(&p.Timestamp)
br.ReadLE(&p.Port)
br.ReadLE(&p.Nonce)
p.Version = br.ReadU32LE()
p.Services = br.ReadU64LE()
p.Timestamp = br.ReadU32LE()
p.Port = br.ReadU16LE()
p.Nonce = br.ReadU32LE()
p.UserAgent = br.ReadVarBytes()
br.ReadLE(&p.StartHeight)
br.ReadLE(&p.Relay)
p.StartHeight = br.ReadU32LE()
p.Relay = br.ReadBool()
}
// EncodeBinary implements Serializable interface.
func (p *Version) EncodeBinary(br *io.BinWriter) {
br.WriteLE(p.Version)
br.WriteLE(p.Services)
br.WriteLE(p.Timestamp)
br.WriteLE(p.Port)
br.WriteLE(p.Nonce)
br.WriteU32LE(p.Version)
br.WriteU64LE(p.Services)
br.WriteU32LE(p.Timestamp)
br.WriteU16LE(p.Port)
br.WriteU32LE(p.Nonce)
br.WriteVarBytes(p.UserAgent)
br.WriteLE(p.StartHeight)
br.WriteLE(&p.Relay)
br.WriteU32LE(p.StartHeight)
br.WriteBool(p.Relay)
}