Skip dead zones in FuzzyMatchV2 score matrix computation
Some checks failed
CodeQL / Analyze (go) (push) Has been cancelled
build / build (push) Has been cancelled
Test fzf on macOS / build (push) Has been cancelled

In Phase 3 of FuzzyMatchV2, when a cell's left neighbor score is <= 1
and the current character doesn't match the pattern character, the
cell's score is guaranteed to be 0 (since gap penalties are -1 and -3).
Skip the bonus/gap computation entirely and fast-forward through
consecutive non-matching characters in the dead zone.

This yields 6-11% faster fuzzy searches on typical workloads.
This commit is contained in:
Junegunn Choi
2026-02-22 00:12:46 +09:00
parent 09ca45f7db
commit 6df5ca17e8

View File

@@ -569,7 +569,27 @@ func FuzzyMatchV2(caseSensitive bool, normalize bool, forward bool, input *util.
Hdiag := H[row+f-f0-1-width:][:len(Tsub)] Hdiag := H[row+f-f0-1-width:][:len(Tsub)]
Hleft := H[row+f-f0-1:][:len(Tsub)] Hleft := H[row+f-f0-1:][:len(Tsub)]
Hleft[0] = 0 Hleft[0] = 0
for off, char := range Tsub { for off := 0; off < len(Tsub); off++ {
char := Tsub[off]
// When the left neighbor's score is 0 or 1 and the current character
// doesn't match, the score is guaranteed to be 0 (gap penalties
// are negative). Skip all the bonus/gap computation.
if pchar != char && Hleft[off] <= 1 {
Hsub[off] = 0
Csub[off] = 0
// Fast-forward: Hleft of the next cell is the Hsub we just
// wrote (0), so only a match can break the dead zone.
off++
for off < len(Tsub) && Tsub[off] != pchar {
Hsub[off] = 0
Csub[off] = 0
off++
}
off--
inGap = false
continue
}
col := off + f col := off + f
var s1, s2, consecutive int16 var s1, s2, consecutive int16