Skip to content

Commit

Permalink
Faster trie node encode
Browse files Browse the repository at this point in the history
  • Loading branch information
hqjang-pepper authored and blukat29 committed Sep 19, 2023
1 parent d61856f commit 854acc4
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 10 deletions.
14 changes: 4 additions & 10 deletions storage/statedb/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ type node interface {
fstring(string) string
cache() (hashNode, bool)
lenEncoded() uint16
encode(w rlp.EncoderBuffer)
}

type (
Expand All @@ -57,16 +58,9 @@ var nilValueNode = valueNode(nil)

// EncodeRLP encodes a full node into the consensus RLP format.
func (n *fullNode) EncodeRLP(w io.Writer) error {
var nodes [17]node

for i, child := range &n.Children {
if child != nil {
nodes[i] = child
} else {
nodes[i] = nilValueNode
}
}
return rlp.Encode(w, nodes)
eb := rlp.NewEncoderBuffer(w)
n.encode(eb)
return eb.Flush()
}

func (n *fullNode) copy() *fullNode { copy := *n; return &copy }
Expand Down
83 changes: 83 additions & 0 deletions storage/statedb/node_enc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
// Modifications Copyright 2023 The klaytn Authors
// Copyright 2022 The go-ethereum Authors
// This file is part of the go-ethereum library.
//
// The go-ethereum library is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// The go-ethereum library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
//
// This file is derived from trie/node_enc.go (2023/09/19).
// Modified and improved for the klaytn development.

package statedb

import (
"github.com/klaytn/klaytn/rlp"
)

func (n *fullNode) encode(w rlp.EncoderBuffer) {
offset := w.List()
for _, c := range n.Children {
if c != nil {
c.encode(w)
} else {
w.Write(rlp.EmptyString)
}
}
w.ListEnd(offset)
}

func (n *shortNode) encode(w rlp.EncoderBuffer) {
offset := w.List()
w.WriteBytes(n.Key)
if n.Val != nil {
n.Val.encode(w)
} else {
w.Write(rlp.EmptyString)
}
w.ListEnd(offset)
}

func (n hashNode) encode(w rlp.EncoderBuffer) {
w.WriteBytes(n)
}

func (n valueNode) encode(w rlp.EncoderBuffer) {
w.WriteBytes(n)
}

func (n rawFullNode) encode(w rlp.EncoderBuffer) {
offset := w.List()
for _, c := range n {
if c != nil {
c.encode(w)
} else {
w.Write(rlp.EmptyString)
}
}
w.ListEnd(offset)
}

func (n *rawShortNode) encode(w rlp.EncoderBuffer) {
offset := w.List()
w.WriteBytes(n.Key)
if n.Val != nil {
n.Val.encode(w)
} else {
w.Write(rlp.EmptyString)
}
w.ListEnd(offset)
}

func (n rawNode) encode(w rlp.EncoderBuffer) {
w.Write(n)
}

0 comments on commit 854acc4

Please sign in to comment.