forked from cerc-io/plugeth
trie: improve node rlp decoding performance (#25357)
This avoids copying the input []byte while decoding trie nodes. In most cases, particularly when the input slice is provided by the underlying database, this optimization is safe to use. For cases where the origin of the input slice is unclear, the copying version is retained. The new code performs better even when the input must be copied, because it is now only copied once in decodeNode.
This commit is contained in:
+26
-4
@@ -99,6 +99,7 @@ func (n valueNode) fstring(ind string) string {
|
||||
return fmt.Sprintf("%x ", []byte(n))
|
||||
}
|
||||
|
||||
// mustDecodeNode is a wrapper of decodeNode and panic if any error is encountered.
|
||||
func mustDecodeNode(hash, buf []byte) node {
|
||||
n, err := decodeNode(hash, buf)
|
||||
if err != nil {
|
||||
@@ -107,8 +108,29 @@ func mustDecodeNode(hash, buf []byte) node {
|
||||
return n
|
||||
}
|
||||
|
||||
// decodeNode parses the RLP encoding of a trie node.
|
||||
// mustDecodeNodeUnsafe is a wrapper of decodeNodeUnsafe and panic if any error is
|
||||
// encountered.
|
||||
func mustDecodeNodeUnsafe(hash, buf []byte) node {
|
||||
n, err := decodeNodeUnsafe(hash, buf)
|
||||
if err != nil {
|
||||
panic(fmt.Sprintf("node %x: %v", hash, err))
|
||||
}
|
||||
return n
|
||||
}
|
||||
|
||||
// decodeNode parses the RLP encoding of a trie node. It will deep-copy the passed
|
||||
// byte slice for decoding, so it's safe to modify the byte slice afterwards. The-
|
||||
// decode performance of this function is not optimal, but it is suitable for most
|
||||
// scenarios with low performance requirements and hard to determine whether the
|
||||
// byte slice be modified or not.
|
||||
func decodeNode(hash, buf []byte) (node, error) {
|
||||
return decodeNodeUnsafe(hash, common.CopyBytes(buf))
|
||||
}
|
||||
|
||||
// decodeNodeUnsafe parses the RLP encoding of a trie node. The passed byte slice
|
||||
// will be directly referenced by node without bytes deep copy, so the input MUST
|
||||
// not be changed after.
|
||||
func decodeNodeUnsafe(hash, buf []byte) (node, error) {
|
||||
if len(buf) == 0 {
|
||||
return nil, io.ErrUnexpectedEOF
|
||||
}
|
||||
@@ -141,7 +163,7 @@ func decodeShort(hash, elems []byte) (node, error) {
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("invalid value node: %v", err)
|
||||
}
|
||||
return &shortNode{key, append(valueNode{}, val...), flag}, nil
|
||||
return &shortNode{key, valueNode(val), flag}, nil
|
||||
}
|
||||
r, _, err := decodeRef(rest)
|
||||
if err != nil {
|
||||
@@ -164,7 +186,7 @@ func decodeFull(hash, elems []byte) (*fullNode, error) {
|
||||
return n, err
|
||||
}
|
||||
if len(val) > 0 {
|
||||
n.Children[16] = append(valueNode{}, val...)
|
||||
n.Children[16] = valueNode(val)
|
||||
}
|
||||
return n, nil
|
||||
}
|
||||
@@ -190,7 +212,7 @@ func decodeRef(buf []byte) (node, []byte, error) {
|
||||
// empty node
|
||||
return nil, rest, nil
|
||||
case kind == rlp.String && len(val) == 32:
|
||||
return append(hashNode{}, val...), rest, nil
|
||||
return hashNode(val), rest, nil
|
||||
default:
|
||||
return nil, nil, fmt.Errorf("invalid RLP string size %d (want 0 or 32)", len(val))
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user