perf: decode scalar key-values of reflection targets without an AST#1103
Open
pelletier wants to merge 1 commit into
Open
perf: decode scalar key-values of reflection targets without an AST#1103pelletier wants to merge 1 commit into
pelletier wants to merge 1 commit into
Conversation
Reflection targets (structs, typed maps, ...) parsed every expression into the AST arena. Scalar key-values — the bulk of a document — now scan their key and value directly and dispatch through the existing descend/assign machinery via path parts carrying the scanned bytes and ranges. Table headers and container values still go through the expression parser (a two-function cursor bridge hands the document position back and forth), so array-table bookkeeping and error rendering are identical; container values are validated by the per-value tracker with the same rules, and strict mode reports missing fields through parts with the same key spans an AST node would produce. The unmarshaler-interface mode keeps the full AST loop: captures need the raw expression ranges. 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 one optimization technique so its impact and review surface stay isolated. Stacked on #1102: merge the decode chain in order.
What
Reflection targets (structs, typed maps, ...) parsed every expression into the AST arena. Scalar key-values — the bulk of a document — now scan their key and value directly (
fusedStructDocument/fusedStructKeyVal) and dispatch through the existing descend/assign machinery via path parts carrying the scanned bytes and ranges (pathPart.data/rng).What deliberately stays on the expression parser:
SetCursor/Cursor) hands the document position toParser.NextExpressionand back, so array-table bookkeeping, seen-tracking, and error rendering are byte-identical. Container values are validated by the per-value tracker with the same rules.Strict mode reports missing fields through parts with the same key spans an AST node would produce (
MissingFieldParts), soStrictMissingErroroutput is unchanged.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/SimpleDocument/struct: -26.87%UnmarshalDataset/code: -20.42%Marshal/SimpleDocument/map: -20.33%UnmarshalDataset/example: -13.26%Unmarshal/SimpleDocument/map: -9.81%Unmarshal/SimpleDocument/struct: -9.53%RealWorldGolangciStrict: -2.25%RealWorldViperWrite: +2.35%Unmarshal/ReferenceFile/struct: +5.64%Unmarshal/HugoFrontMatter: +9.49%Marshal/ReferenceFile/struct: +11.05%Full benchstat (sec/op, allocs/op)
sec/op
allocs/op
This is a decode-only change: the
Marshal/*rows moving in both directions are code-alignment noise (the encoder is untouched byte-for-byte). Within decode, scalar-heavy documents win big (code −20%, example −13%, SimpleDocument −10%);ReferenceFile/struct/HugoFrontMattergive some back on this host — table-header-heavy shapes where the cursor bridge round-trips dominate — and net out ahead across the chain (see the combined numbers in the #1088 close-out).🤖 Generated with Claude Code