]> Cypherpunks repositories - gostls13.git/commitdiff
strings: remove allocations in Split(s, "")
authorEwan Chou <coocood@gmail.com>
Wed, 6 Mar 2013 20:21:19 +0000 (15:21 -0500)
committerRuss Cox <rsc@golang.org>
Wed, 6 Mar 2013 20:21:19 +0000 (15:21 -0500)
BenchmarkSplit1     77984460     24131380  -69.06%

R=golang-dev, rsc, minux.ma, dave, extemporalgenome
CC=golang-dev
https://golang.org/cl/7458043

src/pkg/strings/strings.go
src/pkg/strings/strings_test.go

index ccf415e6949899bb160ae9092514c152c8ae2ea4..263fa02babdaa0315bb0e3b9c6a5fd229ce7e602 100644 (file)
@@ -26,7 +26,11 @@ func explode(s string, n int) []string {
        i, cur := 0, 0
        for ; i+1 < n; i++ {
                ch, size = utf8.DecodeRuneInString(s[cur:])
-               a[i] = string(ch)
+               if ch == utf8.RuneError {
+                       a[i] = string(utf8.RuneError)
+               } else {
+                       a[i] = s[cur : cur+size]
+               }
                cur += size
        }
        // add the rest, if there is any
index 09de49e5fbc73fc70d73643c0846b2ead8a99f66..68b658ca466612e423c3f598b317d8d1ae72af2f 100644 (file)
@@ -1095,3 +1095,21 @@ func BenchmarkFieldsFunc(b *testing.B) {
                FieldsFunc(fieldsInput, unicode.IsSpace)
        }
 }
+
+func BenchmarkSplit1(b *testing.B) {
+       for i := 0; i < b.N; i++ {
+               Split(benchInputHard, "")
+       }
+}
+
+func BenchmarkSplit2(b *testing.B) {
+       for i := 0; i < b.N; i++ {
+               Split(benchInputHard, "/")
+       }
+}
+
+func BenchmarkSplit3(b *testing.B) {
+       for i := 0; i < b.N; i++ {
+               Split(benchInputHard, "hello")
+       }
+}