👌 Fix quadratic complexity in fragments_join / text_join#389
Merged
chrisjsewell merged 2 commits intoexecutablebooks:masterfrom May 6, 2026
Merged
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.83% +0.02%
==========================================
Files 64 64
Lines 3457 3481 +24
==========================================
+ Hits 3312 3336 +24
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 |
Avoid quadratic string concatenation in text_join by collapsing runs of adjacent text-like tokens with a single "".join(...), matching the fix applied to fragments_join. Add tests that distinguish the responsibilities of the two rules: - fragments_join is an inline post-processing rule that runs after emphasis/strikethrough resolution. It merges adjacent text tokens left behind by delimiter processing and recalculates token nesting levels. - text_join is a later core rule that converts text_special tokens to text and performs a final adjacent-text merge across inline children. The new tests verify both behaviors independently: - disabling both fragments_join and text_join preserves the raw text fragments produced by emphasis delimiter handling - disabling only text_join shows fragments_join collapsing those text fragments - disabling text_join preserves multiple text_special tokens from escape handling - enabling text_join converts and merges those text_special tokens into a single text token This keeps the token stream compact in both stages without changing observable parsing behavior.
fragments_join / text_join
Member
|
I added an extra commit 4a89f1d and updated the PR title / description, hope that's all good 😅 |
fragments_join / text_joinfragments_join / text_join
chrisjsewell
approved these changes
May 6, 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.
Optimize adjacent-token joining in both inline cleanup stages by replacing repeated pairwise string concatenation with a single
"".join(...)over each contiguous run.Details
fragments_joinmerges adjacenttexttokens left behind after emphasis/strikethrough post-processing and recalculates token levelstext_joinconvertstext_specialtokens totextand performs the final adjacent-text merge in the inline token streamBoth rules previously rebuilt growing strings incrementally, which can become quadratic for long runs.
Why
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.
Tests
Added focused tests for both rules:
fragments_join: verifies raw adjacent text fragments remain when both join stages are disabled, and thatfragments_joinalone collapses them whentext_joinis disabledtext_join: verifies escaped characters remain as multipletext_specialtokens whentext_joinis disabled, and are converted and merged into a singletexttoken when enabledResult
No behavioral change in parser output, with less unnecessary work when joining long runs of adjacent tokens.