Skip to content

Commit d2f002c

Browse files
korzhaotoothrot
authored andcommitted
time/format: avoid growslice in time.String()/time.GoString()
Pre-allocate the slice of buf with enough capacity to avoid growslice calls. benchmark old ns/op new ns/op delta BenchmarkTimeString-4 493 409 -17.12% BenchmarkTimeGoString-4 309 182 -41.30% benchmark old allocs new allocs delta BenchmarkTimeString-4 5 3 -40.00% BenchmarkTimeGoString-4 4 1 -75.00% benchmark old bytes new bytes delta BenchmarkTimeString-4 152 128 -15.79% BenchmarkTimeGoString-4 248 80 -67.74% Change-Id: I64eabe2ab0b3d4a846453c2e8e548a831d720b8c Reviewed-on: https://go-review.googlesource.com/c/go/+/343971 Run-TryBot: Ian Lance Taylor <[email protected]> TryBot-Result: Go Bot <[email protected]> Reviewed-by: Ian Lance Taylor <[email protected]> Trust: Alexander Rakoczy <[email protected]>
1 parent 08d4cc2 commit d2f002c

File tree

1 file changed

+3
-2
lines changed

1 file changed

+3
-2
lines changed

src/time/format.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -479,7 +479,7 @@ func (t Time) String() string {
479479
}
480480
m1, m2 := m2/1e9, m2%1e9
481481
m0, m1 := m1/1e9, m1%1e9
482-
var buf []byte
482+
buf := make([]byte, 0, 24)
483483
buf = append(buf, " m="...)
484484
buf = append(buf, sign)
485485
wid := 0
@@ -498,7 +498,8 @@ func (t Time) String() string {
498498
// GoString implements fmt.GoStringer and formats t to be printed in Go source
499499
// code.
500500
func (t Time) GoString() string {
501-
buf := []byte("time.Date(")
501+
buf := make([]byte, 0, 70)
502+
buf = append(buf, "time.Date("...)
502503
buf = appendInt(buf, t.Year(), 0)
503504
month := t.Month()
504505
if January <= month && month <= December {

0 commit comments

Comments
 (0)