Skip to content

perf: optionally pool stream writer buffers - #171

Open
jessy-ddog wants to merge 1 commit into
1.xfrom
jessy.chenavas/optional-writer-buffer-pool
Open

perf: optionally pool stream writer buffers#171
jessy-ddog wants to merge 1 commit into
1.xfrom
jessy.chenavas/optional-writer-buffer-pool

Conversation

@jessy-ddog

Copy link
Copy Markdown

Summary

Adds opt-in destination buffer pooling for stream writers. Pooling remains disabled by default.

Purpose

gRPC creates one zstd writer per message. Each writer allocates a destination buffer sized for the input, which creates significant allocation pressure at high throughput.

Changes Made

  • Add a concurrency-safe switch for writer buffer pooling.
  • Reuse destination buffers through sync.Pool when enabled.
  • Return buffers on every Close path.
  • Add a focused test and allocation benchmark.

Impact & Blast Radius

  • Affected Components: Stream writers created after SetWriterBufferPoolEnabled(true).
  • Risk: Low. Existing behavior remains the default.
  • Potential Side Effects: Enabled pooling can retain recently used buffers until a garbage collection clears the pool.

How to Test & Measure

  • go test ./... -count=1
  • go test -race ./... -run ^TestWriterBufferPool -count=1
  • go build -tags external_libzstd ./...
  • go test -run ^ -bench ^BenchmarkStreamWriterBufferPool -benchmem -count=3

For a 70 KB payload, pooling reduced allocations from about 75,016 B/op to 139 B/op and from 4 allocs/op to 2 allocs/op.

Checklist

  • I have self-reviewed my code.

@jessy-ddog
jessy-ddog marked this pull request as ready for review July 30, 2026 09:38
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant