aboutsummaryrefslogtreecommitdiff
path: root/src/bytes
diff options
context:
space:
mode:
authorTobias Klauser <tklauser@distanz.ch>2019-03-09 14:11:40 +0100
committerTobias Klauser <tobias.klauser@gmail.com>2019-03-09 14:17:13 +0000
commit0e9d7d430b1aa74a58054a6a69aa3fb37353168d (patch)
tree4959d9f1112dc72296336b6259c00f0b905842fe /src/bytes
parentca8354843ef9f30207efd0a40bb6c53e7ba86892 (diff)
downloadgo-0e9d7d430b1aa74a58054a6a69aa3fb37353168d.tar.gz
go-0e9d7d430b1aa74a58054a6a69aa3fb37353168d.zip
bytes: return early in Repeat if count is 0
This matches the implementation of strings.Repeat and slightly increases performance: name old time/op new time/op delta Repeat-8 145ns ±12% 125ns ±29% -13.35% (p=0.009 n=10+10) Change-Id: Ic0a0e2ea9e36591286a49def320ddb67fe0b2c50 Reviewed-on: https://go-review.googlesource.com/c/go/+/166399 Run-TryBot: Tobias Klauser <tobias.klauser@gmail.com> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
Diffstat (limited to 'src/bytes')
-rw-r--r--src/bytes/bytes.go5
1 files changed, 4 insertions, 1 deletions
diff --git a/src/bytes/bytes.go b/src/bytes/bytes.go
index f65bf214cc..6fcebe6593 100644
--- a/src/bytes/bytes.go
+++ b/src/bytes/bytes.go
@@ -499,13 +499,16 @@ func Map(mapping func(r rune) rune, s []byte) []byte {
// It panics if count is negative or if
// the result of (len(b) * count) overflows.
func Repeat(b []byte, count int) []byte {
+ if count == 0 {
+ return []byte{}
+ }
// Since we cannot return an error on overflow,
// we should panic if the repeat will generate
// an overflow.
// See Issue golang.org/issue/16237.
if count < 0 {
panic("bytes: negative Repeat count")
- } else if count > 0 && len(b)*count/count != len(b) {
+ } else if len(b)*count/count != len(b) {
panic("bytes: Repeat count causes overflow")
}