Skip to content

Commit

Permalink
internal/strs: avoid unnecessary allocations in Builder
Browse files Browse the repository at this point in the history
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 <[email protected]>
Reviewed-by: Damien Neil <[email protected]>
Reviewed-by: Michael Stapelberg <[email protected]>
  • Loading branch information
leitzler authored and stapelberg committed Feb 6, 2023
1 parent 0430d69 commit 358fe40
Showing 1 changed file with 1 addition and 1 deletion.
2 changes: 1 addition & 1 deletion internal/strs/strings_unsafe.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit 358fe40

Please sign in to comment.