From 908cb6060bc617a2b93f9e0416ea13d5eeb5d10a Mon Sep 17 00:00:00 2001 From: Elle Mouton Date: Thu, 2 Feb 2023 10:59:15 +0200 Subject: [PATCH] channeldb: optimise FetchChannel method This commit adds a small optimisation to the FetchChannel method. Instead of iterating over each channel bucket, an identifiable error is thrown once the wanted channel is found so that the iteration can stop early. --- channeldb/db.go | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/channeldb/db.go b/channeldb/db.go index 7c204375b..273189b47 100644 --- a/channeldb/db.go +++ b/channeldb/db.go @@ -661,6 +661,10 @@ func (c *ChannelStateDB) FetchChannel(tx kvdb.RTx, chanPoint wire.OutPoint) ( var ( targetChan *OpenChannel targetChanPoint bytes.Buffer + + // errChanFound is used to signal that the channel has been + // found so that iteration through the DB buckets can stop. + errChanFound = errors.New("channel found") ) if err := writeOutpoint(&targetChanPoint, &chanPoint); err != nil { @@ -739,7 +743,7 @@ func (c *ChannelStateDB) FetchChannel(tx kvdb.RTx, chanPoint wire.OutPoint) ( targetChan = channel targetChan.Db = c - return nil + return errChanFound }) }) } @@ -750,7 +754,7 @@ func (c *ChannelStateDB) FetchChannel(tx kvdb.RTx, chanPoint wire.OutPoint) ( } else { err = chanScan(tx) } - if err != nil { + if err != nil && !errors.Is(err, errChanFound) { return nil, err }