Remove bound checkings in inner loops
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Junegunn Choi · committed by Junegunn Choi · parent 55ee418 · Browse files · 1 file·+54 added−38 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+54 added−38 removed
- other:+54 added−38 removed
Provenance
- Commit
71fdb99- Tree
4aad848- Author
Junegunn Choi <junegunn.c@gmail.com>- Committer
Junegunn Choi <junegunn.c@gmail.com>- Parent
55ee418
1 file changed+54 added−38 removed
modified:src/algo/algo.go+54 added-38 removed
326 unmodified lines327 for idx, p := range C[I : I+width] {328 if idx+int(F[0]) < int(F[i]) {329 p = 0330 }331+ if p > 0 {331- fmt.Printf("%2d ", p)332+ fmt.Printf("%2d ", p)333+ } else {334+ fmt.Print(" ")335+ }336 }337 fmt.Println()338 }339 }36 unmodified lines376 377 // Phase 2. Calculate bonus for each point378 maxScore, maxScorePos := int16(0), 0379 pidx, lastIdx := 0, 0376- for pchar0, prevClass, inGap := pattern[0], charNonWord, false; idx < N; idx++ {377- char := T[idx]380+ pchar0, pchar, prevH0, prevClass, inGap := pattern[0], pattern[0], int16(0), charNonWord, false381+ Tsub := T[idx:]382+ H0sub, C0sub, Bsub := H0[idx:][:len(Tsub)], C0[idx:][:len(Tsub)], B[idx:][:len(Tsub)]383+ for off, char := range Tsub {384 var class charClass385 if char <= unicode.MaxASCII {386 class = charClassOfAscii(char)387 } else {11 unmodified lines399 if normalize {400 char = normalizeRune(char)401 }402 397- T[idx] = char403+ Tsub[off] = char404 bonus := bonusFor(prevClass, class)399- B[idx] = bonus405+ Bsub[off] = bonus406 prevClass = class407 402- if char == pattern[util.Min(pidx, M-1)] {408+ if char == pchar {409 if pidx < M {404- F[pidx] = int32(idx)410+ F[pidx] = int32(idx + off)411 pidx++412+ pchar = pattern[util.Min(pidx, M-1)]413 }407- lastIdx = idx414+ lastIdx = idx + off415 }416 417 if char == pchar0 {418 score := scoreMatch + bonus*bonusFirstCharMultiplier412- H0[idx] = score413- C0[idx] = 1419+ H0sub[off] = score420+ C0sub[off] = 1421 if M == 1 && (forward && score > maxScore || !forward && score >= maxScore) {415- maxScore, maxScorePos = score, idx422+ maxScore, maxScorePos = score, idx+off423 if forward && bonus == bonusBoundary {424 break425 }426 }427 inGap = false428 } else {422- if idx == 0 {423- H0[idx] = 0424- } else if inGap {425- H0[idx] = util.Max16(H0[idx-1]+scoreGapExtention, 0)429+ if inGap {430+ H0sub[off] = util.Max16(prevH0+scoreGapExtention, 0)431 } else {427- H0[idx] = util.Max16(H0[idx-1]+scoreGapStart, 0)432+ H0sub[off] = util.Max16(prevH0+scoreGapStart, 0)433 }429- C0[idx] = 0434+ C0sub[off] = 0435 inGap = true436 }437+ prevH0 = H0sub[off]438 }439 if pidx != M {440 return Result{-1, -1, 0}, nil441 }16 unmodified lines458 // Possible length of consecutive chunk at each position.459 offset16, C := alloc16(offset16, slab, width*M)460 copy(C, C0[f0:lastIdx+1])461 456- for i := 1; i < M; i++ {457- I := i * width458- f := int(F[i])462+ Fsub := F[1:]463+ Psub := pattern[1:][:len(Fsub)]464+ for off, f := range Fsub {465+ f := int(f)466+ pchar := Psub[off]467+ pidx := off + 1468+ row := pidx * width469 inGap := false460- for j := f; j <= lastIdx; j++ {461- j0 := j - f0470+ Tsub := T[f : lastIdx+1]471+ Bsub := B[f:][:len(Tsub)]472+ Csub := C[row+f-f0:][:len(Tsub)]473+ Cdiag := C[row+f-f0-1-width:][:len(Tsub)]474+ Hsub := H[row+f-f0:][:len(Tsub)]475+ Hdiag := H[row+f-f0-1-width:][:len(Tsub)]476+ Hleft := H[row+f-f0-1:][:len(Tsub)]477+ Hleft[0] = 0478+ for off, char := range Tsub {479+ col := off + f480 var s1, s2, consecutive int16481 464- if j > f {465- if inGap {466- s2 = H[I+j0-1] + scoreGapExtention467- } else {468- s2 = H[I+j0-1] + scoreGapStart482+ if inGap {483+ s2 = Hleft[off] + scoreGapExtention484+ } else {485+ s2 = Hleft[off] + scoreGapStart469- }486 }487 472- if pattern[i] == T[j] {473- s1 = H[I-width+j0-1] + scoreMatch474- b := B[j]475- consecutive = C[I-width+j0-1] + 1488+ if pchar == char {489+ s1 = Hdiag[off] + scoreMatch490+ b := Bsub[off]491+ consecutive = Cdiag[off] + 1492 // Break consecutive chunk493 if b == bonusBoundary {494 consecutive = 1495 } else if consecutive > 1 {480- b = util.Max16(b, util.Max16(bonusConsecutive, B[j-int(consecutive)+1]))496+ b = util.Max16(b, util.Max16(bonusConsecutive, B[col-int(consecutive)+1]))497 }498 if s1+b < s2 {483- s1 += B[j]499+ s1 += Bsub[off]500 consecutive = 0501 } else {502 s1 += b503 }504 }489- C[I+j0] = consecutive505+ Csub[off] = consecutive506 507 inGap = s1 < s2508 score := util.Max16(util.Max16(s1, s2), 0)493- if i == M-1 && (forward && score > maxScore || !forward && score >= maxScore) {494- maxScore, maxScorePos = score, j509+ if pidx == M-1 && (forward && score > maxScore || !forward && score >= maxScore) {510+ maxScore, maxScorePos = score, col511 }496- H[I+j0] = score512+ Hsub[off] = score513 }514 }515 516 if DEBUG {346 unmodified lines