Skip to content

fix(executor): eliminate quadratic backtracking in linkify_markdown - #423

Merged
Jason Robert (jrob5756) merged 2 commits into
mainfrom
fix/395-linkify-markdown-quadratic-perf
Aug 13, 2026
Merged

fix(executor): eliminate quadratic backtracking in linkify_markdown#423
Jason Robert (jrob5756) merged 2 commits into
mainfrom
fix/395-linkify-markdown-quadratic-perf

Conversation

@jrob5756

Copy link
Copy Markdown
Collaborator

Summary

Fixes catastrophic-backtracking / quadratic-time behavior in linkify_markdown when handed pathological input (e.g. long unterminated runs of backticks/tildes or [ characters), which could stall the event loop.

Changes

  • Fenced-code-block regex opener now uses a possessive quantifier so an unterminated fence run doesn't trigger character-by-character backtracking.
  • Existing-markdown-link detection replaced with a linear single-pass scanner (_find_existing_link_spans), fuzz-verified equivalent to the regex it replaces.
  • Added a defensive MAX_LINKIFY_CHARS cap (256K) so any future pathological shape still degrades gracefully.

Testing

Added TestPathologicalInputPerformance and fuzz-equivalence tests in tests/test_executor/test_linkify.py.

Closes#395

Jason Robertand others added 2 commits August 12, 2026 19:04
Guard against pathological inputs that stalled the event loop:
- Use a possessive quantifier on the fenced-code-block regex opener
so an unterminated run of backticks/tildes no longer backtracks
character-by-character (O(n^2) -> O(n) per starting position).
- Replace the existing-markdown-link regex with a linear single-pass
scanner (_find_existing_link_spans) that is fuzz-verified equivalent
to the regex it replaces but avoids quadratic blowup on long runs of
"[" characters.
- Add a defensive MAX_LINKIFY_CHARS cap (256K) so any future
pathological shape still degrades gracefully instead of hanging,
while still normalizing whitespace above the cap.
Closes#395
Blocking:
- _find_existing_link_spans was still O(n^2) on "[](" * k input: a
failed find(")") re-scans to the end of the string while the cursor
advances only ~3 chars. Precompute rfind("]")/rfind(")") bounds so a
lookup is skipped once no more delimiters exist ahead, and correct
the comments that claimed this shape was linear/insurance-only.
- All 8 TestExistingLinkProtection tests were vacuous (return [] from
the scanner left them green). Added cases that embed real linkable
content inside/after candidate links so span-removal, the bracket-ref
alternative, and the span end-boundary are each independently pinned.
- The possessive quantifier silently stops protecting a fence whose
opener is longer than its closer. Documented the semantic effect in
the regex comment and added tests pinning the new (CommonMark-correct)
behaviour for both backtick and tilde fences.
- test_quote_token_completes_quickly passed against the pre-fix code
(budget was ~20x too loose). Tightened to a threshold calibrated
against the fixed code's actual runtime.
- test_bracket_wall_completes_quickly never reached the skip-past-"]"
path (no "]" in the input at all). Added a companion test with a
balanced bracket wall that does, plus a "[](" * 30_000 test for the
newly-fixed quadratic shape.
Recommendations applied (comment-only, no behaviour change):
- Corrected the "O(1) per starting position" claim to describe the real
O(n) mechanism (opener consumed once instead of retried).
- Documented that the trailing [^\n]*\n in the fence regex is inert
given the ^...^\1 MULTILINE anchoring, retained only for explicitness.
Reformatted test_linkify.py per `make lint`.
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@jrob5756
Jason Robert (jrob5756) marked this pull request as ready for review August 12, 2026 23:29
@jrob5756
Jason Robert (jrob5756) merged commit 3fd5f55 into mainAug 13, 2026
11 checks passed
Sign up for freeto 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.

perf(executor): linkify_markdown is O(n²) on backtick/bracket runs and stalls the event loop

1 participant

@jrob5756