perf: cut per-document overheads of pooled decoders#1099
Open
pelletier wants to merge 1 commit into
Open
Conversation
Three per-document costs on the decoder hot path: - Decoder.Decode read its input through io.ReadAll's growth sequence. Readers that know their size (bytes.Reader, strings.Reader, bytes.Buffer, ...) now get an exactly-sized buffer up front. - Resetting a pooled decoder walked the array-table count map to zero every slot. Slots are now stamped with a generation and a single counter bump invalidates all of them in O(1). - Every struct table lookup paid an interface-hash sync.Map access for its struct plan. A small MRU memo on the decoder catches the handful of types a document actually uses. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
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.
Splitting #1088: this PR carries three small per-document costs of the pooled decoder in one reviewable unit.
What
Decoder.Decoderead its input throughio.ReadAll's growth sequence. Readers that know their size (bytes.Reader,strings.Reader,bytes.Buffer, anything withLen() int) now get an exactly-sized buffer up front, with aReadAllfallback for readers that outgrow their announced size.acSlot), and a single counter bump invalidates all of them in O(1); stale slots read as zero.sync.Mapaccess for its struct plan. A small MRU memo on the decoder (8 entries) catches the handful of struct types a document actually uses.Impact
Benchmarked on a dedicated linux/amd64 spot VM (t2d), go1.26.4, interleaved A/B vs the base of this PR, benchstat over 10 samples per side:
Unmarshal/SimpleDocument/struct: -6.07%RealWorldContainerdConfig: -3.58%RealWorldHugoFrontMatterBatch: -2.86%Unmarshal/HugoFrontMatter: -2.78%Unmarshal/SimpleDocument/map: -2.61%UnmarshalDataset/code: -2.47%RealWorldGolangciStrict: +3.60%Unmarshal/ReferenceFile/struct: +9.56%RealWorldGolangciStrict: -2.08%Full benchstat (sec/op, allocs/op)
sec/op
allocs/op
Notes:
RealWorldGolangciStrictdecodes into a config with more struct types than the memo holds; the memo uses a transpose-on-hit / rotating-overwrite scheme so that this worst case degrades to a few pointer comparisons (+3.6% here) instead of an array shift per lookup, and it nets out negative once the rest of the series lands (−1.75% in the combined branch). TheUnmarshal/ReferenceFile/structrow is alignment noise on this host — the function it exercises is untouched apart from the memo indirection, and the combined branch has it at −4.95%.🤖 Generated with Claude Code