Skip to content

perf(reflow): optimize inline code mask calculation using pre-extracted spans#714

Merged
rvben merged 1 commit into
rvben:mainfrom
chandlerc:perfreflow-optimize-inline-code-mask-cal/lkmuxtzwltpy
Jul 12, 2026
Merged

perf(reflow): optimize inline code mask calculation using pre-extracted spans#714
rvben merged 1 commit into
rvben:mainfrom
chandlerc:perfreflow-optimize-inline-code-mask-cal/lkmuxtzwltpy

Conversation

@chandlerc

Copy link
Copy Markdown
Contributor

Pre-extract code span ranges using pulldown-cmark instead of manually looping through backticks in compute_inline_code_mask.

This resolves a potential quadratic O(N^2) complexity bottleneck when computing the inline code mask on lines with many unclosed backticks, achieving guaranteed O(N) linear time complexity.

Assisted-by: Antigravity with Gemini

…ed spans

Pre-extract code span ranges using pulldown-cmark instead of manually
looping through backticks in compute_inline_code_mask.

This resolves a potential quadratic O(N^2) complexity bottleneck when
computing the inline code mask on lines with many unclosed backticks,
achieving guaranteed O(N) linear time complexity.

Assisted-by: Antigravity with Gemini

@rvben rvben left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I reviewed this differentially: built main and this branch, ran sentence-per-line reflow over 90+ real docs plus crafted edge cases (nested and unequal backtick runs, multi-line spans, code in link titles); outputs are byte-identical and a second pass is idempotent. On an adversarial 200KB paragraph of strictly increasing backtick runs this is ~8x faster in a dev build (3.05s to 0.39s), so the superlinear behavior was real and is gone.

Two behavior changes exist, both matching CommonMark where the old scanner didn't: escaped backticks no longer mask sentence splits (the spec says they're literal, so the old masking was wrong), and backticks inside HTML comments lose what was only accidental protection (comments without backticks were never protected). Neither triggers on anything in docs/.

Non-blocking thought: this file now runs three pulldown passes per text (code spans, emphasis, links); consolidating into one offset-iter pass extracting all three span kinds would be a natural follow-up. Nice fix.

@rvben
rvben merged commit 27e5507 into rvben:main Jul 12, 2026
12 checks passed
@chandlerc

Copy link
Copy Markdown
Contributor Author

Non-blocking thought: this file now runs three pulldown passes per text (code spans, emphasis, links); consolidating into one offset-iter pass extracting all three span kinds would be a natural follow-up. Nice fix.

I have a few more fixes queued up here, but will add consolidating it into one pass after they land.

@chandlerc
chandlerc deleted the perfreflow-optimize-inline-code-mask-cal/lkmuxtzwltpy branch July 13, 2026 02:34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants