Skip to content

Commit

Permalink
optimize allocations by using pre allocated buffer for priorities (dg…
Browse files Browse the repository at this point in the history
  • Loading branch information
mYmNeo committed Jan 25, 2024
1 parent 65d2676 commit 60fb8d7
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 4 deletions.
2 changes: 1 addition & 1 deletion db.go
Original file line number Diff line number Diff line change
Expand Up @@ -1334,7 +1334,7 @@ func (db *DB) Flatten(workers int) error {
}
}
if len(levels) <= 1 {
prios := db.lc.pickCompactLevels()
prios := db.lc.pickCompactLevels(nil)
if len(prios) == 0 || prios[0].score <= 1.0 {
db.opt.Infof("All tables consolidated into one level. Flattening done.\n")
return nil
Expand Down
17 changes: 14 additions & 3 deletions levels.go
Original file line number Diff line number Diff line change
Expand Up @@ -397,8 +397,13 @@ func (s *levelsController) runCompactor(id int, lc *y.Closer) {
}
return false
}

var priosBuffer []compactionPriority
runOnce := func() bool {
prios := s.pickCompactLevels()
prios := s.pickCompactLevels(priosBuffer)
defer func() {
priosBuffer = prios
}()
if id == 0 {
// Worker ID zero prefers to compact L0 always.
prios = moveL0toFront(prios)
Expand Down Expand Up @@ -464,7 +469,7 @@ type compactionPriority struct {

// pickCompactLevel determines which level to compact.
// Based on: https://github.com/facebook/rocksdb/wiki/Leveled-Compaction
func (s *levelsController) pickCompactLevels() (prios []compactionPriority) {
func (s *levelsController) pickCompactLevels(priosBuffer []compactionPriority) (prios []compactionPriority) {
// This function must use identical criteria for guaranteeing compaction's progress that
// addLevel0Table uses.

Expand All @@ -477,6 +482,12 @@ func (s *levelsController) pickCompactLevels() (prios []compactionPriority) {
prios = append(prios, pri)
}

// Grow buffer to fit all levels.
if cap(priosBuffer) < len(s.levels) {
priosBuffer = make([]compactionPriority, 0, len(s.levels))
}
prios = priosBuffer[:0]

addPriority(0, float64(s.levels[0].numTables())/float64(s.kv.opt.NumLevelZeroTables))
for i := 1; i < len(s.levels); i++ {
// Don't consider those tables that are already being compacted right now.
Expand Down Expand Up @@ -1151,7 +1162,7 @@ func (s *levelsController) addLevel0Table(t *table.Table) error {
}
time.Sleep(10 * time.Millisecond)
if i%100 == 0 {
prios := s.pickCompactLevels()
prios := s.pickCompactLevels(nil)
s.elog.Printf("Waiting to add level 0 table. Compaction priorities: %+v\n", prios)
i = 0
}
Expand Down

0 comments on commit 60fb8d7

Please sign in to comment.