Skip to content
Myr

Add direct algo fast path in matchChunk

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

For the common case of a single fuzzy term with no nth transform, call the algo function directly from matchChunk, bypassing the MatchItem -> extendedMatch -> iter dispatch chain. This eliminates 3 function calls and the per-match []Offset heap allocation.

authored by Junegunn Choi · committed by Junegunn Choi · parent 8452c78 · Browse files · 2 files+56 added−3 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+56 added−3 removed

  • other:+56 added−3 removed

2 files+56 added−3 removed

modified:src/pattern.go+48 added
63 unmodified lines
64 procFun map[termType]algo.Algo65 cache *ChunkCache66 denylist map[int32]struct{}67 startIndex int3268 directAlgo algo.Algo69 directTerm *term70}7172var _splitRegex *regexp.Regexp73
78 unmodified lines
152 startIndex: startIndex,153 procFun: make(map[termType]algo.Algo)}154155 ptr.cacheKey = ptr.buildCacheKey()156 ptr.directAlgo, ptr.directTerm = ptr.buildDirectAlgo(fuzzyAlgo)157 ptr.procFun[termFuzzy] = fuzzyAlgo158 ptr.procFun[termEqual] = algo.EqualMatch159 ptr.procFun[termExact] = algo.ExactMatchNaive160 ptr.procFun[termExactBoundary] = algo.ExactMatchBoundary
115 unmodified lines
276 }277 return strings.Join(cacheableTerms, "\t")278}279280// buildDirectAlgo returns the algo function and term for the direct fast path281// in matchChunk. Returns (nil, nil) if the pattern is not suitable.282// Requirements: extended mode, single term set with single non-inverse fuzzy term, no nth.283func (p *Pattern) buildDirectAlgo(fuzzyAlgo algo.Algo) (algo.Algo, *term) {284 if !p.extended || len(p.nth) > 0 {285 return nil, nil286 }287 if len(p.termSets) == 1 && len(p.termSets[0]) == 1 {288 t := &p.termSets[0][0]289 if !t.inv && t.typ == termFuzzy {290 return fuzzyAlgo, t291 }292 }293 return nil, nil294}295296// CacheKey is used to build string to be used as the key of result cache297func (p *Pattern) CacheKey() string {298 return p.cacheKey299}
30 unmodified lines
330 return matches331 }332 }333334 // Fast path: single fuzzy term, no nth, no denylist.335 // Calls the algo function directly, bypassing MatchItem/extendedMatch/iter336 // and avoiding per-match []Offset heap allocation.337 if p.directAlgo != nil && len(p.denylist) == 0 {338 t := p.directTerm339 if space == nil {340 for idx := startIdx; idx < chunk.count; idx++ {341 res, _ := p.directAlgo(t.caseSensitive, t.normalize, p.forward,342 &chunk.items[idx].text, t.text, p.withPos, slab)343 if res.Start >= 0 {344 matches = append(matches, buildResultFromBounds(345 &chunk.items[idx], res.Score,346 int(res.Start), int(res.End), int(res.End), true))347 }348 }349 } else {350 for _, result := range space {351 res, _ := p.directAlgo(t.caseSensitive, t.normalize, p.forward,352 &result.item.text, t.text, p.withPos, slab)353 if res.Start >= 0 {354 matches = append(matches, buildResultFromBounds(355 result.item, res.Score,356 int(res.Start), int(res.End), int(res.End), true))357 }358 }359 }360 return matches361 }362363 if len(p.denylist) == 0 {364 // Huge code duplication for minimizing unnecessary map lookups365 if space == nil {366 for idx := startIdx; idx < chunk.count; idx++ {
152 unmodified lines
modified:src/result.go+8 added-3 removed
31 unmodified lines
32 if len(offsets) > 1 {33 sort.Sort(ByOrder(offsets))34 }3536 result := Result{item: item}37 numChars := item.text.Length()36 minBegin := math.MaxUint1637 minEnd := math.MaxUint1638 maxEnd := 039 validOffsetFound := false
6 unmodified lines
46 validOffsetFound = true47 }48 }4950 return buildResultFromBounds(item, score, minBegin, minEnd, maxEnd, validOffsetFound)51}5253// buildResultFromBounds builds a Result from pre-computed offset bounds.54func buildResultFromBounds(item *Item, score int, minBegin, minEnd, maxEnd int, validOffsetFound bool) Result {55 result := Result{item: item}56 numChars := item.text.Length()5758 for idx, criterion := range sortCriteria {59 val := uint16(math.MaxUint16)60 switch criterion {61 case byScore:
18 unmodified lines
80 case byLength:81 val = item.TrimLength()82 case byPathname:83 if validOffsetFound {78 // lastDelim := strings.LastIndexByte(item.text.ToString(), '/')84 lastDelim := -185 s := item.text.ToString()86 for i := len(s) - 1; i >= 0; i-- {87 if s[i] == '/' || s[i] == '\\' {
254 unmodified lines