Skip to content

Commit 110ab1a

Browse files
committed
slices: document two oddities
Fixes #70935 Change-Id: Idf4a38a05ba595d616b6469a14419ff873bbd354 Reviewed-on: https://go-review.googlesource.com/c/go/+/638095 Reviewed-by: Keith Randall <[email protected]> LUCI-TryBot-Result: Go LUCI <[email protected]> Reviewed-by: Keith Randall <[email protected]> Reviewed-by: Ian Lance Taylor <[email protected]>
1 parent 669d87a commit 110ab1a

File tree

1 file changed

+4
-0
lines changed

1 file changed

+4
-0
lines changed

src/slices/slices.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -414,6 +414,7 @@ func Grow[S ~[]E, E any](s S, n int) S {
414414
panic("cannot be negative")
415415
}
416416
if n -= cap(s) - len(s); n > 0 {
417+
// This expression allocates only once (see test).
417418
s = append(s[:cap(s)], make([]E, n)...)[:len(s)]
418419
}
419420
return s
@@ -483,6 +484,9 @@ func Concat[S ~[]E, E any](slices ...S) S {
483484
panic("len out of range")
484485
}
485486
}
487+
// Use Grow, not make, to round up to the size class:
488+
// the extra space is otherwise unused and helps
489+
// callers that append a few elements to the result.
486490
newslice := Grow[S](nil, size)
487491
for _, s := range slices {
488492
newslice = append(newslice, s...)

0 commit comments

Comments
 (0)