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
|
@ -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)))
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue