[perf] Optimize AWK-style tokenizer for --nth
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Approx. 50% less memory footprint and 40% improvement in query time
authored by Junegunn Choi · committed by Junegunn Choi · parent 1d4057c · Browse files · 5 files·+57 added−59 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
5 files+57 added−59 removed
- other:+57 added−59 removed
Provenance
- Commit
ddc7bb9- Tree
0eb63cd- Author
Junegunn Choi <junegunn.c@gmail.com>- Committer
Junegunn Choi <junegunn.c@gmail.com>- Parent
1d4057c
5 files changed+57 added−59 removed
modified:src/util/chars_test.go+21 added
32 unmodified lines33 if chars.ToString() != text {34 t.Error()35 }36 }37+38+func TestTrimLength(t *testing.T) {39+ check := func(str string, exp int) {40+ chars := ToChars([]byte(str))41+ trimmed := chars.TrimLength()42+ if trimmed != exp {43+ t.Errorf("Invalid TrimLength result for '%s': %d (expected %d)",44+ str, trimmed, exp)45+ }46+ }47+ check("hello", 5)48+ check("hello ", 5)49+ check("hello ", 5)50+ check(" hello", 5)51+ check(" hello", 5)52+ check(" hello ", 5)53+ check(" hello ", 5)54+ check("h o", 5)55+ check(" h o ", 5)56+ check(" ", 0)57+}modified:src/util/chars.go+7 added
109 unmodified lines110 runes[idx] = rune(b)111 }112 return runes113 }114+115+func (chars *Chars) Slice(b int, e int) Chars {116+ if chars.runes != nil {117+ return Chars{runes: chars.runes[b:e]}118+ }119+ return Chars{bytes: chars.bytes[b:e]}120+}modified:src/util/util_test.go-20 removed
18 unmodified lines19 if Constrain(5, -1, 3) != 3 {20 t.Error("Expected", 3)21 }22 }23-24-func TestTrimLen(t *testing.T) {25- check := func(str string, exp int) {26- trimmed := TrimLen([]rune(str))27- if trimmed != exp {28- t.Errorf("Invalid TrimLen result for '%s': %d (expected %d)",29- str, trimmed, exp)30- }31- }32- check("hello", 5)33- check("hello ", 5)34- check("hello ", 5)35- check(" hello", 5)36- check(" hello", 5)37- check(" hello ", 5)38- check(" hello ", 5)39- check("h o", 5)40- check(" h o ", 5)41- check(" ", 0)42-}modified:src/util/util.go-24 removed
81 unmodified lines82 func IsTty() bool {83 return int(C.isatty(C.int(os.Stdin.Fd()))) != 084 }85 86-// TrimLen returns the length of trimmed rune array87-func TrimLen(runes []rune) int {88- var i int89- for i = len(runes) - 1; i >= 0; i-- {90- char := runes[i]91- if char != ' ' && char != '\t' {92- break93- }94- }95- // Completely empty96- if i < 0 {97- return 098- }99-100- var j int101- for j = 0; j < len(runes); j++ {102- char := runes[j]103- if char != ' ' && char != '\t' {104- break105- }106- }107- return i - j + 1108-}109-86 // ExecCommand executes the given command with $SHELL87 func ExecCommand(command string) *exec.Cmd {88 shell := os.Getenv("SHELL")89 if len(shell) == 0 {4 unmodified linesmodified:src/tokenizer.go+29 added-15 removed
95 unmodified lines96 97 func awkTokenizer(input util.Chars) ([]util.Chars, int) {98 // 9, 3299 ret := []util.Chars{}100- str := []rune{}100 prefixLength := 0101 state := awkNil102 numChars := input.Length()103+ begin := 0104+ end := 0105 for idx := 0; idx < numChars; idx++ {106 r := input.Get(idx)107 white := r == 9 || r == 32108 switch state {109 case awkNil:110 if white {111 prefixLength++112 } else {112- state = awkBlack113- str = append(str, r)113+ state, begin, end = awkBlack, idx, idx+1114 }115 case awkBlack:116- str = append(str, r)116+ end = idx + 1117 if white {118 state = awkWhite119 }120 case awkWhite:121 if white {122- str = append(str, r)122+ end = idx + 1123 } else {124- ret = append(ret, util.RunesToChars(str))125- state = awkBlack126- str = []rune{r}124+ ret = append(ret, input.Slice(begin, end))125+ state, begin, end = awkBlack, idx, idx+1126 }127 }128 }130- if len(str) > 0 {131- ret = append(ret, util.RunesToChars(str))129+ if begin < end {130+ ret = append(ret, input.Slice(begin, end))131 }132 return ret, prefixLength133 }134 50 unmodified lines185 func Transform(tokens []Token, withNth []Range) []Token {186 transTokens := make([]Token, len(withNth))187 numTokens := len(tokens)188 for idx, r := range withNth {190- part := []rune{}189+ parts := []util.Chars{}190 minIdx := 0191 if r.begin == r.end {192 idx := r.begin193 if idx == rangeEllipsis {195- part = append(part, joinTokensAsRunes(tokens)...)194+ parts = append(parts, util.RunesToChars(joinTokensAsRunes(tokens)))195 } else {196 if idx < 0 {197 idx += numTokens + 1198 }199 if idx >= 1 && idx <= numTokens {200 minIdx = idx - 1202- part = append(part, tokens[idx-1].text.ToRunes()...)201+ parts = append(parts, tokens[idx-1].text)202 }203 }204 } else {205 var begin, end int18 unmodified lines224 }225 minIdx = util.Max(0, begin-1)226 for idx := begin; idx <= end; idx++ {227 if idx >= 1 && idx <= numTokens {229- part = append(part, tokens[idx-1].text.ToRunes()...)228+ parts = append(parts, tokens[idx-1].text)229 }230 }231 }232+ // Merge multiple parts233+ var merged util.Chars234+ switch len(parts) {235+ case 0:236+ merged = util.RunesToChars([]rune{})237+ case 1:238+ merged = parts[0]239+ default:240+ runes := []rune{}241+ for _, part := range parts {242+ runes = append(runes, part.ToRunes()...)243+ }244+ merged = util.RunesToChars(runes)245+ }246+247 var prefixLength int248 if minIdx < numTokens {249 prefixLength = tokens[minIdx].prefixLength250 } else {251 prefixLength = 0252 }239- transTokens[idx] = Token{util.RunesToChars(part), prefixLength, util.TrimLen(part)}253+ transTokens[idx] = Token{merged, prefixLength, merged.TrimLength()}254 }255 return transTokens256 }