fix: perf quadratic ^block-id transform on long lines - #455
Open
zeyutang wants to merge 2 commits into
Open
Conversation
* add block-id.md/.expect.md fixture pair covering the syntax battery * pin trailing-space, CRLF, multi-caret, empty-prefix cases in unit tests * unit tests cover the bytes that editors and git rewrite in fixtures
* gate the quadratic lazy-head replace behind a linear suffix test * guard matches iff the replace matches, so output bytes are unchanged * warm parseMD on a 192 KB sentence-per-line paper: ~2.1 s to ~55 ms * add a long-line smoke test whose runtime exposes quadratic regressions
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.
Problem
transformMarkdownruns the^block-idreplace on every normal line, ungated:The lazy
(.*?)head under the$anchor backtracks quadratically on every non-matching line, which could mean every sentence of a real document in the worst case. Cost grows with the sum of squared line lengths, so large documents with long lines (e.g. one sentence per line, without manual line wrap) reach multi-second previews: a CPU profile of a 192 KB paper attributes ~93% of warmparseMDself-time to this one regex.Fix
Gate the replace behind a linear-time suffix test, leaving the replace untouched:
The guard matches iff the replace regex matches (both require the line to end with whitespace, then
^, then one or more id characters), and when both match the original replace still runs, so rendered HTML is byte-identical. A greedy rewrite (/^(.*)\s+\^([a-zA-Z0-9_-]+)$/) would also be fast but shifts part of a multi-space run into group 1, changing output whitespace.parseMD, 192 KB one-sentence-per-line fileTests
block-id.md/block-id.expect.mdcovering the syntax battery (transform positives, mid-line and no-space negatives,$$math and code-fence protection, indented code), pinning byte-identical output.a ^x ^y), whitespace-only prefix.