Commit 7bcbb65d authored by Rui Ueyama's avatar Rui Ueyama Committed by Josh Bleecher Snyder

bytes, strings: optimize Repeat

Call copy with as large buffer as possible to reduce the
number of function calls.

benchmark                 old ns/op    new ns/op    delta
BenchmarkBytesRepeat            540          162  -70.00%
BenchmarkStringsRepeat          563          177  -68.56%

LGTM=josharian
R=golang-codereviews, josharian, dave, dvyukov
CC=golang-codereviews
https://golang.org/cl/90550043
parent 22a5d2cc
...@@ -377,9 +377,10 @@ func Map(mapping func(r rune) rune, s []byte) []byte { ...@@ -377,9 +377,10 @@ func Map(mapping func(r rune) rune, s []byte) []byte {
// Repeat returns a new byte slice consisting of count copies of b. // Repeat returns a new byte slice consisting of count copies of b.
func Repeat(b []byte, count int) []byte { func Repeat(b []byte, count int) []byte {
nb := make([]byte, len(b)*count) nb := make([]byte, len(b)*count)
bp := 0 bp := copy(nb, b)
for i := 0; i < count; i++ { for bp < len(nb) {
bp += copy(nb[bp:], b) copy(nb[bp:], nb[:bp])
bp *= 2
} }
return nb return nb
} }
......
...@@ -1232,3 +1232,9 @@ func BenchmarkTrimSpace(b *testing.B) { ...@@ -1232,3 +1232,9 @@ func BenchmarkTrimSpace(b *testing.B) {
TrimSpace(s) TrimSpace(s)
} }
} }
func BenchmarkRepeat(b *testing.B) {
for i := 0; i < b.N; i++ {
Repeat([]byte("-"), 80)
}
}
...@@ -423,9 +423,10 @@ func Map(mapping func(rune) rune, s string) string { ...@@ -423,9 +423,10 @@ func Map(mapping func(rune) rune, s string) string {
// Repeat returns a new string consisting of count copies of the string s. // Repeat returns a new string consisting of count copies of the string s.
func Repeat(s string, count int) string { func Repeat(s string, count int) string {
b := make([]byte, len(s)*count) b := make([]byte, len(s)*count)
bp := 0 bp := copy(b, s)
for i := 0; i < count; i++ { for bp < len(b) {
bp += copy(b[bp:], s) copy(b[bp:], b[:bp])
bp *= 2
} }
return string(b) return string(b)
} }
......
...@@ -1174,3 +1174,9 @@ func BenchmarkSplit3(b *testing.B) { ...@@ -1174,3 +1174,9 @@ func BenchmarkSplit3(b *testing.B) {
Split(benchInputHard, "hello") Split(benchInputHard, "hello")
} }
} }
func BenchmarkRepeat(b *testing.B) {
for i := 0; i < b.N; i++ {
Repeat("-", 80)
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment