Skip to content

Commit

Permalink
trie: use a scratchspace for path
Browse files Browse the repository at this point in the history
goos: linux
goarch: amd64
pkg: github.com/ethereum/go-ethereum/trie
cpu: 12th Gen Intel(R) Core(TM) i7-1270P
             │ stacktrie.3  │          stacktrie.4          │
             │    sec/op    │    sec/op     vs base         │
Insert100K-8   69.50m ± 12%   74.59m ± 14%  ~ (p=0.128 n=7)

             │ stacktrie.3  │             stacktrie.4             │
             │     B/op     │     B/op      vs base               │
Insert100K-8   4.640Mi ± 0%   3.112Mi ± 0%  -32.93% (p=0.001 n=7)

             │ stacktrie.3 │            stacktrie.4             │
             │  allocs/op  │  allocs/op   vs base               │
Insert100K-8   226.7k ± 0%   126.7k ± 0%  -44.11% (p=0.001 n=7)
  • Loading branch information
holiman committed Nov 11, 2024
1 parent f0ccc37 commit 4a2a72b
Showing 1 changed file with 8 additions and 5 deletions.
13 changes: 8 additions & 5 deletions trie/stacktrie.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,16 +48,19 @@ type StackTrie struct {
last []byte
onTrieNode OnTrieNode

keyScratch []byte
keyScratch []byte
pathScratch []byte
}

// NewStackTrie allocates and initializes an empty trie. The committed nodes
// will be discarded immediately if no callback is configured.
func NewStackTrie(onTrieNode OnTrieNode) *StackTrie {
return &StackTrie{
root: stPool.Get().(*stNode),
h: newHasher(false),
onTrieNode: onTrieNode,
root: stPool.Get().(*stNode),
h: newHasher(false),
onTrieNode: onTrieNode,
keyScratch: make([]byte, 0, 32),
pathScratch: make([]byte, 0, 32),
}
}

Expand Down Expand Up @@ -86,7 +89,7 @@ func (t *StackTrie) Update(key, value []byte) error {
} else {
t.last = append(t.last[:0], k...) // reuse key slice
}
t.insert(t.root, k, value, nil)
t.insert(t.root, k, value, t.pathScratch[:0])
return nil
}

Expand Down

0 comments on commit 4a2a72b

Please sign in to comment.