perf: fast-path encoder type properties - #1105
Open
pelletier wants to merge 1 commit into
Open
Conversation
Every encoded value consults its type's cached properties (TextMarshaler and unstable.Marshaler receivers, value-vs-table), paying a sync.Map lookup — an interface hash — per consultation; this was 20% of struct marshaling. Three layers shortcut it: - builtinEncProps resolves the handful of builtin types generic documents are made of with a kind dispatch and one pointer comparison. - A small MRU memo on the encoder state catches the few named types a document actually uses. - appendValue dispatches special types kind-first (they are all structs except json.Number), keeping common scalars away from the interface comparisons, and appendValueProps lets callers that already hold the properties skip the lookup entirely. 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 #1104.
What
Every encoded value consults its type's cached properties (TextMarshaler and unstable.Marshaler receivers, value-vs-table), paying a
sync.Maplookup — an interface hash — per consultation; this was ~20% of struct marshaling. Three layers shortcut it:builtinEncPropsresolves the handful of builtin types generic documents are made of (string,bool,int,int64,float64,[]interface{},map[string]interface{}) with a kind dispatch and one pointer comparison — none of them can implement TextMarshaler.sync.Mapstays as the shared cold cache.appendValuedispatches special types kind-first (they are all structs exceptjson.Number), keeping common scalars away from four interface comparisons, and the newappendValuePropslets callers that already hold the properties skip the lookup entirely (used by the plan-facts PR next in the chain).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:
RealWorldViperWrite: -15.13%Marshal/HugoFrontMatter: -13.55%Marshal/ReferenceFile/map: -13.53%Marshal/SimpleDocument/map: -7.10%Marshal/ReferenceFile/struct: -4.25%Marshal/SimpleDocument/struct: -3.87%UnmarshalDataset/code: -2.64%Unmarshal/SimpleDocument/struct: +2.55%Full benchstat (sec/op, allocs/op)
sec/op
allocs/op
🤖 Generated with Claude Code