Skip to content

fix(buffer): preserve chunks for pooling after writes - #447

Open
ViktorBystrov72 wants to merge 1 commit into
mailru:masterfrom
ViktorBystrov72:fix/dump-buffer-pooling
Open

ViktorBystrov72 wants to merge 1 commit into
mailru:masterfrom
ViktorBystrov72:fix/dump-buffer-pooling

Conversation

@ViktorBystrov72

Copy link
Copy Markdown

net.Buffers.WriteTo modifies its slice entries. When those entries share the backing array of Buffer.bufs, the pool-return loop receives nil or shortened slices instead of the original chunks.

Copy the completed-chunk descriptors before writing. The chunk data and vectored-write path stay the same, and chunks are still offered back to the pool only after WriteTo returns. Empty and single-chunk buffers keep the existing path.

Fixes #359.

Local microbenchmark, Go 1.23.0 on darwin/arm64, six samples, 16 KiB written to io.Discard:

Metric Before After
Bytes allocated/op 16,699 946
Allocations/op 10 11

The measured case trades one small descriptor allocation for reuse of the larger chunks. This is a local result, not a universal performance claim; allocation counts for the empty and 128-byte cases were unchanged.

Validation:

  • A diagnostic probe showed original chunk capacities becoming zero before the pool-return loop; the fix preserves them.
  • Partial-write/error tests check byte counts, written prefixes, reset, and reuse. They do not depend on sync.Pool retention across GC.
  • Full make (generation, tests, benchmarks, and golint), go test -race -count=1 ./..., and go vet ./... passed.

Signed-off-by: Виктор Быстров <anatema@bk.ru>
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.

MarshalToWriter does not pool buffers since Go 1.17

1 participant