diff options
| author | Ben Hoyt <benhoyt@gmail.com> | 2018-12-06 08:53:29 -0500 |
|---|---|---|
| committer | Brad Fitzpatrick <bradfitz@golang.org> | 2019-03-12 15:52:17 +0000 |
| commit | 4b4f222a0dd8765e5b493d458fa352ea22045575 (patch) | |
| tree | 8546c03668ded903e08c74935566b16146f5166f /src/strings/strings_test.go | |
| parent | 10aede26d0603c16f6f66c87a84bccfeb2e0c8e0 (diff) | |
| download | go-4b4f222a0dd8765e5b493d458fa352ea22045575.tar.xz | |
bytes, strings: speed up TrimSpace 4-5x for common ASCII cases
This change adds a fast path for ASCII strings to both
strings.TrimSpace and bytes.TrimSpace. It doesn't slow down the
non-ASCII path much, if at all.
I added benchmarks for strings.TrimSpace as it didn't have any, and
I fleshed out the benchmarks for bytes.TrimSpace as it just had one
case (for ASCII). The benchmarks (and the code!) are now the same
between the two versions. Below are the benchmark results:
strings.TrimSpace:
name old time/op new time/op delta
TrimSpace/NoTrim-8 18.6ns ± 0% 3.8ns ± 0% -79.53% (p=0.000 n=5+4)
TrimSpace/ASCII-8 33.5ns ± 2% 6.0ns ± 3% -82.05% (p=0.008 n=5+5)
TrimSpace/SomeNonASCII-8 97.1ns ± 1% 88.6ns ± 1% -8.68% (p=0.008 n=5+5)
TrimSpace/JustNonASCII-8 144ns ± 0% 143ns ± 0% ~ (p=0.079 n=4+5)
bytes.TrimSpace:
name old time/op new time/op delta
TrimSpace/NoTrim-8 18.9ns ± 1% 4.1ns ± 1% -78.34% (p=0.008 n=5+5)
TrimSpace/ASCII-8 29.9ns ± 0% 6.3ns ± 1% -79.06% (p=0.008 n=5+5)
TrimSpace/SomeNonASCII-8 91.5ns ± 0% 82.3ns ± 0% -10.03% (p=0.008 n=5+5)
TrimSpace/JustNonASCII-8 150ns ± 0% 150ns ± 0% ~ (all equal)
Fixes #29122
Change-Id: Ica45cd86a219cadf60173ec9db260133cd1d7951
Reviewed-on: https://go-review.googlesource.com/c/go/+/152917
Reviewed-by: Daniel Martí <mvdan@mvdan.cc>
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
Run-TryBot: Daniel Martí <mvdan@mvdan.cc>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Diffstat (limited to 'src/strings/strings_test.go')
| -rw-r--r-- | src/strings/strings_test.go | 16 |
1 files changed, 16 insertions, 0 deletions
diff --git a/src/strings/strings_test.go b/src/strings/strings_test.go index eee2dd55df..500671aca4 100644 --- a/src/strings/strings_test.go +++ b/src/strings/strings_test.go @@ -1731,3 +1731,19 @@ func BenchmarkJoin(b *testing.B) { }) } } + +func BenchmarkTrimSpace(b *testing.B) { + tests := []struct{ name, input string }{ + {"NoTrim", "typical"}, + {"ASCII", " foo bar "}, + {"SomeNonASCII", " \u2000\t\r\n x\t\t\r\r\ny\n \u3000 "}, + {"JustNonASCII", "\u2000\u2000\u2000☺☺☺☺\u3000\u3000\u3000"}, + } + for _, test := range tests { + b.Run(test.name, func(b *testing.B) { + for i := 0; i < b.N; i++ { + TrimSpace(test.input) + } + }) + } +} |
