Skip to content

[markdown] Fix quadratic parsing of consecutive link reference definitions - #2662

Merged
sigurdm merged 2 commits into
dart-lang:mainfrom
sigurdm:markdown-linear-link-refs
Oct 13, 2026
Merged

sigurdm merged 2 commits into
dart-lang:mainfrom
sigurdm:markdown-linear-link-refs

Conversation

@sigurdm

@sigurdm sigurdm commented Oct 12, 2026

Copy link
Copy Markdown
Contributor

LinkReferenceDefinitionSyntax.canEndBlock is false (link reference definitions cannot interrupt a paragraph), so BlockSyntax.isAtBlockEnd(parser) does not stop at subsequent link reference definitions.

Previously, LinkReferenceDefinitionSyntax.parse greedily consumed all remaining lines until the end of the block, joined them with \n, parsed a single definition, split source.substring(pos).split('\n') to count unconsumed lines, and called parser.retreatBy(unconsumedLines). For $N$ consecutive link reference definitions (such as the [#1]..[#N] block at the bottom of large changelogs), this joined and split $O(N^2)$ lines.

Instead, pass BlockParser directly into LinkParser and start with only the current line, advancing through additional lines from BlockParser on demand only when a link label, destination, or title spans across a newline, and retreating BlockParser if the definition or a multiline title fails.

@gemini-code-assist gemini-code-assist Bot 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.

Code Review

This pull request addresses a performance issue where parsing consecutive link reference definitions took quadratic time. Instead of pre-consuming and joining all remaining lines in a block, LinkParser now directly wraps BlockParser and lazily consumes lines only as needed for a single definition, retreating the parser if validation fails. A regression test has been added to verify that parsing 5,000 consecutive link reference definitions is efficient. There are no review comments, and I have no additional feedback to provide.

@srawlins srawlins left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Looks very clean!

@sigurdm
sigurdm merged commit a559658 into dart-lang:main Oct 13, 2026
24 checks passed
copybara-service Bot pushed a commit to dart-lang/sdk that referenced this pull request Oct 15, 2026
Revisions updated by `dart tools/rev_sdk_deps.dart`.

native is rolled separately in
https://dart-review.googlesource.com/c/sdk/+/563524.

http (https://github.com/dart-lang/http/compare/23f8574..7d7aae2):
  7d7aae2  Mon Oct 12 20:39:25 2026 -0700  Kevin Moore  Clean up dependencies and expand Node and Wasm test coverage (dart-lang/http#1903)
  ab986ad  Mon Oct 12 18:59:24 2026 -0700  Kevin Moore  fix(http_parser): fix chunked size overflow and MediaType.toString quoting (dart-lang/http#2001)
  cb8113b  Mon Oct 12 17:35:26 2026 -0700  Kevin Moore  docs(http2): move post-3.1.0 security entries to 3.2.0-wip (dart-lang/http#2022)

test (https://github.com/dart-lang/test/compare/30acb69..9bf69a8):
  9bf69a88  Tue Oct 13 14:33:18 2026 -0700  Nate Bosch  Reject asynchronous matchers inside isNot and anyOf (dart-lang/test#2792)

tools (https://github.com/dart-lang/tools/compare/2b54c71..350bde5):
  350bde54f  Tue Oct 13 08:22:26 2026 -0700  Kevin Moore  chore(coverage): prepare for v1.16.0 release (dart-lang/tools#2643)
  a55965844  Tue Oct 13 13:29:52 2026 +0200  Sigurd Meldgaard  [markdown] Fix quadratic parsing of consecutive link reference definitions (dart-lang/tools#2662)

[email protected]
TEST=ci

Change-Id: I1065854cfbaf71ebb7a097d89f28902394a80c2e
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/563365
Commit-Queue: Nate Bosch <[email protected]>
Reviewed-by: Brian Quinlan <[email protected]>
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.

2 participants