Skip to content

Trim inline code margins without backtracking - #1890

Open
adityawrk wants to merge 1 commit into
ruby:masterfrom
adityawrk:fix/code-span-space-margin
Open

adityawrk wants to merge 1 commit into
ruby:masterfrom
adityawrk:fix/code-span-space-margin

Conversation

@adityawrk

@adityawrk adityawrk commented Nov 29, 2026 •

Copy link
Copy Markdown

Fixes #1889.

RDoc::Markdown#code trimmed a matching space or tab margin with a pattern that required the same run at both ends, and it retried that pattern in a loop. On a long run of spaces the match backtracks, so one inline code span can occupy a core for seconds. A span of 4,000 spaces took several seconds; 20,000 is far slower.

The margin is now removed with a linear walk. It still peels a leading run of spaces or tabs only when the same run is a suffix, and it still leaves text that contains a newline unchanged. The parser source and the generated parser both carry the change.

Markdown tests: 127 tests, 0 failures. The MarkdownTest 1.0.3 suite: 22 tests, 0 failures. The parser suite: 3 tests, 0 failures. The new assertion trims a 20,000-space span in under half a second.

The even-space trim scanned each code span with a regex that retried a long
run of spaces. A linear walk removes the same margin, including layered
space and tab runs, and still leaves a newline untouched.

This branch has not been deployed

No deployments
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.

RDoc::Markdown#code takes superlinear time on long space/tab runs in inline code spans

1 participant