transport: avoid allocation when releasing shared write buffers (#9233)
Shared write buffers currently add one heap allocation whenever `bufWriter` returns a buffer to the pool. This PR removes that allocation. It does not change when buffers are shared or released, and it does not change Go's GC pacing. After upgrading a service with many long lived gRPC connections and streaming responses, we saw more time spent in garbage collection. The investigation found two separate effects: 1. Shared write buffers reduce retained heap, which can cause Go to collect more frequently depending on the application's GC settings. 2. `bufWriter.Flush` copies the buffer's slice header into a local variable and passes its address to the pool. That local variable escapes to the heap, adding one 24-byte allocation to every write and flush cycle. This PR only fixes the second issue. The writer now keeps the pointer returned by the pool while the buffer is in use and returns that same pointer after flushing. It also releases the buffer when a network write fails before the normal `Flush` path. The benchmark added in this change can be run with: ```console go test ./internal/transport -run '^$' -bench '^BenchmarkBufWriter/Shared/WriteAndFlush$' -benchmem -count=10 ``` Results on an Apple M4 Pro with Go 1.26.5: ```text before: 24.2-26.2 ns/op, 24 B/op, 1 alloc/op after: 13.5-13.9 ns/op, 0 B/op, 0 alloc/op ``` Tests run: ```console go test -cpu 1,4 -timeout 7m ./internal/transport go test -race -cpu 1,4 -timeout 7m ./internal/transport ``` RELEASE NOTES: * transport: Avoid a heap allocation when flushing the write buffer.
Z
Zaidoon Abd Al Hadi committed
0e45140ef07a51fd746da380e6d7ab60add24088
Parent: 6f508fa
Committed by GitHub <noreply@github.com>
on 7/31/2026, 9:59:46 AM