add super.Arena to batch value copies in sbuf.Array#7065
Open
nishantmehta wants to merge 1 commit into
Open
Conversation
sbuf.Array.Write copied every value individually via Value.Copy, which calls bytes.Clone and allocates once per value. Reading a large input into an Array therefore allocates once per value: BenchmarkReadBSUP (10M values) does ~10M allocations. Add a chunked super.Arena that copies value bytes into 64 KiB chunks. A chunk is never reallocated once it holds a value, so earlier values stay valid as later ones are appended. Native values are returned unchanged (no allocation), and a value with no bytes keeps its nil-vs-empty distinction. sbuf.Array now copies through an Arena. BenchmarkReadBSUP: 10,000,416 -> 904 allocs/op (-99.99%), B/op -2.6%.
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.
Closes #7064.
Problem
sbuf.Array.Writecopies every value withValue.Copy→bytes.Clone, so itallocates once per non-native value. Reading a large input into an
Arrayallocates proportionally to the value count —
BenchmarkReadBSUP(10M values)does ~10M allocations.
Change
Add a chunked
super.Arenaand havesbuf.Arraycopy through it:earlier remain valid as later ones are appended (when a chunk fills, a fresh
one is allocated and the old one is kept alive by the values referencing it);
allocation), matching
Value.Copy;unchanged, preserving the nil-vs-empty distinction
bytes.Clonewould;arena's spare capacity.
Result
BenchmarkReadBSUP:ns/opis unchanged (the benchmark is GC/bandwidth-bound); the win is thecollapse in allocation count and the resulting GC pressure on large reads.
Testing
TestArenaCopycovers native values, source-buffer independence, null(nil-bytes) preservation, values spanning many chunks, and a value larger
than one chunk.
go test -short ./...passes across the repo (round-trips unchanged).