Skip to content

Commit 3ddc9ad

Browse files
committed
strings: add special cases for Join of 2 and 3 strings
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 <[email protected]> Run-TryBot: Brad Fitzpatrick <[email protected]>
1 parent c88e868 commit 3ddc9ad

File tree

1 file changed

+11
-3
lines changed

1 file changed

+11
-3
lines changed

src/strings/strings.go

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -342,11 +342,19 @@ func FieldsFunc(s string, f func(rune) bool) []string {
342342
// Join concatenates the elements of a to create a single string. The separator string
343343
// sep is placed between elements in the resulting string.
344344
func Join(a []string, sep string) string {
345-
if len(a) == 0 {
345+
switch len(a) {
346+
case 0:
346347
return ""
347-
}
348-
if len(a) == 1 {
348+
case 1:
349349
return a[0]
350+
case 2:
351+
// Special case for common small values.
352+
// Remove if golang.org/issue/6714 is fixed
353+
return a[0] + sep + a[1]
354+
case 3:
355+
// Special case for common small values.
356+
// Remove if golang.org/issue/6714 is fixed
357+
return a[0] + sep + a[1] + sep + a[2]
350358
}
351359
n := len(sep) * (len(a) - 1)
352360
for i := 0; i < len(a); i++ {

0 commit comments

Comments
 (0)