summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJunegunn Choi <junegunn.c@gmail.com>2024-04-13 16:57:34 +0900
committerJunegunn Choi <junegunn.c@gmail.com>2024-04-14 11:47:06 +0900
commita5447b8b7531dacb49961d3fccc404f634f06709 (patch)
tree0b4bea8e4b8a135d5bc077ee2ec2465694f6acc4
parent7ce6452d83a62a3bc409fef8face1cfaef6e0146 (diff)
Improve search performance by pre-calculating bonus matrix
This gives yet another 5% boost.
-rw-r--r--src/algo/algo.go14
1 files changed, 11 insertions, 3 deletions
diff --git a/src/algo/algo.go b/src/algo/algo.go
index 6e0d437c..41b2db97 100644
--- a/src/algo/algo.go
+++ b/src/algo/algo.go
@@ -156,6 +156,9 @@ var (
// A minor optimization that can give 15%+ performance boost
asciiCharClasses [unicode.MaxASCII + 1]charClass
+
+ // A minor optimization that can give yet another 5% performance boost
+ bonusMatrix [charNumber + 1][charNumber + 1]int16
)
type charClass int
@@ -206,6 +209,11 @@ func Init(scheme string) bool {
}
asciiCharClasses[i] = c
}
+ for i := 0; i <= int(charNumber); i++ {
+ for j := 0; j <= int(charNumber); j++ {
+ bonusMatrix[i][j] = bonusFor(charClass(i), charClass(j))
+ }
+ }
return true
}
@@ -291,7 +299,7 @@ func bonusAt(input *util.Chars, idx int) int16 {
if idx == 0 {
return bonusBoundaryWhite
}
- return bonusFor(charClassOf(input.Get(idx-1)), charClassOf(input.Get(idx)))
+ return bonusMatrix[charClassOf(input.Get(idx-1))][charClassOf(input.Get(idx))]
}
func normalizeRune(r rune) rune {
@@ -467,7 +475,7 @@ func FuzzyMatchV2(caseSensitive bool, normalize bool, forward bool, input *util.
Tsub[off] = char
}
- bonus := bonusFor(prevClass, class)
+ bonus := bonusMatrix[prevClass][class]
Bsub[off] = bonus
prevClass = class
@@ -650,7 +658,7 @@ func calculateScore(caseSensitive bool, normalize bool, text *util.Chars, patter
*pos = append(*pos, idx)
}
score += scoreMatch
- bonus := bonusFor(prevClass, class)
+ bonus := bonusMatrix[prevClass][class]
if consecutive == 0 {
firstBonus = bonus
} else {