From 73548fef14f3bf978f54696b598d8ba2b024f25f Mon Sep 17 00:00:00 2001 From: qianbin Date: Tue, 25 Feb 2020 01:04:02 +0800 Subject: [PATCH 1/2] leveldb: optimize pickCompaction using binary search to find next table --- leveldb/session_compaction.go | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/leveldb/session_compaction.go b/leveldb/session_compaction.go index 4c1d336b..41032ede 100644 --- a/leveldb/session_compaction.go +++ b/leveldb/session_compaction.go @@ -7,6 +7,7 @@ package leveldb import ( + "sort" "sync/atomic" "github.com/syndtr/goleveldb/leveldb/iterator" @@ -62,10 +63,21 @@ func (s *session) pickCompaction() *compaction { sourceLevel = v.cLevel cptr := s.getCompPtr(sourceLevel) tables := v.levels[sourceLevel] - for _, t := range tables { - if cptr == nil || s.icmp.Compare(t.imax, cptr) > 0 { - t0 = append(t0, t) - break + if cptr != nil { + if sourceLevel == 0 { + for _, t := range tables { + if s.icmp.Compare(t.imax, cptr) > 0 { + t0 = append(t0, t) + break + } + } + } else { + n := len(tables) + if i := sort.Search(n, func(i int) bool { + return s.icmp.Compare(tables[i].imax, cptr) > 0 + }); i < n { + t0 = append(t0, tables[i]) + } } } if len(t0) == 0 { From 220069e961bfb1a36a6bb8dcc1dd2ffbb8819434 Mon Sep 17 00:00:00 2001 From: qianbin Date: Mon, 13 Jul 2020 10:48:23 +0800 Subject: [PATCH 2/2] leveldb: remove unnecessary code for picking L0 table --- leveldb/session_compaction.go | 21 ++++++--------------- 1 file changed, 6 insertions(+), 15 deletions(-) diff --git a/leveldb/session_compaction.go b/leveldb/session_compaction.go index 41032ede..b46a3e45 100644 --- a/leveldb/session_compaction.go +++ b/leveldb/session_compaction.go @@ -63,21 +63,12 @@ func (s *session) pickCompaction() *compaction { sourceLevel = v.cLevel cptr := s.getCompPtr(sourceLevel) tables := v.levels[sourceLevel] - if cptr != nil { - if sourceLevel == 0 { - for _, t := range tables { - if s.icmp.Compare(t.imax, cptr) > 0 { - t0 = append(t0, t) - break - } - } - } else { - n := len(tables) - if i := sort.Search(n, func(i int) bool { - return s.icmp.Compare(tables[i].imax, cptr) > 0 - }); i < n { - t0 = append(t0, tables[i]) - } + if cptr != nil && sourceLevel > 0 { + n := len(tables) + if i := sort.Search(n, func(i int) bool { + return s.icmp.Compare(tables[i].imax, cptr) > 0 + }); i < n { + t0 = append(t0, tables[i]) } } if len(t0) == 0 {