Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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 <[email protected]> Reviewed-by: Damien Neil <[email protected]> Reviewed-by: Michael Stapelberg <[email protected]>
- Loading branch information