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:
parent
89d7f6d26e
commit
54d888ba70
43 changed files with 441 additions and 205 deletions
|
@ -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
|
||||
|
|
|
@ -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.
|
||||
|
|
|
@ -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)
|
||||
|
|
|
@ -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)
|
||||
}
|
||||
|
|
|
@ -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()
|
||||
}
|
||||
|
|
|
@ -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.
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue