rlp, trie: faster trie node encoding (#24126)
This change speeds up trie hashing and all other activities that require RLP encoding of trie nodes by approximately 20%. The speedup is achieved by avoiding reflection overhead during node encoding. The interface type trie.node now contains a method 'encode' that works with rlp.EncoderBuffer. Management of EncoderBuffers is left to calling code. trie.hasher, which is pooled to avoid allocations, now maintains an EncoderBuffer. This means memory resources related to trie node encoding are tied to the hasher pool. Co-authored-by: Felix Lange <fjl@twurst.com>
This commit is contained in:
@ -56,20 +56,16 @@ type Encoder interface {
|
||||
// Please see package-level documentation of encoding rules.
|
||||
func Encode(w io.Writer, val interface{}) error {
|
||||
// Optimization: reuse *encBuffer when called by EncodeRLP.
|
||||
if buf, ok := w.(*encBuffer); ok {
|
||||
if buf := encBufferFromWriter(w); buf != nil {
|
||||
return buf.encode(val)
|
||||
}
|
||||
if ebuf, ok := w.(EncoderBuffer); ok {
|
||||
return ebuf.buf.encode(val)
|
||||
}
|
||||
|
||||
buf := getEncBuffer()
|
||||
defer encBufferPool.Put(buf)
|
||||
|
||||
if err := buf.encode(val); err != nil {
|
||||
return err
|
||||
}
|
||||
return buf.toWriter(w)
|
||||
return buf.writeTo(w)
|
||||
}
|
||||
|
||||
// EncodeToBytes returns the RLP encoding of val.
|
||||
@ -81,7 +77,7 @@ func EncodeToBytes(val interface{}) ([]byte, error) {
|
||||
if err := buf.encode(val); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return buf.toBytes(), nil
|
||||
return buf.makeBytes(), nil
|
||||
}
|
||||
|
||||
// EncodeToReader returns a reader from which the RLP encoding of val
|
||||
|
Reference in New Issue
Block a user