Minor optimization of FuzzyMatchV2
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Calculate the first row of the score matrix during phase 2
authored by Junegunn Choi · committed by Junegunn Choi · parent 6aae122 · Browse files · 1 file·+52 added−37 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
1 file+52 added−37 removed
- other:+52 added−37 removed
Provenance
- Commit
941b0a0- Tree
8727484- Author
Junegunn Choi <junegunn.c@gmail.com>- Committer
Junegunn Choi <junegunn.c@gmail.com>- Parent
6aae122
1 file changed+52 added−37 removed
modified:src/algo/algo.go+52 added-37 removed
358 unmodified lines359 360 // Reuse pre-allocated integer slice to avoid unnecessary sweeping of garbages361 offset16 := 0362 offset32 := 0363+ offset16, H0 := alloc16(offset16, slab, N)364+ offset16, C0 := alloc16(offset16, slab, N)365 // Bonus point for each position366 offset16, B := alloc16(offset16, slab, N)367 // The first occurrence of each character in the pattern368 offset32, F := alloc32(offset32, slab, M)369 // Rune array370 offset32, T := alloc32(offset32, slab, N)371+ input.CopyRunes(T)372 373 // Phase 2. Calculate bonus for each point371- pidx, lastIdx, prevClass := 0, 0, charNonWord372- input.CopyRunes(T)373- for ; idx < N; idx++ {374+ maxScore, maxScorePos := int16(0), 0375+ pidx, lastIdx := 0, 0376+ for pchar0, prevClass, inGap := pattern[0], charNonWord, false; idx < N; idx++ {377 char := T[idx]378 var class charClass379 if char <= unicode.MaxASCII {380 class = charClassOfAscii(char)13 unmodified lines394 char = normalizeRune(char)395 }396 397 T[idx] = char395- B[idx] = bonusFor(prevClass, class)398+ bonus := bonusFor(prevClass, class)399+ B[idx] = bonus400 prevClass = class401 398- if pidx < M {399- if char == pattern[pidx] {400- lastIdx = idx402+ if char == pattern[util.Min(pidx, M-1)] {403+ if pidx < M {404 F[pidx] = int32(idx)405 pidx++406 }407+ lastIdx = idx408+ }409+410+ if char == pchar0 {411+ score := scoreMatch + bonus*bonusFirstCharMultiplier412+ H0[idx] = score413+ C0[idx] = 1414+ if M == 1 && (forward && score > maxScore || !forward && score >= maxScore) {415+ maxScore, maxScorePos = score, idx416+ if forward && bonus == bonusBoundary {417+ break418+ }419+ }420+ inGap = false421 } else {405- if char == pattern[M-1] {406- lastIdx = idx422+ if idx == 0 {423+ H0[idx] = 0424+ } else if inGap {425+ H0[idx] = util.Max16(H0[idx-1]+scoreGapExtention, 0)426+ } else {427+ H0[idx] = util.Max16(H0[idx-1]+scoreGapStart, 0)428 }429+ C0[idx] = 0430+ inGap = true431 }432 }433 if pidx != M {434 return Result{-1, -1, 0}, nil435 }413- if M == 1 && B[F[0]] == bonusBoundary {414- p := int(F[0])415- result := Result{p, p + 1, scoreMatch + bonusBoundary*bonusFirstCharMultiplier}436+ if M == 1 {437+ result := Result{maxScorePos, maxScorePos + 1, int(maxScore)}438 if !withPos {439 return result, nil440 }419- pos := []int{p}441+ pos := []int{maxScorePos}442 return result, &pos443 }444 445 // Phase 3. Fill in score matrix (H)446 // Unlike the original algorithm, we do not allow omission.447+ f0 := int(F[0])425- width := lastIdx - int(F[0]) + 1448+ width := lastIdx - f0 + 1449 offset16, H := alloc16(offset16, slab, width*M)450+ copy(H, H0[f0:lastIdx+1])451 452 // Possible length of consecutive chunk at each position.453 offset16, C := alloc16(offset16, slab, width*M)454+ copy(C, C0[f0:lastIdx+1])455 431- maxScore, maxScorePos := int16(0), 0432- for i := 0; i < M; i++ {456+ for i := 1; i < M; i++ {457 I := i * width458+ f := int(F[i])459 inGap := false435- for j := int(F[i]); j <= lastIdx; j++ {436- j0 := j - int(F[0])460+ for j := f; j <= lastIdx; j++ {461+ j0 := j - f0462 var s1, s2, consecutive int16463 439- if j > int(F[i]) {464+ if j > f {465 if inGap {466 s2 = H[I+j0-1] + scoreGapExtention467 } else {468 s2 = H[I+j0-1] + scoreGapStart469 }470 }471 472 if pattern[i] == T[j] {448- var diag int16449- if i > 0 && j0 > 0 {450- diag = H[I-width+j0-1]451- }452- s1 = diag + scoreMatch473+ s1 = H[I-width+j0-1] + scoreMatch474 b := B[j]454- if i > 0 {455- // j > 0 if i > 0456- consecutive = C[I-width+j0-1] + 1457- // Break consecutive chunk458- if b == bonusBoundary {475+ consecutive = C[I-width+j0-1] + 1476+ // Break consecutive chunk477+ if b == bonusBoundary {459- consecutive = 1460- } else if consecutive > 1 {461- b = util.Max16(b, util.Max16(bonusConsecutive, B[j-int(consecutive)+1]))462- }463- } else {478 consecutive = 1465- b *= bonusFirstCharMultiplier479+ } else if consecutive > 1 {480+ b = util.Max16(b, util.Max16(bonusConsecutive, B[j-int(consecutive)+1]))481 }482 if s1+b < s2 {483 s1 += B[j]484 consecutive = 017 unmodified lines502 }503 504 // Phase 4. (Optional) Backtrace to find character positions505 pos := posArray(withPos, M)491- j := int(F[0])506+ j := f0507 if withPos {508 i := M - 1509 j = maxScorePos510 preferMatch := true511 for {512 I := i * width498- j0 := j - int(F[0])513+ j0 := j - f0514 s := H[I+j0]515 516 var s1, s2 int16517 if i > 0 && j >= int(F[i]) {329 unmodified lines