Optimize fuzzy search performance for ASCII strings
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Junegunn Choi · committed by Junegunn Choi · parent 298749b · Browse files · 2 files·+67 added−9 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+67 added−9 removed
- other:+67 added−9 removed
Provenance
- Commit
69aa2fe- Tree
12a49ea- Author
Junegunn Choi <junegunn.c@gmail.com>- Committer
Junegunn Choi <junegunn.c@gmail.com>- Parent
298749b
2 files changed+67 added−9 removed
modified:src/algo/algo.go+59 added-9 removed
76 unmodified lines77 ````````````78 */79 80 import (81+ "bytes"82 "fmt"83 "strings"84 "unicode"85+ "unicode/utf8"86 87 "github.com/junegunn/fzf/src/util"88 )89 162 unmodified lines252 // 1. "pattern" is given in lowercase if "caseSensitive" is false253 // 2. "pattern" is already normalized if "normalize" is true254 type Algo func(caseSensitive bool, normalize bool, forward bool, input util.Chars, pattern []rune, withPos bool, slab *util.Slab) (Result, *[]int)255 256+func trySkip(input *util.Chars, caseSensitive bool, b byte, from int) int {257+ byteArray := input.Bytes()[from:]258+ idx := bytes.IndexByte(byteArray, b)259+ if idx == 0 {260+ // Can't skip any further261+ return from262+ }263+ // We may need to search for the uppercase letter again. We don't have to264+ // consider normalization as we can be sure that this is an ASCII string.265+ if !caseSensitive && b >= 'a' && b <= 'z' {266+ uidx := bytes.IndexByte(byteArray, b-32)267+ if idx < 0 || uidx >= 0 && uidx < idx {268+ idx = uidx269+ }270+ }271+ if idx < 0 {272+ return -1273+ }274+ return from + idx275+}276+277 func FuzzyMatchV2(caseSensitive bool, normalize bool, forward bool, input util.Chars, pattern []rune, withPos bool, slab *util.Slab) (Result, *[]int) {278 // Assume that pattern is given in lowercase if case-insensitive.279 // First check if there's a match and calculate bonus for each position.280 // If the input string is too long, consider finding the matching chars in281 // this phase as well (non-optimal alignment).259- N := input.Length()282 M := len(pattern)261- switch M {262- case 0:283+ if M == 0 {284 return Result{0, 0, 0}, posArray(withPos, M)264- case 1:265- return ExactMatchNaive(caseSensitive, normalize, forward, input, pattern[0:1], withPos, slab)285 }286+ N := input.Length()287 288 // Since O(nm) algorithm can be prohibitively expensive for large input,289 // we fall back to the greedy algorithm.290 if slab != nil && N*M > cap(slab.I16) {9 unmodified lines300 offset32, F := alloc32(offset32, slab, M, false)301 // Rune array302 offset32, T := alloc32(offset32, slab, N, false)303 284- // Phase 1. Check if there's a match and calculate bonus for each point304+ // Phase 1. Optimized search for ASCII string305+ firstIdx := 0306+ if input.IsBytes() {307+ idx := 0308+ for pidx := 0; pidx < M; pidx++ {309+ // Not possible310+ if pattern[pidx] >= utf8.RuneSelf {311+ return Result{-1, -1, 0}, nil312+ }313+ idx = trySkip(&input, caseSensitive, byte(pattern[pidx]), idx)314+ if idx < 0 {315+ return Result{-1, -1, 0}, nil316+ }317+ if pidx == 0 && idx > 0 {318+ // Step back to find the right bonus point319+ firstIdx = idx - 1320+ }321+ idx++322+ }323+ }324+325+ // Phase 2. Calculate bonus for each point326 pidx, lastIdx, prevClass := 0, 0, charNonWord327 input.CopyRunes(T)287- for idx := 0; idx < N; idx++ {328+ for idx := firstIdx; idx < N; idx++ {329 char := T[idx]330 var class charClass331 if char <= unicode.MaxASCII {332 class = charClassOfAscii(char)31 unmodified lines364 }365 if pidx != M {366 return Result{-1, -1, 0}, nil367 }368+ if M == 1 && B[F[0]] == bonusBoundary {369+ p := int(F[0])370+ result := Result{p, p + 1, scoreMatch + bonusBoundary*bonusFirstCharMultiplier}371+ if !withPos {372+ return result, nil373+ }374+ pos := []int{p}375+ return result, &pos376+ }377 328- // Phase 2. Fill in score matrix (H)378+ // Phase 3. Fill in score matrix (H)379 // Unlike the original algorithm, we do not allow omission.380 width := lastIdx - int(F[0]) + 1381 offset16, H := alloc16(offset16, slab, width*M, false)382 80 unmodified lines463 fmt.Println()464 }465 }466 417- // Phase 3. (Optional) Backtrace to find character positions467+ // Phase 4. (Optional) Backtrace to find character positions468 pos := posArray(withPos, M)469 j := int(F[0])470 if withPos {471 i := M - 1331 unmodified linesmodified:src/util/chars.go+8 added
63 unmodified lines64 func RunesToChars(runes []rune) Chars {65 return Chars{slice: *(*[]byte)(unsafe.Pointer(&runes)), inBytes: false}66 }67 68+func (chars *Chars) IsBytes() bool {69+ return chars.inBytes70+}71+72+func (chars *Chars) Bytes() []byte {73+ return chars.slice74+}75+76 func (chars *Chars) optionalRunes() []rune {77 if chars.inBytes {78 return nil79 }88 unmodified lines