Skip to content
Myr

Make --filter non-blocking when --no-sort (#132)

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

When fzf works in filtering mode (--filter) and sorting is disabled (--no-sort), there's no need to block until input is complete. This commit makes fzf print the matches on-the-fly when the following condition is met: --filter FILTER --no-sort [--no-tac --no-sync] or simply: -f FILTER +s This removes unnecessary delay in use cases like the following: fzf -f xxx +s | head -5 However, in this case, fzf processes the input lines sequentially, so it cannot utilize multiple cores, which makes it slightly slower than the previous mode of execution where filtering is done in parallel after the entire input is loaded. If the user is concerned about the performance problem, one can add --sync option to re-enable buffering.

authored by Junegunn Choi · committed by Junegunn Choi · parent 4d2d186 · Browse files · 3 files+76 added−50 removed
This commit

State

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

Diff stats

3 files+76 added−50 removed

  • other:+76 added−50 removed

3 files+76 added−50 removed

modified:src/core.go+28 added-14 removed
83 unmodified lines
84 })85 }8687 // Reader88 streamingFilter := opts.Filter != nil && opts.Sort == 0 && !opts.Tac && !opts.Sync89 if !streamingFilter {88 reader := Reader{func(str string) { chunkList.Push(str) }, eventBox}89 go reader.ReadSource()90 reader := Reader{func(str string) { chunkList.Push(str) }, eventBox}91 go reader.ReadSource()92 }9394 // Matcher95 patternBuilder := func(runes []rune) *Pattern {96 return BuildPattern(
2 unmodified lines
99 matcher := NewMatcher(patternBuilder, opts.Sort > 0, opts.Tac, eventBox)100101 // Filtering mode102 if opts.Filter != nil {103 if opts.PrintQuery {104 fmt.Println(*opts.Filter)105 }106107 pattern := patternBuilder([]rune(*opts.Filter))108109 if streamingFilter {110 reader := Reader{111 func(str string) {112 item := chunkList.trans(&str, 0)113 if pattern.MatchItem(item) {114 fmt.Println(*item.text)115 }116 }, eventBox}117 reader.ReadSource()118 } else {102 eventBox.Unwatch(EvtReadNew)103 eventBox.WaitFor(EvtReadFin)119 eventBox.Unwatch(EvtReadNew)120 eventBox.WaitFor(EvtReadFin)121105 snapshot, _ := chunkList.Snapshot()106 merger, _ := matcher.scan(MatchRequest{107 chunks: snapshot,108 pattern: pattern})109110 if opts.PrintQuery {111 fmt.Println(*opts.Filter)122 snapshot, _ := chunkList.Snapshot()123 merger, _ := matcher.scan(MatchRequest{124 chunks: snapshot,125 pattern: pattern})126 for i := 0; i < merger.Length(); i++ {127 fmt.Println(merger.Get(i).AsString())128 }129 }113 for i := 0; i < merger.Length(); i++ {114 fmt.Println(merger.Get(i).AsString())115 }130 os.Exit(0)131 }132133 // Synchronous search
80 unmodified lines
modified:src/pattern_test.go+3 added-2 removed
96 unmodified lines
97 pattern := BuildPattern(ModeExtended, CaseSmart, []Range{}, nil, []rune("jg"))98 tokens := Tokenize(strptr("junegunn"), nil)99 trans := Transform(tokens, []Range{Range{1, 1}})100101 for _, fun := range []func(*Chunk) []*Item{pattern.fuzzyMatch, pattern.extendedMatch} {101 for _, mode := range []Mode{ModeFuzzy, ModeExtended} {102 chunk := Chunk{103 &Item{104 text: strptr("junegunn"),105 origText: strptr("junegunn.choi"),106 transformed: trans},107 }108 matches := fun(&chunk)108 pattern.mode = mode109 matches := pattern.matchChunk(&chunk)110 if *matches[0].text != "junegunn" || *matches[0].origText != "junegunn.choi" ||111 matches[0].offsets[0][0] != 0 || matches[0].offsets[0][1] != 5 ||112 matches[0].transformed != trans {113 t.Error("Invalid match result", matches)
3 unmodified lines
modified:src/pattern.go+45 added-34 removed
217 unmodified lines
218 }219 }220 }221222 var matches []*Item223 if p.mode == ModeFuzzy {224 matches = p.fuzzyMatch(space)225 } else {226 matches = p.extendedMatch(space)227 }222 matches := p.matchChunk(space)223224 if !p.hasInvTerm {225 _cache.Add(chunk, cacheKey, matches)226 }227 return matches228}229230func (p *Pattern) matchChunk(chunk *Chunk) []*Item {231 matches := []*Item{}232 if p.mode == ModeFuzzy {233 for _, item := range *chunk {234 if sidx, eidx := p.fuzzyMatch(item); sidx >= 0 {235 matches = append(matches,236 dupItem(item, []Offset{Offset{int32(sidx), int32(eidx)}}))237 }238 }239 } else {240 for _, item := range *chunk {241 if offsets := p.extendedMatch(item); len(offsets) == len(p.terms) {242 matches = append(matches, dupItem(item, offsets))243 }244 }245 }246 return matches247}248249// MatchItem returns true if the Item is a match250func (p *Pattern) MatchItem(item *Item) bool {251 if p.mode == ModeFuzzy {252 sidx, _ := p.fuzzyMatch(item)253 return sidx >= 0254 }255 offsets := p.extendedMatch(item)256 return len(offsets) == len(p.terms)257}258259func dupItem(item *Item, offsets []Offset) *Item {260 sort.Sort(ByOrder(offsets))261 return &Item{262 text: item.text,
3 unmodified lines
266 offsets: offsets,267 rank: Rank{0, 0, item.index}}268}269246func (p *Pattern) fuzzyMatch(chunk *Chunk) []*Item {247 matches := []*Item{}248 for _, item := range *chunk {249 input := p.prepareInput(item)250 if sidx, eidx := p.iter(algo.FuzzyMatch, input, p.text); sidx >= 0 {251 matches = append(matches,252 dupItem(item, []Offset{Offset{int32(sidx), int32(eidx)}}))253 }254 }255 return matches270func (p *Pattern) fuzzyMatch(item *Item) (int, int) {271 input := p.prepareInput(item)272 return p.iter(algo.FuzzyMatch, input, p.text)273}274258func (p *Pattern) extendedMatch(chunk *Chunk) []*Item {259 matches := []*Item{}260 for _, item := range *chunk {261 input := p.prepareInput(item)262 offsets := []Offset{}263 for _, term := range p.terms {264 pfun := p.procFun[term.typ]265 if sidx, eidx := p.iter(pfun, input, term.text); sidx >= 0 {266 if term.inv {267 break275func (p *Pattern) extendedMatch(item *Item) []Offset {276 input := p.prepareInput(item)277 offsets := []Offset{}278 for _, term := range p.terms {279 pfun := p.procFun[term.typ]280 if sidx, eidx := p.iter(pfun, input, term.text); sidx >= 0 {281 if term.inv {282 break268 }269 offsets = append(offsets, Offset{int32(sidx), int32(eidx)})270 } else if term.inv {271 offsets = append(offsets, Offset{0, 0})283 }284 offsets = append(offsets, Offset{int32(sidx), int32(eidx)})285 } else if term.inv {286 offsets = append(offsets, Offset{0, 0})287 }274 if len(offsets) == len(p.terms) {275 matches = append(matches, dupItem(item, offsets))276 }288 }278 return matches289 return offsets290}291292func (p *Pattern) prepareInput(item *Item) *Transformed {293 if item.transformed != nil {
27 unmodified lines