perf: classify encoder table entries once#1104
Open
pelletier wants to merge 1 commit into
Open
Conversation
Deciding whether an entry is a key-value, a table, or an array of tables requires resolving the value's type — and for arrays, the type of every element. That decision was re-derived by each encoding pass (and again by the super-table check), up to three times per entry. Entries now carry an entryClass, computed at most once and cached: the first pass classifies while emitting key-values, the second pass routes on the stored class, and onlySubTables reuses whatever the entries already know. 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. First of the encoder chain.
What
Deciding whether an entry is a key-value, a table, or an array of tables requires resolving the value's type — and for arrays, resolving the type of every element. That decision was re-derived by each encoding pass (and a third time by
SetOmitEmptySuperTables's super-table check), up to three times per entry.Entries now carry an
entryClass, computed at most once and cached on the entry: the first pass classifies while emitting key-values, the second pass routes on the stored class, andonlySubTablesreuses whatever the entries already know (classOfcomputes lazily for the entries it sees first).entryIsTabledisappears.Marshaler-shaped entries keep their
rawShaperouting and are assigned their class by it, so the opt-inunstable.Marshalersemantics are untouched.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/HugoFrontMatter: -9.85%Marshal/ReferenceFile/struct: -9.63%RealWorldViperWrite: -8.13%Marshal/SimpleDocument/map: -7.63%Marshal/SimpleDocument/struct: -7.23%Marshal/ReferenceFile/map: -7.02%Full benchstat (sec/op, allocs/op)
sec/op
allocs/op
🤖 Generated with Claude Code