perf: precompute static field facts in encoder plans#1106
Open
pelletier wants to merge 1 commit into
Open
Conversation
Struct entries re-derived per encode what their plan already knows: - whether the key needs quoting (a byte scan per key, and again for every parent part of every nested table header — key-stack parts now carry their quoting need, computed once at push); - the entry class, when the field type fully determines it (interfaces and pointers resolve dynamically, and slices of table-like elements switch on emptiness, so those still classify at runtime — as does everything when the Marshaler interface is enabled); - the type encoding properties, previously an interface-hashed cache lookup per value. Entries also slim down: the options (the widest part) become a pointer into the shared plan, with the Marshaler rawShape moving onto the entry itself, and single-index (non-embedded) field access skips the index-walk loop. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This was referenced Jul 11, 2026
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 one optimization technique so its impact and review surface stay isolated. Stacked on #1105.
What
Struct entries re-derived per encode what their plan already knows statically:
bareKey; key-stack parts carry their quoting need, computed once at push (keyStackPart).staticFieldFacts). Interfaces and pointers resolve dynamically, slices of table-like elements switch on emptiness, and the Marshaler mode re-classifies dynamically (its shape depends on runtime content) — those keep the runtime path.props/propsKnown), feedingappendValuePropsdirectly instead of a cache lookup per value.Entries also slim down:
options(the widest part of the entry) becomes a pointer into the shared read-only plan — with the MarshalerrawShapemoving onto the entry itself so nothing ever writes through the shared pointer — and single-index (non-embedded) field access skips the index-walk loop.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:
Marshal/ReferenceFile/struct: -26.91%Marshal/SimpleDocument/struct: -5.81%RealWorldViperWrite: -4.73%Unmarshal/SimpleDocument/struct: -4.01%Unmarshal/SimpleDocument/map: -3.08%Marshal/HugoFrontMatter: -1.59%RealWorldContainerdConfig: +1.56%RealWorldPyproject: +1.68%UnmarshalDataset/canada: +2.18%Marshal/SimpleDocument/map: +5.25%Full benchstat (sec/op, allocs/op)
sec/op
allocs/op
🤖 Generated with Claude Code