Skip to content
Myr

Use a shared work queue instead of static partitioning in matcher

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Replace static chunk partitioning (sliceChunks) with a shared atomic counter that workers pull from. This gives natural load balancing; workers that finish chunks quickly grab more work instead of idling. With this change, NumCPU workers suffice (no need for 8x oversubscription), reducing goroutine overhead while improving throughput by 5-22%. Now the performance scales linearly to the number of threads: === query: 'linux' === [all] baseline: 17.12ms current: 14.28ms (1.20x) matches: 179966 (12.79%) [1T] baseline: 136.49ms current: 137.25ms (0.99x) matches: 179966 (12.79%) [2T] baseline: 75.74ms current: 68.75ms (1.10x) matches: 179966 (12.79%) [4T] baseline: 41.16ms current: 34.97ms (1.18x) matches: 179966 (12.79%) [8T] baseline: 32.82ms current: 17.79ms (1.84x) matches: 179966 (12.79%)

authored by Junegunn Choi · committed by Junegunn Choi · parent 92dc40e · Browse files · 2 files+23 added−47 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

2 files+23 added−47 removed

  • other:+23 added−47 removed

2 files+23 added−47 removed

modified:src/constants.go+1 added-3 removed
32 unmodified lines
33 maxBgProcesses = 3034 maxBgProcessesPerAction = 33536 // Matcher37 numPartitionsMultiplier = 838 maxPartitions = 3239 progressMinDuration = 200 * time.Millisecond37 progressMinDuration = 200 * time.Millisecond3839 // Capacity of each chunk40 chunkSize int = 100041
35 unmodified lines
modified:src/matcher.go+22 added-44 removed
2 unmodified lines
3import (4 "fmt"5 "runtime"6 "sync"7 "sync/atomic"8 "time"910 "github.com/junegunn/fzf/src/util"11)
45 unmodified lines
5758// NewMatcher returns a new Matcher59func NewMatcher(cache *ChunkCache, patternBuilder func([]rune) *Pattern,60 sort bool, tac bool, eventBox *util.EventBox, revision revision, threads int) *Matcher {60 partitions := min(numPartitionsMultiplier*runtime.NumCPU(), maxPartitions)61 partitions := runtime.NumCPU()62 if threads > 0 {63 partitions = threads64 }65 return &Matcher{
82 unmodified lines
148 }149 }150}151151func (m *Matcher) sliceChunks(chunks []*Chunk) [][]*Chunk {152 partitions := m.partitions153 perSlice := len(chunks) / partitions154155 if perSlice == 0 {156 partitions = len(chunks)157 perSlice = 1158 }159160 slices := make([][]*Chunk, partitions)161 for i := 0; i < partitions; i++ {162 start := i * perSlice163 end := start + perSlice164 if i == partitions-1 {165 end = len(chunks)166 }167 slices[i] = chunks[start:end]168 }169 return slices170}171152type partialResult struct {153 index int154 matches []Result155}
15 unmodified lines
171 minIndex := request.chunks[0].items[0].Index()172 maxIndex := request.chunks[numChunks-1].lastIndex(minIndex)173 cancelled := util.NewAtomicBool(false)174195 slices := m.sliceChunks(request.chunks)196 numSlices := len(slices)197 resultChan := make(chan partialResult, numSlices)175 numWorkers := min(m.partitions, numChunks)176 var nextChunk atomic.Int32177 resultChan := make(chan partialResult, numWorkers)178 countChan := make(chan int, numChunks)179 waitGroup := sync.WaitGroup{}180201 for idx, chunks := range slices {181 for idx := range numWorkers {182 waitGroup.Add(1)183 if m.slab[idx] == nil {184 m.slab[idx] = util.MakeSlab(slab16Size, slab32Size)185 }206 go func(idx int, slab *util.Slab, chunks []*Chunk) {207 defer func() { waitGroup.Done() }()208 count := 0209 allMatches := make([][]Result, len(chunks))210 for idx, chunk := range chunks {211 matches := request.pattern.Match(chunk, slab)212 allMatches[idx] = matches213 count += len(matches)186 go func(idx int, slab *util.Slab) {187 defer waitGroup.Done()188 var matches []Result189 for {190 ci := int(nextChunk.Add(1)) - 1191 if ci >= numChunks {192 break193 }194 chunkMatches := request.pattern.Match(request.chunks[ci], slab)195 matches = append(matches, chunkMatches...)196 if cancelled.Get() {197 return198 }217 countChan <- len(matches)199 countChan <- len(chunkMatches)200 }219 sliceMatches := make([]Result, 0, count)220 for _, matches := range allMatches {221 sliceMatches = append(sliceMatches, matches...)222 }201 if m.sort && request.pattern.sortable {224 m.sortBuf[idx] = radixSortResults(sliceMatches, m.tac, m.sortBuf[idx])202 m.sortBuf[idx] = radixSortResults(matches, m.tac, m.sortBuf[idx])203 }226 resultChan <- partialResult{idx, sliceMatches}227 }(idx, m.slab[idx], chunks)204 resultChan <- partialResult{idx, matches}205 }(idx, m.slab[idx])206 }207208 wait := func() bool {209 cancelled.Set(true)
19 unmodified lines
229 m.eventBox.Set(EvtSearchProgress, float32(count)/float32(numChunks))230 }231 }232255 partialResults := make([][]Result, numSlices)256 for range slices {233 partialResults := make([][]Result, numWorkers)234 for range numWorkers {235 partialResult := <-resultChan236 partialResults[partialResult.index] = partialResult.matches237 }238 merger := NewMerger(pattern, partialResults, m.sort && request.pattern.sortable, m.tac, request.revision, minIndex, maxIndex)
33 unmodified lines