fix: Avoid O(n^2) masked source rebuild in inline tokenizer#4
Closed
hong4rc wants to merge 1 commit into
Closed
Conversation
Owner
Author
|
Superseded by upstream markedjs#4017 |
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.
While looking for remaining quadratic paths after markedjs#4013/markedjs#4014, I found that the link-masking step in
Lexer.tokenizeis O(n²) on some ordinary inputs.Before em/strong processing,
tokenizemasks out reflinks, escaped characters, and other inline blocks so they don't interfere. Each of the three masks ran awhile ((match = rule.exec(maskedSrc)))loop that rebuilt the entiremaskedSrcstring on every match:For an input with k matches that's k full-length string rebuilds — O(k·n), i.e. quadratic when the matches are dense. It's easy to hit with perfectly normal markdown: a paragraph full of escaped punctuation, many inline
`code`spans, or many reference-style[ref]links. On my machine (marked.parseat HEAD):'\\.'.repeat(100000)'`x` '.repeat(60000)'[a]: x\n\n' + '[a] '.repeat(100000)The masking regexes match linearly on their own — the cost is purely the repeated
slice+concat rebuild, not the regex. This is the string-rebuild sibling of the em-mask work in markedjs#2818 (which fixed the regex side).The fix replaces each hand-rolled loop with a single-pass
String.prototype.replace, so the masked string is built once instead of k times. Every replacement is exactly the same length as what it replaces (reflink →[+a×(len−2) +], escapes →++, blockSkip → context prefix + same-length padding), so the global regex'slastIndexbookkeeping is unaffected and the output is byte-identical — the whole spec suite (CommonMark, GFM, original, redos) passes unchanged. The three inputs above drop to ~5 ms / ~40 ms / ~68 ms and scale linearly.I added
test/specs/redos/quadratic_inline_masking.cjscovering the three shapes; each exceeds the redos harness's 1-second budget onmasterand passes with this change.