]> Cypherpunks repositories - gostls13.git/commitdiff
strings: add special cases for Join of 2 and 3 strings
authorBrad Fitzpatrick <bradfitz@golang.org>
Mon, 18 Jul 2016 17:27:21 +0000 (17:27 +0000)
committerBrad Fitzpatrick <bradfitz@golang.org>
Tue, 16 Aug 2016 00:33:15 +0000 (00:33 +0000)
We already had special cases for 0 and 1. Add 2 and 3 for now too.
To be removed if the compiler is improved later (#6714).

This halves the number of allocations and total bytes allocated via
common filepath.Join calls, improving filepath.Walk performance.

Noticed as part of investigating filepath.Walk in #16399.

Change-Id: If7b1bb85606d4720f3ebdf8de7b1e12ad165079d
Reviewed-on: https://go-review.googlesource.com/25005
Reviewed-by: Ian Lance Taylor <iant@golang.org>
Run-TryBot: Brad Fitzpatrick <bradfitz@golang.org>

src/strings/strings.go

index 919e8c8354e55e4d633d4ddb065e1457c62ac89e..738c49303259171f6918643a2d529d5b67bbd375 100644 (file)
@@ -342,11 +342,19 @@ func FieldsFunc(s string, f func(rune) bool) []string {
 // Join concatenates the elements of a to create a single string. The separator string
 // sep is placed between elements in the resulting string.
 func Join(a []string, sep string) string {
-       if len(a) == 0 {
+       switch len(a) {
+       case 0:
                return ""
-       }
-       if len(a) == 1 {
+       case 1:
                return a[0]
+       case 2:
+               // Special case for common small values.
+               // Remove if golang.org/issue/6714 is fixed
+               return a[0] + sep + a[1]
+       case 3:
+               // Special case for common small values.
+               // Remove if golang.org/issue/6714 is fixed
+               return a[0] + sep + a[1] + sep + a[2]
        }
        n := len(sep) * (len(a) - 1)
        for i := 0; i < len(a); i++ {