👌 fix quadratic complexity in fragments_join#389
Open
petricevich wants to merge 1 commit intoexecutablebooks:masterfrom
Open
👌 fix quadratic complexity in fragments_join#389petricevich wants to merge 1 commit intoexecutablebooks:masterfrom
petricevich wants to merge 1 commit intoexecutablebooks:masterfrom
Conversation
When emphasis/strikethrough postprocessing leaves a long run of adjacent text tokens (e.g. unmatched intraword `_` delimiters), fragments_join merged them via pairwise `a + b` concatenation. Each step rebuilds the growing prefix, costing O(L*k) per run. Walk the whole run once, collect content into a list, and "".join into the last token, making the work O(L). The kept token is still the last in the run so its non-content attributes (markup, etc.) are preserved.
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## master #389 +/- ##
==========================================
+ Coverage 95.80% 95.81% +0.01%
==========================================
Files 64 64
Lines 3457 3467 +10
==========================================
+ Hits 3312 3322 +10
Misses 145 145
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
Member
|
Thanks, will double check soon, but sounds good in principle |
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.
When emphasis/strikethrough postprocessing leaves a long run of adjacent text tokens (e.g. lots of intraword
_that can't open or close emphasis), the old code merged them pairwise:That's quadratic in the size of the run because every step rebuilds the growing prefix. Switched it to collect the run into a list and
"".joinonce into the last token, which keeps the existing semantics (last token of the run is the one preserved, level is unchanged inside a run because text tokens havenesting=0).Tested on an adversarial ~190 KB document with ~30k intraword underscores on a single line. With
tracemallocrunning:It's not just a contrived attack input - this kind of thing also shows up naturally in markdown produced by OCR pipelines, where tables of identifiers / references can easily contain very long runs of underscores or other delimiter characters.
Existing tests still pass.