Repository navigation
Conversation
newFormatter rebuilt the whole-document token-to-origin map on every call, and the decoder constructs a new formatter per CustomUnmarshaler node, making decode time quadratic in the number of such nodes. Build the map once per document and reuse it across formatter calls.
|
Codecov Report❌ Patch coverage is Additional details and impacted files@@ Coverage Diff @@
## master #966 +/- ##
=======================================
Coverage 80.65% 80.65%
=======================================
Files 22 22
Lines 6845 6856 +11
=======================================
+ Hits 5521 5530 +9
- Misses 905 906 +1
- Partials 419 420 +1 🚀 New features to boost your workflow:
|
Author
|
Closing in favour of #967, which removes the full-document scan entirely and is faster. |
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.
What
Fixes #965: decode time is quadratic in the number of
CustomUnmarshalernodes.Why
newFormatterwalks the entire document token chain to rebuild itstokenToOriginMapon every call.decodeByUnmarshalercreates a fresh formatter(via
unmarshalableDocument) for eachCustomUnmarshaler/BytesUnmarshalernode,so decoding N such nodes rebuilds the whole-document map N times → O(N²).
The map depends only on the document's token chain, which is the same for every
node in that document, so it can be built once and reused.
Change
format.NewTokenToOriginMapand letnewFormatteraccept a prebuilt map (newFormatNodeWithTokenOriginMap;FormatNodeWithResolvedAliasstill works, passingnil).reused
Decoderacross different trees rebuilds correctly) and reuses it for allcustom-unmarshaler formatting.
Formatter output is unchanged — only the per-node rebuild is removed.
Tests
Added
BenchmarkUnmarshalWithCustomUnmarshalermirroring the issue's repro.Validation (
go1.23.4,go test ./...all pass):Before (ns per line stays proportional to line count — quadratic):
After (flat — linear):
At n=2000 the decode drops from ~2.2s to ~23ms. Full suite green on the branch.