Skip to content
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

perf: Prevent wasted time on adding to LRU cache during large commits #937

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions cache/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ type Cache interface {

// Len returns the cache length.
Len() int

// Capacity returns the maximum number of nodes the cache can hold.
Capacity() int
}

// lruCache is an LRU cache implementation.
Expand Down Expand Up @@ -96,6 +99,10 @@ func (c *lruCache) Len() int {
return c.ll.Len()
}

func (c *lruCache) Capacity() int {
return c.maxElementCount
}

func (c *lruCache) Remove(key []byte) Node {
if elem, exists := c.dict[string(key)]; exists {
return c.removeWithKey(elem, string(key))
Expand Down
9 changes: 6 additions & 3 deletions mutable_tree.go
Original file line number Diff line number Diff line change
Expand Up @@ -745,7 +745,7 @@ func (tree *MutableTree) SaveVersion() ([]byte, int64, error) {
// it will update the legacy node to the new format
// which ensures the reference node is not a legacy node
tree.root.isLegacy = false
if err := tree.ndb.SaveNode(tree.root); err != nil {
if err := tree.ndb.SaveNode(tree.root, true); err != nil {
return nil, 0, fmt.Errorf("failed to save the reference legacy node: %w", err)
}
}
Expand Down Expand Up @@ -1044,8 +1044,11 @@ func (tree *MutableTree) saveNewNodes(version int64) error {
return err
}

for _, node := range newNodes {
if err := tree.ndb.SaveNode(node); err != nil {
cacheCapacity := tree.ndb.nodeCache.Capacity()
for i, node := range newNodes {
// only add up to cacheCapacity nodes to the cache
addToLruCache := i < cacheCapacity
if err := tree.ndb.SaveNode(node, addToLruCache); err != nil {
return err
}
node.leftNode, node.rightNode = nil, nil
Expand Down
8 changes: 5 additions & 3 deletions nodedb.go
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ func (ndb *nodeDB) GetFastNode(key []byte) (*fastnode.Node, error) {
}

// SaveNode saves a node to disk.
func (ndb *nodeDB) SaveNode(node *Node) error {
func (ndb *nodeDB) SaveNode(node *Node, useLruCache bool) error {
ndb.mtx.Lock()
defer ndb.mtx.Unlock()

Expand All @@ -225,7 +225,9 @@ func (ndb *nodeDB) SaveNode(node *Node) error {
}

ndb.logger.Debug("BATCH SAVE", "node", node)
ndb.nodeCache.Add(node)
if useLruCache {
ndb.nodeCache.Add(node)
}
return nil
}

Expand Down Expand Up @@ -386,7 +388,7 @@ func (ndb *nodeDB) deleteVersion(version int64) error {
}
// instead, the root should be reformatted to (version, 0)
root.nodeKey.nonce = 0
if err := ndb.SaveNode(root); err != nil {
if err := ndb.SaveNode(root, true); err != nil {
return err
}
}
Expand Down
2 changes: 1 addition & 1 deletion tree_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1775,7 +1775,7 @@ func TestNodeCacheStatisic(t *testing.T) {
expectCacheMissCnt int
}{
"with_cache": {
cacheSize: numKeyVals,
cacheSize: 2 * numKeyVals, // has to cover all inner nodes
expectFastCacheHitCnt: numKeyVals,
expectFastCacheMissCnt: 0,
expectCacheHitCnt: 1,
Expand Down
Loading