[perf] Remove memory copy when using string delimiter
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 ddc7bb9 · Browse files · 3 files·+67 added−6 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+67 added−6 removed
- other:+67 added−6 removed
Provenance
- Commit
d9c8a9a- Tree
98a2018- Author
Junegunn Choi <junegunn.c@gmail.com>- Committer
Junegunn Choi <junegunn.c@gmail.com>- Parent
ddc7bb9
3 files changed+67 added−6 removed
modified:src/util/chars_test.go+25 added
53 unmodified lines54 check("h o", 5)55 check(" h o ", 5)56 check(" ", 0)57 }58+59+func TestSplit(t *testing.T) {60+ check := func(str string, delim string, tokens ...string) {61+ input := ToChars([]byte(str))62+ result := input.Split(delim)63+ if len(result) != len(tokens) {64+ t.Errorf("Invalid Split result for '%s': %d tokens found (expected %d): %s",65+ str, len(result), len(tokens), result)66+ }67+ for idx, token := range tokens {68+ if result[idx].ToString() != token {69+ t.Errorf("Invalid Split result for '%s': %s (expected %s)",70+ str, result[idx].ToString(), token)71+ }72+ }73+ }74+ check("abc:def::", ":", "abc:", "def:", ":")75+ check("abc:def::", "-", "abc:def::")76+ check("abc", "", "a", "b", "c")77+ check("abc", "a", "a", "bc")78+ check("abc", "ab", "ab", "c")79+ check("abc", "abc", "abc")80+ check("abc", "abcd", "abc")81+ check("", "abcd", "")82+}modified:src/util/chars.go+36 added
116 unmodified lines117 return Chars{runes: chars.runes[b:e]}118 }119 return Chars{bytes: chars.bytes[b:e]}120 }121+122+func (chars *Chars) Split(delimiter string) []Chars {123+ delim := []rune(delimiter)124+ numChars := chars.Length()125+ numDelim := len(delim)126+ begin := 0127+ ret := make([]Chars, 0, 1)128+129+ for index := 0; index < numChars; {130+ if index+numDelim <= numChars {131+ match := true132+ for off, d := range delim {133+ if chars.Get(index+off) != d {134+ match = false135+ break136+ }137+ }138+ // Found the delimiter139+ if match {140+ incr := Max(numDelim, 1)141+ ret = append(ret, chars.Slice(begin, index+incr))142+ index += incr143+ begin = index144+ continue145+ }146+ } else {147+ // Impossible to find the delimiter in the remaining substring148+ break149+ }150+ index++151+ }152+ if begin < numChars || len(ret) == 0 {153+ ret = append(ret, chars.Slice(begin, numChars))154+ }155+ return ret156+}modified:src/tokenizer.go+6 added-6 removed
138 unmodified lines139 tokens, prefixLength := awkTokenizer(text)140 return withPrefixLengths(tokens, prefixLength)141 }142 143- var tokens []string143 if delimiter.str != nil {145- tokens = strings.Split(text.ToString(), *delimiter.str)146- for i := 0; i < len(tokens)-1; i++ {147- tokens[i] = tokens[i] + *delimiter.str148- }149- } else if delimiter.regex != nil {144+ return withPrefixLengths(text.Split(*delimiter.str), 0)145+ }146+147+ // FIXME performance148+ var tokens []string149+ if delimiter.regex != nil {150 str := text.ToString()151 for len(str) > 0 {152 loc := delimiter.regex.FindStringIndex(str)153 if loc == nil {103 unmodified lines