mirror of
https://github.com/junegunn/fzf
synced 2026-08-11 20:11:42 +00:00
Add fast path for two-character patterns in FuzzyMatchV2
For two ASCII characters, rows 0 and 1 of the score matrix collapse to scalar running state, so Phase 2 and Phase 3 fuse into one pass with no score arrays. withPos stores the two rows for the backtrace. Up to 1.4x on two-char queries, the most common multi-char length. Verify both fast paths against the general algorithm with exhaustive (every short string over a class-complete alphabet) and fuzz tests, runnable via the new make fuzz target.
This commit is contained in:
@@ -241,3 +241,75 @@ func TestResultPositionsWithReusedSlab(t *testing.T) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// TestFuzzyMatchV2TwoEquivalence verifies that the two-character fast path
|
||||
// produces the same Result and positions as the general algorithm across
|
||||
// case sensitivity, direction, and withPos, using a reused slab to surface
|
||||
// any stale-data reads in the backtrace.
|
||||
func TestFuzzyMatchV2TwoEquivalence(t *testing.T) {
|
||||
words := []string{"src", "main", "core", "config", "parser", "render", "server",
|
||||
"client", "index", "handler", "util", "list", "cache", "reader"}
|
||||
exts := []string{".go", ".rb", ".py", ".md", ".c", ".txt"}
|
||||
// Deterministic corpus (LCG), plus adversarial short/repeated items
|
||||
corpus := []util.Chars{}
|
||||
seed := uint32(12345)
|
||||
next := func(n int) int { seed = seed*1664525 + 1013904223; return int(seed>>8) % n }
|
||||
for i := 0; i < 4000; i++ {
|
||||
depth := 2 + next(4)
|
||||
s := ""
|
||||
for d := 0; d < depth; d++ {
|
||||
if d > 0 {
|
||||
s += "/"
|
||||
}
|
||||
s += words[next(len(words))]
|
||||
if next(5) == 0 {
|
||||
s += "_" + words[next(len(words))]
|
||||
}
|
||||
}
|
||||
s += exts[next(len(exts))]
|
||||
corpus = append(corpus, util.ToChars([]byte(s)))
|
||||
}
|
||||
for _, s := range []string{"", "a", "ab", "aa", "aXb", "a/b", "//", "..", "abcabc",
|
||||
"AaBb", "x.y.z", "a_b_c", "CoreCore", " co"} {
|
||||
corpus = append(corpus, util.ToChars([]byte(s)))
|
||||
}
|
||||
|
||||
pats := []string{"co", "ab", "aa", "//", "..", "sr", "a/", "_c", "oo", "Ab",
|
||||
"z.", "b.", "1a", "ll", "re", "er", "Co"}
|
||||
slab := util.MakeSlab(100*1024, 2048)
|
||||
for _, cs := range []bool{false, true} {
|
||||
for _, fwd := range []bool{true, false} {
|
||||
for _, wp := range []bool{false, true} {
|
||||
for _, p := range pats {
|
||||
pattern := []rune(p)
|
||||
for j := range corpus {
|
||||
disableTwo = true
|
||||
rg, pg := FuzzyMatchV2(cs, false, fwd, &corpus[j], pattern, wp, slab)
|
||||
disableTwo = false
|
||||
rt, pt := FuzzyMatchV2(cs, false, fwd, &corpus[j], pattern, wp, slab)
|
||||
if rg != rt {
|
||||
t.Fatalf("Result cs=%v fwd=%v wp=%v pat=%q item=%q: general %v vs two %v",
|
||||
cs, fwd, wp, p, corpus[j].ToString(), rg, rt)
|
||||
}
|
||||
if (pg == nil) != (pt == nil) || (pg != nil && !equalInts(*pg, *pt)) {
|
||||
t.Fatalf("Pos cs=%v fwd=%v wp=%v pat=%q item=%q: general %v vs two %v",
|
||||
cs, fwd, wp, p, corpus[j].ToString(), pg, pt)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func equalInts(a, b []int) bool {
|
||||
if len(a) != len(b) {
|
||||
return false
|
||||
}
|
||||
for i := range a {
|
||||
if a[i] != b[i] {
|
||||
return false
|
||||
}
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user