mpt: do not allocate NodeObject for serialization

Signed-off-by: Evgeniy Stratonikov <evgeniy@nspcc.ru>
This commit is contained in:
Evgeniy Stratonikov 2021-08-03 14:19:50 +03:00
parent e41fc2fd1b
commit 43ee671f36
5 changed files with 11 additions and 27 deletions

View file

@ -23,7 +23,6 @@ type BaseNodeIface interface {
Hash() util.Uint256
Type() NodeType
Bytes() []byte
EncodeBinaryAsChild(w *io.BinWriter)
}
type flushedNode interface {
@ -76,6 +75,15 @@ func (b *BaseNode) invalidateCache() {
b.hashValid = false
}
func encodeBinaryAsChild(n Node, w *io.BinWriter) {
if isEmpty(n) {
w.WriteB(byte(EmptyT))
return
}
w.WriteB(byte(HashT))
w.WriteBytes(n.Hash().BytesBE())
}
// encodeNodeWithType encodes node together with it's type.
func encodeNodeWithType(n Node, w *io.BinWriter) {
switch t := n.Type(); t {

View file

@ -48,16 +48,10 @@ func (b *BranchNode) Bytes() []byte {
// EncodeBinary implements io.Serializable.
func (b *BranchNode) EncodeBinary(w *io.BinWriter) {
for i := 0; i < childrenCount; i++ {
b.Children[i].EncodeBinaryAsChild(w)
encodeBinaryAsChild(b.Children[i], w)
}
}
// EncodeBinaryAsChild implements BaseNode interface.
func (b *BranchNode) EncodeBinaryAsChild(w *io.BinWriter) {
n := &NodeObject{Node: NewHashNode(b.Hash())} // with type
n.EncodeBinary(w)
}
// DecodeBinary implements io.Serializable.
func (b *BranchNode) DecodeBinary(r *io.BinReader) {
for i := 0; i < childrenCount; i++ {

View file

@ -69,13 +69,7 @@ func (e *ExtensionNode) DecodeBinary(r *io.BinReader) {
// EncodeBinary implements io.Serializable.
func (e ExtensionNode) EncodeBinary(w *io.BinWriter) {
w.WriteVarBytes(e.key)
e.next.EncodeBinaryAsChild(w)
}
// EncodeBinaryAsChild implements BaseNode interface.
func (e *ExtensionNode) EncodeBinaryAsChild(w *io.BinWriter) {
n := &NodeObject{Node: NewHashNode(e.Hash())} // with type
n.EncodeBinary(w)
encodeBinaryAsChild(e.next, w)
}
// MarshalJSON implements json.Marshaler.

View file

@ -58,12 +58,6 @@ func (h HashNode) EncodeBinary(w *io.BinWriter) {
w.WriteBytes(h.hash[:])
}
// EncodeBinaryAsChild implements BaseNode interface.
func (h *HashNode) EncodeBinaryAsChild(w *io.BinWriter) {
no := &NodeObject{Node: h} // with type
no.EncodeBinary(w)
}
// MarshalJSON implements json.Marshaler.
func (h *HashNode) MarshalJSON() ([]byte, error) {
if !h.hashValid {

View file

@ -56,12 +56,6 @@ func (n LeafNode) EncodeBinary(w *io.BinWriter) {
w.WriteVarBytes(n.value)
}
// EncodeBinaryAsChild implements BaseNode interface.
func (n *LeafNode) EncodeBinaryAsChild(w *io.BinWriter) {
no := &NodeObject{Node: NewHashNode(n.Hash())} // with type
no.EncodeBinary(w)
}
// MarshalJSON implements json.Marshaler.
func (n *LeafNode) MarshalJSON() ([]byte, error) {
return []byte(`{"value":"` + hex.EncodeToString(n.value) + `"}`), nil