Skip to content

Improve Perf: fail inline links early on deep parentheses in destination or title - #985

Open
heberholanda wants to merge 1 commit into
xoofx:mainfrom
heberholanda:fix-link-paren-quadratic
Open

heberholanda wants to merge 1 commit into
xoofx:mainfrom
heberholanda:fix-link-paren-quadratic

Conversation

@heberholanda

Copy link
Copy Markdown

Problem

Parsing an inline link is quadratic for inputs with many unclosed ]( openers:

  • "[a](" × N + ")": for each ](, LinkHelper.TryParseUrl counts the nested ( of the following openers and scans all the way to the end of the text before failing.
  • "[a](u (" × N + ")": the destination is fine, but TryParseTitle accepts unescaped ( inside a title enclosed in parentheses, so every attempt again scans to the end of the text.

On current main, rendering 100,000 characters of either input takes 1–2 seconds. Found while hardening a wiki that renders user content.

Fix

Two small changes in LinkHelper, both following the CommonMark spec and cmark:

  1. Link destination: the spec says "Implementations may impose limits on parentheses nesting to avoid performance issues, but at least three levels of nesting should be supported." cmark limits it to 32 (manual_scan_link_url_2), so TryParseUrl now fails as soon as more than 32 unescaped ( are open. Autolinks (isAutoLink) are not affected.
  2. Link title: the spec (0.31.2) defines a title in parentheses as "a sequence of zero or more characters between matching parentheses ((...)), including a ( or ) character only if it is backslash-escaped". cmark's scanner does the same ([(] (escaped_char|[^()\x00])* [)]). TryParseTitle now fails on an unescaped ( in a title enclosed in parentheses.

The same change is applied to TryParseUrlTrivia and TryParseTitleTrivia. With both limits, each failed attempt stops after a bounded number of characters, so parsing becomes linear.

Measurements

Markdown.ToHtml with the default pipeline, best of 3 runs, .NET 9, Release:

Input Length Before After
"[a](" × N + ")" 20,000 101 ms 7–12 ms
40,000 334 ms 14–15 ms
100,000 2,136 ms 35–73 ms
"[a](u (" × N + ")" 20,000 53 ms 3–6 ms
40,000 176 ms 5–7 ms
100,000 1,348 ms 13–24 ms

Tests

  • TestLinkHelper.TestUrlNestedParenthesisLimit: 32 levels are still accepted, 33 are rejected (TryParseUrl and TryParseUrlTrivia).
  • TestLinkHelper.TestTitleUnescapedParenthesis: escaped parentheses in a (...) title are still accepted, an unescaped ( is rejected, and ( inside a quoted title is still accepted (TryParseTitle and TryParseTitleTrivia).
  • MiscTests.UnclosedInlineLinksAreNotQuadratic: both inputs at 100,000 characters render the expected HTML.

The two TestLinkHelper tests fail without the fix. The whole suite passes on net8.0 and net10.0 (7579 passed, 1 skipped), including the CommonMark spec and roundtrip tests.

Copilot AI balanced review requested due to automatic review settings November 25, 2026 03:54

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

else if (c == '(' && enclosingCharacter == '(')
{
// An unescaped ( is not allowed in a title enclosed in parentheses
break;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This makes a valid link reference definition disappear when the next line is text that starts with ( and has another ( inside:

[a]: /u
(see (x) for more)

[a]

main (same as commonmark.js): <p>(see (x) for more)</p> and <p><a href="/u">a</a></p>
this PR: <p>[a]: /u\n(see (x) for more)</p> and <p>[a]</p>

Before, the title parsed as see (x, and TryParseLinkReferenceDefinition went to the "title followed by text" case, which keeps the definition and drops the title. Now the title fails, so it goes to the else { return false; } next to it.

I ran 60k random inputs against commonmark.js: 952 go from right to wrong with this PR. With #986 applied on top (it keeps the definition when a title on the next line does not close) these are all gone, so #986 should go in first, or that part of it could be included here. A test with the input above would guard it.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Thanks for catching this, and for the commonmark.js comparison. You're right: this is a regression from this PR.

[a]: /u
(see (x) for more)

[a]

On main the title parses as see (x and the definition is kept through the "title followed by text" path. With this PR, TryParseTitle now rejects the unescaped ( (correct per spec), so the definition falls into the else { return false; } branch and is lost. That branch already drops definitions on main when a title on the next line doesn't close ([foo]: /url\n(title), which is what #986 fixes; this PR just sends more inputs down that path.

I applied #986 on top of this branch locally: your example, [a]: /u\n(t (x) y)\n\n[a] and [foo]: /url\n(title all keep the definition and match commonmark.js, and [a]: /u (ok) still gets its title.

So this PR shouldn't go in before #986. My plan:

  1. Once Fix link definition lost to an unclosed title on the next line #986 is merged, rebase this PR on top of it.
  2. Add your input as a regression test ([a]: /u\n(see (x) for more)\n\n[a] → <p>(see (x) for more)</p>\n<p><a href="/u">a</a></p>), with and without trivia tracking.

If you'd rather have this PR stand on its own, I can include the same fallback here instead. Let me know which you prefer.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Thanks for checking it. Rebasing on #986 sounds best to me: one fallback in one place. That is xoofx's call though.

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants