From 71d746006b396a999d6659246e1a84007c3ad0ad Mon Sep 17 00:00:00 2001 From: Martin Holst Swende Date: Wed, 8 Dec 2021 16:00:30 +0100 Subject: [PATCH 1/2] leveldb/memdb: avoid repeated comparisons --- leveldb/memdb/memdb.go | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/leveldb/memdb/memdb.go b/leveldb/memdb/memdb.go index 824e47f5..da4bea28 100644 --- a/leveldb/memdb/memdb.go +++ b/leveldb/memdb/memdb.go @@ -211,10 +211,11 @@ func (p *DB) randHeight() (h int) { func (p *DB) findGE(key []byte, prev bool) (int, bool) { node := 0 h := p.maxHeight - 1 + visited := 0 for { next := p.nodeData[node+nNext+h] cmp := 1 - if next != 0 { + if next != 0 && next != visited { o := p.nodeData[next] cmp = p.cmp.Compare(p.kvData[o:o+p.nodeData[next+nKey]], key) } @@ -222,6 +223,11 @@ func (p *DB) findGE(key []byte, prev bool) (int, bool) { // Keep searching in this list node = next } else { + if next != 0 { + // If we go down further, we can skip this element if we + // encounter it on a lower level. + visited = next + } if prev { p.prevNode[h] = node } else if cmp == 0 { From c612a5c66d206688b43dfd47d1d45fe82f31386c Mon Sep 17 00:00:00 2001 From: Martin Holst Swende Date: Thu, 9 Dec 2021 13:15:14 +0100 Subject: [PATCH 2/2] memdb: fix the case where we find an early exact match --- leveldb/memdb/memdb.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/leveldb/memdb/memdb.go b/leveldb/memdb/memdb.go index da4bea28..86a2d158 100644 --- a/leveldb/memdb/memdb.go +++ b/leveldb/memdb/memdb.go @@ -223,7 +223,7 @@ func (p *DB) findGE(key []byte, prev bool) (int, bool) { // Keep searching in this list node = next } else { - if next != 0 { + if next != 0 && cmp != 0 { // If we go down further, we can skip this element if we // encounter it on a lower level. visited = next