Skip to content

Commit

Permalink
Prevent wasted time on Commit
Browse files Browse the repository at this point in the history
  • Loading branch information
ValarDragon committed Apr 28, 2024
1 parent f0c4102 commit af8cf93
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 7 deletions.
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

0 comments on commit af8cf93

Please sign in to comment.