transport: avoid allocation when releasing shared write buffers#9233
Open
zaidoon1 wants to merge 1 commit into
Open
transport: avoid allocation when releasing shared write buffers#9233zaidoon1 wants to merge 1 commit into
zaidoon1 wants to merge 1 commit into
Conversation
Shared write buffers currently copy the slice header into a local variable and pass its address to the pool on every flush. The local escapes to the heap, adding one allocation to each write and flush cycle. Keep the pointer returned by the pool while the buffer is in use and return that same pointer after flushing. Also release it when a network write fails before the normal Flush path.
|
|
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## master #9233 +/- ##
==========================================
- Coverage 83.30% 83.23% -0.08%
==========================================
Files 421 421
Lines 34061 34066 +5
==========================================
- Hits 28375 28354 -21
- Misses 4261 4275 +14
- Partials 1425 1437 +12
🚀 New features to boost your workflow:
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Shared write buffers currently add one heap allocation whenever
bufWriterreturns 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:
bufWriter.Flushcopies 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
Flushpath.The benchmark added in this change can be run with:
go test ./internal/transport -run '^$' -bench '^BenchmarkBufWriter/Shared/WriteAndFlush$' -benchmem -count=10Results on an Apple M4 Pro with Go 1.26.5:
Tests run:
RELEASE NOTES: