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: remove duplicated rootkey fetch inpruning (9% pruning speedup on osmosis) #1026

Merged
merged 1 commit into from
Dec 27, 2024
Merged
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
31 changes: 27 additions & 4 deletions nodedb.go
Original file line number Diff line number Diff line change
Expand Up @@ -416,10 +416,32 @@ func (ndb *nodeDB) saveNodeFromPruning(node *Node) error {
return ndb.batch.Set(ndb.nodeKey(node.GetKey()), buf.Bytes())
}

type rootkeyCache struct {
version int64
rootKey []byte
}

func (rkc *rootkeyCache) getRootKey(ndb *nodeDB, version int64) ([]byte, error) {
if rkc.version == version {
return rkc.rootKey, nil
}
rootKey, err := ndb.GetRoot(version)
if err != nil {
return nil, err
}
rkc.setRootKey(version, rootKey)
return rootKey, nil
}

func (rkc *rootkeyCache) setRootKey(version int64, rootKey []byte) {
rkc.version = version
rkc.rootKey = rootKey
}

// deleteVersion deletes a tree version from disk.
// deletes orphans
func (ndb *nodeDB) deleteVersion(version int64) error {
rootKey, err := ndb.GetRoot(version)
func (ndb *nodeDB) deleteVersion(version int64, cache *rootkeyCache) error {
rootKey, err := cache.getRootKey(ndb, version)
if err != nil {
return err
}
Expand Down Expand Up @@ -457,7 +479,7 @@ func (ndb *nodeDB) deleteVersion(version int64) error {
}

// check if the version is referred by the next version
nextRootKey, err := ndb.GetRoot(version + 1)
nextRootKey, err := cache.getRootKey(ndb, version+1)
if err != nil {
return err
}
Expand Down Expand Up @@ -684,8 +706,9 @@ func (ndb *nodeDB) deleteVersionsTo(toVersion int64) error {
ndb.resetLegacyLatestVersion(-1)
}

rootkeyCache := &rootkeyCache{}
for version := first; version <= toVersion; version++ {
if err := ndb.deleteVersion(version); err != nil {
if err := ndb.deleteVersion(version, rootkeyCache); err != nil {
return err
}
ndb.resetFirstVersion(version + 1)
Expand Down
Loading