-
Notifications
You must be signed in to change notification settings - Fork 20.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
ethdb/memorydb, trie: reduced allocations #28473
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -144,7 +144,8 @@ type nodeIterator struct { | |
path []byte // Path to the current node | ||
err error // Failure set in case of an internal error in the iterator | ||
|
||
resolver NodeResolver // optional node resolver for avoiding disk hits | ||
resolver NodeResolver // optional node resolver for avoiding disk hits | ||
pool []*nodeIteratorState // local pool for iteratorstates | ||
} | ||
|
||
// errIteratorEnd is stored in nodeIterator.err when iteration is done. | ||
|
@@ -172,6 +173,24 @@ func newNodeIterator(trie *Trie, start []byte) NodeIterator { | |
return it | ||
} | ||
|
||
func (it *nodeIterator) putInPool(item *nodeIteratorState) { | ||
if len(it.pool) < 40 { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hi @holiman , I'm confused why pool size should be 40? And not sure concurrency safe should be considered in putInPool and getFromPool |
||
item.node = nil | ||
it.pool = append(it.pool, item) | ||
} | ||
} | ||
|
||
func (it *nodeIterator) getFromPool() *nodeIteratorState { | ||
idx := len(it.pool) - 1 | ||
if idx < 0 { | ||
return new(nodeIteratorState) | ||
} | ||
el := it.pool[idx] | ||
it.pool[idx] = nil | ||
it.pool = it.pool[:idx] | ||
return el | ||
} | ||
|
||
func (it *nodeIterator) AddResolver(resolver NodeResolver) { | ||
it.resolver = resolver | ||
} | ||
|
@@ -423,8 +442,9 @@ func (st *nodeIteratorState) resolve(it *nodeIterator, path []byte) error { | |
return nil | ||
} | ||
|
||
func findChild(n *fullNode, index int, path []byte, ancestor common.Hash) (node, *nodeIteratorState, []byte, int) { | ||
func (it *nodeIterator) findChild(n *fullNode, index int, ancestor common.Hash) (node, *nodeIteratorState, []byte, int) { | ||
var ( | ||
path = it.path | ||
child node | ||
state *nodeIteratorState | ||
childPath []byte | ||
|
@@ -433,13 +453,12 @@ func findChild(n *fullNode, index int, path []byte, ancestor common.Hash) (node, | |
if n.Children[index] != nil { | ||
child = n.Children[index] | ||
hash, _ := child.cache() | ||
state = &nodeIteratorState{ | ||
hash: common.BytesToHash(hash), | ||
node: child, | ||
parent: ancestor, | ||
index: -1, | ||
pathlen: len(path), | ||
} | ||
state = it.getFromPool() | ||
state.hash = common.BytesToHash(hash) | ||
state.node = child | ||
state.parent = ancestor | ||
state.index = -1 | ||
state.pathlen = len(path) | ||
childPath = append(childPath, path...) | ||
childPath = append(childPath, byte(index)) | ||
return child, state, childPath, index | ||
|
@@ -452,21 +471,20 @@ func (it *nodeIterator) nextChild(parent *nodeIteratorState, ancestor common.Has | |
switch node := parent.node.(type) { | ||
case *fullNode: | ||
// Full node, move to the first non-nil child. | ||
if child, state, path, index := findChild(node, parent.index+1, it.path, ancestor); child != nil { | ||
if child, state, path, index := it.findChild(node, parent.index+1, ancestor); child != nil { | ||
parent.index = index - 1 | ||
return state, path, true | ||
} | ||
case *shortNode: | ||
// Short node, return the pointer singleton child | ||
if parent.index < 0 { | ||
hash, _ := node.Val.cache() | ||
state := &nodeIteratorState{ | ||
hash: common.BytesToHash(hash), | ||
node: node.Val, | ||
parent: ancestor, | ||
index: -1, | ||
pathlen: len(it.path), | ||
} | ||
state := it.getFromPool() | ||
state.hash = common.BytesToHash(hash) | ||
state.node = node.Val | ||
state.parent = ancestor | ||
state.index = -1 | ||
state.pathlen = len(it.path) | ||
path := append(it.path, node.Key...) | ||
return state, path, true | ||
} | ||
|
@@ -480,7 +498,7 @@ func (it *nodeIterator) nextChildAt(parent *nodeIteratorState, ancestor common.H | |
switch n := parent.node.(type) { | ||
case *fullNode: | ||
// Full node, move to the first non-nil child before the desired key position | ||
child, state, path, index := findChild(n, parent.index+1, it.path, ancestor) | ||
child, state, path, index := it.findChild(n, parent.index+1, ancestor) | ||
if child == nil { | ||
// No more children in this fullnode | ||
return parent, it.path, false | ||
|
@@ -492,7 +510,7 @@ func (it *nodeIterator) nextChildAt(parent *nodeIteratorState, ancestor common.H | |
} | ||
// The child is before the seek position. Try advancing | ||
for { | ||
nextChild, nextState, nextPath, nextIndex := findChild(n, index+1, it.path, ancestor) | ||
nextChild, nextState, nextPath, nextIndex := it.findChild(n, index+1, ancestor) | ||
// If we run out of children, or skipped past the target, return the | ||
// previous one | ||
if nextChild == nil || bytes.Compare(nextPath, key) >= 0 { | ||
|
@@ -506,13 +524,12 @@ func (it *nodeIterator) nextChildAt(parent *nodeIteratorState, ancestor common.H | |
// Short node, return the pointer singleton child | ||
if parent.index < 0 { | ||
hash, _ := n.Val.cache() | ||
state := &nodeIteratorState{ | ||
hash: common.BytesToHash(hash), | ||
node: n.Val, | ||
parent: ancestor, | ||
index: -1, | ||
pathlen: len(it.path), | ||
} | ||
state := it.getFromPool() | ||
state.hash = common.BytesToHash(hash) | ||
state.node = n.Val | ||
state.parent = ancestor | ||
state.index = -1 | ||
state.pathlen = len(it.path) | ||
path := append(it.path, n.Key...) | ||
return state, path, true | ||
} | ||
|
@@ -533,6 +550,8 @@ func (it *nodeIterator) pop() { | |
it.path = it.path[:last.pathlen] | ||
it.stack[len(it.stack)-1] = nil | ||
it.stack = it.stack[:len(it.stack)-1] | ||
// last is now unused | ||
it.putInPool(last) | ||
} | ||
|
||
func compareNodes(a, b NodeIterator) int { | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Any particular reason to put the pool here? Usually we always define a global pool.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Well, it's rather that I didn't think a global
nodeIteratorPool
was "generically useful". We only use node iterators here and there. So I figured that we can take the overhead of 'ramping up' a fresh pool in those instances, rather than having yet another global pool.Aside from that, a local non-synched pool is slightly faster since it doesn't need syncing.