diff options
author | Pontus Leitzler <leitzler@gmail.com> | 2023-02-03 15:59:31 +0100 |
---|---|---|
committer | Michael Stapelberg <stapelberg@google.com> | 2023-02-06 09:03:56 +0000 |
commit | 358fe40267afd0cb5380d132cc93ddcbed2beb44 (patch) | |
tree | 00c5619e6e0d11692d49f1b03106cdbbf196355f | |
parent | 0430d694e04ae06ca14b27b50e83674d07e2e662 (diff) | |
download | golang-protobuf-358fe40267afd0cb5380d132cc93ddcbed2beb44.tar.gz |
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 <joetsai@digital-static.net>
Reviewed-by: Damien Neil <dneil@google.com>
Reviewed-by: Michael Stapelberg <stapelberg@google.com>
-rw-r--r-- | internal/strs/strings_unsafe.go | 2 |
1 files changed, 1 insertions, 1 deletions
diff --git a/internal/strs/strings_unsafe.go b/internal/strs/strings_unsafe.go index fea589c4..61a84d34 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 { |