diff options
| author | Joe Tsai <joetsai@digital-static.net> | 2023-10-01 12:15:14 -0700 |
|---|---|---|
| committer | Joseph Tsai <joetsai@digital-static.net> | 2024-03-18 19:36:37 +0000 |
| commit | 3c78ace24f3aa025a72b53be3b83423f9f24ee5d (patch) | |
| tree | 4aaac37c3a289ca54d650f41154888ddf82fe663 /src/strings/strings.go | |
| parent | 0d7afc2ebff781c2f3100177d26ed0c3b56247c7 (diff) | |
| download | go-3c78ace24f3aa025a72b53be3b83423f9f24ee5d.tar.xz | |
strings: optimize Repeat for common substrings
According to static analysis of Go source code known by the module proxy,
spaces, dashes, zeros, and tabs are the most commonly repeated string literals.
Out of ~69k total calls to Repeat:
* ~25k calls are repeats of " "
* ~7k calls are repeats of "-"
* ~4k calls are repeats of "0"
* ~2k calls are repeats of "="
* ~2k calls are repeats of "\t"
After this optimization, ~60% of Repeat calls will go through the fast path.
These are often used in padding of fixed-width terminal UI or
in the presentation of humanly readable text
(e.g., indentation made of spaces or tabs).
Optimize for this case by handling short repeated sequences of common literals.
Performance:
name old time/op new time/op delta
RepeatSpaces-24 19.3ns ± 1% 5.0ns ± 1% -74.27% (p=0.000 n=8+9)
name old alloc/op new alloc/op delta
RepeatSpaces-24 2.00B ± 0% 0.00B -100.00% (p=0.000 n=10+10)
name old allocs/op new allocs/op delta
RepeatSpaces-24 1.00 ± 0% 0.00 -100.00% (p=0.000 n=10+10)
Change-Id: Id1cafd0cc509e835c8241a626489eb206e0adc3c
Reviewed-on: https://go-review.googlesource.com/c/go/+/536615
Reviewed-by: Ian Lance Taylor <iant@google.com>
Reviewed-by: Emmanuel Odeke <emmanuel@orijtech.com>
Reviewed-by: Than McIntosh <thanm@google.com>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Diffstat (limited to 'src/strings/strings.go')
| -rw-r--r-- | src/strings/strings.go | 38 |
1 files changed, 38 insertions, 0 deletions
diff --git a/src/strings/strings.go b/src/strings/strings.go index f3f0723721..f53ae1f9a7 100644 --- a/src/strings/strings.go +++ b/src/strings/strings.go @@ -530,6 +530,27 @@ func Map(mapping func(rune) rune, s string) string { return b.String() } +// According to static analysis, spaces, dashes, zeros, equals, and tabs +// are the most commonly repeated string literal, +// often used for display on fixed-width terminal windows. +// Pre-declare constants for these for O(1) repetition in the common-case. +const ( + repeatedSpaces = "" + + " " + + " " + repeatedDashes = "" + + "----------------------------------------------------------------" + + "----------------------------------------------------------------" + repeatedZeroes = "" + + "0000000000000000000000000000000000000000000000000000000000000000" + repeatedEquals = "" + + "================================================================" + + "================================================================" + repeatedTabs = "" + + "\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t" + + "\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t" +) + // Repeat returns a new string consisting of count copies of the string s. // // It panics if count is negative or if the result of (len(s) * count) @@ -557,6 +578,23 @@ func Repeat(s string, count int) string { return "" } + // Optimize for commonly repeated strings of relatively short length. + switch s[0] { + case ' ', '-', '0', '=', '\t': + switch { + case n <= len(repeatedSpaces) && HasPrefix(repeatedSpaces, s): + return repeatedSpaces[:n] + case n <= len(repeatedDashes) && HasPrefix(repeatedDashes, s): + return repeatedDashes[:n] + case n <= len(repeatedZeroes) && HasPrefix(repeatedZeroes, s): + return repeatedZeroes[:n] + case n <= len(repeatedEquals) && HasPrefix(repeatedEquals, s): + return repeatedEquals[:n] + case n <= len(repeatedTabs) && HasPrefix(repeatedTabs, s): + return repeatedTabs[:n] + } + } + // Past a certain chunk size it is counterproductive to use // larger chunks as the source of the write, as when the source // is too large we are basically just thrashing the CPU D-cache. |
