Skip to content
Myr

Skip the normalization map for runes that cannot normalize

Before adoption: this commit came in with the history the project adopted, so no landing records it.

normalizeRune guarded with 0x00C0..0xFF61, which does not exclude Hangul, CJK or Cyrillic, so every rune of those scripts hashed into the map only to miss. Every key of the map folds to ASCII, so the bitmap added for the rune prefilter rejects them without a lookup. - Non-ASCII queries 1.21x where every line is CJK, 1.05x on mostly-ASCII paths behind a Hangul prefix - ASCII queries unchanged, the prefilter already skips Phase 2 for them - Normalization share of query time for a non-ASCII query: 17.2% -> 0%

authored by Junegunn Choi · committed by Junegunn Choi · parent a650900 · Browse files · 2 files+32 added−1 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+32 added−1 removed

  • other:+32 added−1 removed

2 files+32 added−1 removed

modified:src/algo/algo.go+3 added-1 removed
301 unmodified lines
302 return bonusMatrix[charClassOf(input.Get(idx-1))][charClassOf(input.Get(idx))]303}304305func normalizeRune(r rune) rune {306 if r < 0x00C0 || r > 0xFF61 {306 // Every key of the map folds to ASCII, so a rune the bitmap rejects cannot307 // be in it. TestNormalizedKeysAreFlagged pins that.308 if !util.MayFoldToAscii(r) {309 return r310 }311312 n := normalized[r]
987 unmodified lines
modified:src/algo/runeprefilter_test.go+29 added
224 unmodified lines
225 }226 disableRunePrefilter = false227}228229// normalizeRune skips the map when util.MayFoldToAscii rejects the rune. That230// is only sound if every key of the map is flagged, since a flagged-false rune231// is returned unchanged.232func TestNormalizedKeysAreFlagged(t *testing.T) {233 for k := range normalized {234 if !util.MayFoldToAscii(k) {235 t.Errorf("normalized key U+%04X (%c) is not flagged by MayFoldToAscii", k, k)236 }237 }238}239240// Guarding normalizeRune must not change what it returns, for any rune.241func TestNormalizeRuneUnchangedByGuard(t *testing.T) {242 for r := rune(0); r <= unicode.MaxRune; r++ {243 if r >= 0xD800 && r <= 0xDFFF {244 continue245 }246 exp := r247 if r >= 0x00C0 && r <= 0xFF61 {248 if n := normalized[r]; n > 0 {249 exp = n250 }251 }252 if got := normalizeRune(r); got != exp {253 t.Fatalf("normalizeRune(U+%04X) = U+%04X, expected U+%04X", r, got, exp)254 }255 }256}257258// preparePattern mirrors what pattern.go guarantees the algo functions:259// lowercased when case-insensitive, normalized when normalize is on.260func preparePattern(pat string, caseSensitive, normalize bool) []rune {261 if !caseSensitive {
93 unmodified lines