crypto/internal/boring: replace slice growth loop with slices.Grow

If dst is nil and len(plaintext) is 10MB, the slice growth loop
allocates about 70MB. slices.Grow only allocates 10MB.
Noticed on a real profile in a program built with boringcrypto.

Change-Id: Iecc727b961273d4f2d553669d43014c507e25df8
Reviewed-on: https://go-review.googlesource.com/c/go/+/744400
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Reviewed-by: Roland Shoemaker <roland@golang.org>
This commit is contained in:
Russ Cox
2026-02-11 10:25:42 -05:00
parent 89d92fc211
commit 753022f82f

View File

@@ -48,6 +48,7 @@ import (
"crypto/cipher"
"errors"
"runtime"
"slices"
"strconv"
"unsafe"
)
@@ -323,9 +324,7 @@ func (g *aesGCM) Seal(dst, nonce, plaintext, additionalData []byte) []byte {
// Make room in dst to append plaintext+overhead.
n := len(dst)
for cap(dst) < n+len(plaintext)+gcmTagSize {
dst = append(dst[:cap(dst)], 0)
}
dst = slices.Grow(dst, len(plaintext)+gcmTagSize)
dst = dst[:n+len(plaintext)+gcmTagSize]
// Check delayed until now to make sure len(dst) is accurate.