From 637ac85d1d8914bff14366e14fd8b54e03b5e861 Mon Sep 17 00:00:00 2001 From: Matheus Degiovani Date: Wed, 13 Mar 2024 12:46:42 -0300 Subject: [PATCH] missioncontrolstore: skip work when there are no new entries This modifies the mission control store to avoid doing any work when no new payment result entries are in the queue to be processed. The mission control store maintains keeps the latest N (in production: 1000) entries in its DB, evicting older entries when new ones are added. Currently, its implementation is somewhat less performant than it could be. This commit adds an early return to the storeResults function to avoid doing any DB or memory operations when its outstanding queue is empty, improving the performance during quiescent periods of the LN node's execution. --- routing/missioncontrol_store.go | 23 +++++++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) diff --git a/routing/missioncontrol_store.go b/routing/missioncontrol_store.go index dd60d9346..cfe0a2cf0 100644 --- a/routing/missioncontrol_store.go +++ b/routing/missioncontrol_store.go @@ -96,6 +96,8 @@ func newMissionControlStore(db kvdb.Backend, maxRecords int, return nil, err } + log.Infof("Loaded %d mission control entries", len(keysMap)) + return &missionControlStore{ done: make(chan struct{}), db: db, @@ -302,14 +304,24 @@ func (b *missionControlStore) run() { // storeResults stores all accumulated results. func (b *missionControlStore) storeResults() error { + // We copy a reference to the queue and clear the original queue to be + // able to release the lock. b.queueMx.Lock() l := b.queue + + if l.Len() == 0 { + b.queueMx.Unlock() + + return nil + } b.queue = list.New() b.queueMx.Unlock() var ( - keys *list.List - keysMap map[string]struct{} + keys *list.List + keysMap map[string]struct{} + storeCount int + pruneCount int ) err := kvdb.Update(b.db, func(tx kvdb.RwTx) error { @@ -337,6 +349,7 @@ func (b *missionControlStore) storeResults() error { keys.PushBack(string(k)) keysMap[string(k)] = struct{}{} + storeCount++ } // Prune oldest entries. @@ -354,6 +367,7 @@ func (b *missionControlStore) storeResults() error { keys.Remove(front) delete(keysMap, key) + pruneCount++ } return nil @@ -365,12 +379,17 @@ func (b *missionControlStore) storeResults() error { for k := range b.keysMap { keysMap[k] = struct{}{} } + + storeCount, pruneCount = 0, 0 }) if err != nil { return err } + log.Debugf("Stored mission control results: %d added, %d deleted", + storeCount, pruneCount) + b.keys = keys b.keysMap = keysMap