From 358fe40267afd0cb5380d132cc93ddcbed2beb44 Mon Sep 17 00:00:00 2001 From: Pontus Leitzler Date: Fri, 3 Feb 2023 15:59:31 +0100 Subject: [PATCH] internal/strs: avoid unnecessary allocations in Builder The grow() method in func did create a new internal byte slice with a specific size instead of a specific capacity. As the call following grow() is append() we ended up with a slice larger than needed. It affects the memory consumption of programs that import generated protobuf files. As an example with the following: -- go.mod -- module x go 1.20 require cloud.google.com/go/appengine v1.6.0 -- main.go -- package main import _ "cloud.google.com/go/appengine/apiv1/appenginepb" func main() {} Running the following a few times: $ go mod tidy && GODEBUG=inittrace=1 go run . 2>&1 | grep appenginepb Before: init cloud.google.com/go/appengine/apiv1/appenginepb @3.4 ms, 0.23 ms clock, 204624 bytes, 231 allocs init cloud.google.com/go/appengine/apiv1/appenginepb @3.2 ms, 0.17 ms clock, 204688 bytes, 231 allocs init cloud.google.com/go/appengine/apiv1/appenginepb @2.5 ms, 0.15 ms clock, 204400 bytes, 230 allocs init cloud.google.com/go/appengine/apiv1/appenginepb @2.5 ms, 0.16 ms clock, 205552 bytes, 234 allocs After: init cloud.google.com/go/appengine/apiv1/appenginepb @3.3 ms, 0.19 ms clock, 143440 bytes, 226 allocs init cloud.google.com/go/appengine/apiv1/appenginepb @2.7 ms, 0.16 ms clock, 144368 bytes, 229 allocs init cloud.google.com/go/appengine/apiv1/appenginepb @2.8 ms, 0.16 ms clock, 144304 bytes, 229 allocs init cloud.google.com/go/appengine/apiv1/appenginepb @3.1 ms, 0.16 ms clock, 142864 bytes, 224 allocs Change-Id: If4ece5d70d6bd9de8a758cb29ce9dffc741c4951 Reviewed-on: https://go-review.googlesource.com/c/protobuf/+/465115 Reviewed-by: Joseph Tsai Reviewed-by: Damien Neil Reviewed-by: Michael Stapelberg --- internal/strs/strings_unsafe.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/internal/strs/strings_unsafe.go b/internal/strs/strings_unsafe.go index fea589c45..61a84d341 100644 --- a/internal/strs/strings_unsafe.go +++ b/internal/strs/strings_unsafe.go @@ -87,7 +87,7 @@ func (sb *Builder) grow(n int) { // Unlike strings.Builder, we do not need to copy over the contents // of the old buffer since our builder provides no API for // retrieving previously created strings. - sb.buf = make([]byte, 2*(cap(sb.buf)+n)) + sb.buf = make([]byte, 0, 2*(cap(sb.buf)+n)) } func (sb *Builder) last(n int) string {