Improve search performance by pre-calculating character classes
Before adoption: this commit came in with the history the project adopted, so no landing records it.
This simple optmization can give more than 15% performance boost in some scenarios.
authored by Junegunn Choi · committed by Junegunn Choi · parent 5643a30 · Browse files · 3 files·+28 added−21 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
3 files+28 added−21 removed
- other:+28 added−21 removed
Provenance
- Commit
7ce6452- Tree
a6aa9e4- Author
Junegunn Choi <junegunn.c@gmail.com>- Committer
Junegunn Choi <junegunn.c@gmail.com>- Parent
5643a30
3 files changed+28 added−21 removed
modified:src/algo/algo_test.go+4 added
7 unmodified lines8 9 "github.com/junegunn/fzf/src/util"10 )11 12+func init() {13+ Init("default")14+}15+16 func assertMatch(t *testing.T, fun Algo, caseSensitive, forward bool, input, pattern string, sidx int, eidx int, score int) {17 assertMatch2(t, fun, caseSensitive, false, forward, input, pattern, sidx, eidx, score)18 }19 183 unmodified linesmodified:src/algo/algo.go+23 added-18 removed
151 unmodified lines152 // Extra bonus for word boundary after slash, colon, semi-colon, and comma153 bonusBoundaryDelimiter int16 = bonusBoundary + 1154 155 initialCharClass charClass = charWhite156+157+ // A minor optimization that can give 15%+ performance boost158+ asciiCharClasses [unicode.MaxASCII + 1]charClass159 )160 161 type charClass int162 26 unmodified lines189 bonusBoundaryDelimiter = bonusBoundary190 default:191 return false192 }193+ for i := 0; i <= unicode.MaxASCII; i++ {194+ char := rune(i)195+ c := charNonWord196+ if char >= 'a' && char <= 'z' {197+ c = charLower198+ } else if char >= 'A' && char <= 'Z' {199+ c = charUpper200+ } else if char >= '0' && char <= '9' {201+ c = charNumber202+ } else if strings.ContainsRune(whiteChars, char) {203+ c = charWhite204+ } else if strings.ContainsRune(delimiterChars, char) {205+ c = charDelimiter206+ }207+ asciiCharClasses[i] = c208+ }209 return true210 }211 212 func posArray(withPos bool, len int) *[]int {19 unmodified lines232 }233 return offset, make([]int32, size)234 }235 217-func charClassOfAscii(char rune) charClass {218- if char >= 'a' && char <= 'z' {219- return charLower220- } else if char >= 'A' && char <= 'Z' {221- return charUpper222- } else if char >= '0' && char <= '9' {223- return charNumber224- } else if strings.ContainsRune(whiteChars, char) {225- return charWhite226- } else if strings.ContainsRune(delimiterChars, char) {227- return charDelimiter228- }229- return charNonWord230-}231-236 func charClassOfNonAscii(char rune) charClass {237 if unicode.IsLower(char) {238 return charLower239 } else if unicode.IsUpper(char) {11 unmodified lines251 }252 253 func charClassOf(char rune) charClass {254 if char <= unicode.MaxASCII {251- return charClassOfAscii(char)255+ return asciiCharClasses[char]256 }257 return charClassOfNonAscii(char)258 }259 190 unmodified lines450 H0sub, C0sub, Bsub := H0[idx:][:len(Tsub)], C0[idx:][:len(Tsub)], B[idx:][:len(Tsub)]451 for off, char := range Tsub {452 var class charClass453 if char <= unicode.MaxASCII {450- class = charClassOfAscii(char)454+ class = asciiCharClasses[char]455 if !caseSensitive && class == charUpper {456 char += 32457+ Tsub[off] = char458 }459 } else {460 class = charClassOfNonAscii(char)461 if !caseSensitive && class == charUpper {1 unmodified line463 }464 if normalize {465 char = normalizeRune(char)466 }467+ Tsub[off] = char468 }469 464- Tsub[off] = char470 bonus := bonusFor(prevClass, class)471 Bsub[off] = bonus472 prevClass = class473 483 unmodified linesmodified:src/options.go+1 added-3 removed
2257 unmodified lines2258 theme.Cursor = boldify(theme.Cursor)2259 theme.Spinner = boldify(theme.Spinner)2260 }2261 2262- if opts.Scheme != "default" {2263- processScheme(opts)2262+ processScheme(opts)2264- }2263 }2264 2265 func expectsArbitraryString(opt string) bool {2266 switch opt {57 unmodified lines