Skip to content

fix(memory): look up the opener and the link of a ]( once, not per code span (#1215) - #1264

Open
antonwolfy wants to merge 3 commits into
intel:mainfrom
antonwolfy:fix/angle-link-quadratic
Open

antonwolfy wants to merge 3 commits into
intel:mainfrom
antonwolfy:fix/angle-link-quadratic

Conversation

@antonwolfy

@antonwolfy antonwolfy commented Nov 26, 2026 •

Copy link
Copy Markdown
Contributor

Summary

dokimi memory lint blanks the code spans of a line in _blank_inline. For every code span that follows a ](, _angle_link_end built the paragraph (before plus the blanked line), searched back for the last ](, ran _has_opener (a Python loop back to the start of the paragraph) and matched _ANGLE_LINK, and _unpaired_run did the same for every backtick run of the final line. The cost per span grew with the line, so a line with a link before n spans was quadratic. On main, _blank_inline("[x](<y>) " + "a " * n, False) takes 1.10 s at n = 20000 (80009 characters), 3.5 times the time of n = 10000; with this change it takes 0.10 s, 2.0 times per doubling.

The issue offers two fixes; this takes the first (cache per ](, match on the text from that ](), since the blank ranges are appended left to right and the text before a live ]( cannot change once the scan has passed it.

Closes #1215

Three shapes that were quadratic on main stay so here (the same time as on main): a line of [x](<y>) a repeated, an unclosed destination ([x](<y then many spans), and [x]( followed by many spans (0.13 s, 0.51 s and 2.0 s at 2000, 4000 and 8000 spans). The first two are tracked in #1260, the third in #1342. The changelog fragment names the third. The same cause in other per-line and per-link loops of the memory store is tracked in #1261, #1262 and #1263. Also on main and unchanged here: _blank_inline("[x]( a<yb>)", False) returns [x]( <y b>), because the blanked first span lets the angle-link check read a link, so b stays text; it is tracked in #1343.

Changes

  • dokimi/dokimi/memory/store.py: new _LinkCache keeps, per ](, whether it closes an opener, the end of the link it matched, and that a ]( whose first character after the blanks is not < starts no link. The text is built only when a ]( is looked up for the first time.
  • _BlankedLine tracks the last live ]( as the scan passes it, instead of searching back for it per span. _unpaired_run builds the text once and finds the ]( of a run by bisection on their offsets. _angle_link_end keeps its signature and result and goes through the same cache.
  • _BlankedLine.__init__ builds the newline-terminated paragraph above the line once and reuses it and its length for every lookup, so a cache hit no longer copies the paragraph.
  • tests/test_blank_inline.py: a growth test for the issue's shape, two counts of _has_opener calls, a count of the copies of the paragraph above, a test of an opener blanked inside a code span, and one of a link opened on the line above.
  • Changelog fragment (Fixed).

Risk

None: the output is unchanged. On 200000 random lines over [ ] ( ) < > newline backslash a, <!--, --> and a quote, with and without before, main and this branch return the same _blank_inline, _angle_link_end and _unpaired_run results; the existing tests of tests/test_blank_inline.py and the dokimi memory lint tests are unchanged and pass.

Testing

On a Linux review host, at c377836d (on main 4abc47d):

  • make ci-local: 35 passed, 3 failed, 15 skipped. The failures are Install gitleaks (download), Scan commits (needs gitleaks), and tests/test_repo_typecheck_grader.py::test_a_site_packages_module_not_claimed_by_the_workspace_alone_is_still_shadowed[index], a test outside this PR that hit the 60 s timeout under host load and passes alone (that file: 108 passed in 26 s). 29682 tests passed, 1 failed, 23 skipped. The round's commit changes only the changelog fragment.
  • The repro of the issue on main: 1.10 s at 80009 characters, 3.5 times per doubling; here 0.10 s, 2.0 times per doubling.
  • tests/test_blank_inline.py on main's code: 4 failed, 153 passed; the growth test fails with a ratio of 115 against a limit of 64, and the three count tests with 501 calls of _has_opener where one or two are expected.
  • Mutants, each failing the test named: no cache of a matched link (both opener-count tests, 501 == 2); a search back for the ]( per span (the growth test, ratio 124 > 64); the unblanked line read for the opener (the opener-in-a-span test); the ]( of before ignored (the test of a link opened on the line above); a new cache per backtick run in _unpaired_run (the backtick-run test, 501 == 1); the paragraph above rebuilt with its newline on every lookup (the paragraph-copy test, 502 == 2).
  • Not run: the matrix legs on Python 3.11, 3.12 and 3.14.

Open questions

None.

Revert

A plain revert restores the per-span lookup. Nothing is stored.

Checklist

  • Tests added/updated and pytest passes
  • ruff, mypy, and schema checks pass
  • Docs updated (or N/A) — see the docs-drift checklist in CONTRIBUTING.md
  • Self-reviewed per "Before opening a PR" in AGENTS.md
  • Each new raise/exit and test names the doc, contract or issue it traces to
  • No ad-hoc run artifacts committed
  • New files carry the SPDX license header (or are exempt in .github/.licenserc.yaml)

…code span (intel#1215)

`_angle_link_end` built the paragraph (`before` + the blanked line) and ran `_has_opener`, a back scan to the paragraph start, and `_ANGLE_LINK` on it for every code span that followed a `](`, and `_unpaired_run` did the same for every backtick run. On `[x](<y>) ` followed by n times `` `a` `` that is quadratic in the line length: 0.88 s at 80009 characters, 3.5 to 4 times more per doubling.

`_LinkCache` keeps, per `](`, whether it closes an opener (the text before it no longer changes once the scan has passed it), the end of the link when it matched, and that a `](` whose first character after the blanks is not `<` never starts one. The text is built only when a `](` is looked up for the first time. `_BlankedLine` tracks the last live `](` instead of searching back for it, and `_unpaired_run` finds it by bisection on the `](` offsets of the final line. `_angle_link_end` keeps its signature and result.

Signed-off-by: Anton Volkov <antonwolfy@gmail.com>
@exai-bot

exai-bot commented Nov 26, 2026 •

Copy link
Copy Markdown

✅ OpenCodeReview: Review complete: 0 finding(s) across 1 selected item(s).

CI at c377836: 21 passing, 4 skipped.

Review details
  • Commit reviewed: c377836 against merge base 849b342
  • Coverage: 1 files in 1 groups
  • Engine: OpenCodeReview v1.13.5-exai.4.8f73eada, us.openai.gpt-6.1-sol
  • Usage: 10k tokens (0 from cache), 4s
  • Tool calls: 0
  • dokimi/changelog.d/20261126_140000_angle-link-quadratic.md: 1 file

@codecov-commenter

codecov-commenter commented Nov 26, 2026 •

Copy link
Copy Markdown

⚠️ Please install the 'codecov app svg image' to ensure uploads and comments are reliably processed by Codecov.

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 96.18%. Comparing base (4abc47d) to head (c377836).
⚠️ Report is 28 commits behind head on main.
❗ Your organization needs to install the Codecov GitHub app to enable full functionality.

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #1264      +/-   ##
==========================================
+ Coverage   96.14%   96.18%   +0.03%     
==========================================
  Files         498      499       +1     
  Lines       66004    66506     +502     
  Branches    11461    11549      +88     
==========================================
+ Hits        63460    63968     +508     
+ Misses       1573     1564       -9     
- Partials      971      974       +3     
Files with missing lines Coverage Δ
dokimi/dokimi/memory/store.py 96.36% <100.00%> (+0.37%) ⬆️

... and 56 files with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@napetrov napetrov 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.

Change class: LOCAL (head 0592203)

  • Contract: none
  • Responsibility: none
  • Dependency: none
  • Review depth: skim: local change, check diff + tests
  • Coverage gaps: none

The new cache avoids repeated opener lookup for a single link. Source review of the multiline caller found a remaining paragraph copy on cache hits. I left a question inline and reviewed the equivalence and growth tests from source. CI is green.

Comment thread dokimi/dokimi/memory/store.py Resolved
… in _BlankedLine

Review point on intel#1264: `angle_link_end` rebuilt `self.before + "\n"` on every call, before `_LinkCache.end` could return its cached value, so a long paragraph above cost its length per code span.

`_BlankedLine.__init__` now builds that prefix once and reuses it and its length for the `](` offsets and for each lookup. A test counts the copies of the paragraph made with its newline: two for 500 spans (the blanked line and the unpaired run), where the per-call build made one per span.

Signed-off-by: Anton Volkov <antonwolfy@gmail.com>

@vchamarthi vchamarthi 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.

@antonwolfy, could you fix the small thing I've marked fix before merge (in the line comments)? I'll approve once that's done.

Optional, non-blocking (2)
  • dokimi/dokimi/memory/store.py:415: Input [x]( `a` <y `b`>): the code span a is blanked before the link check runs, and the check then reads [x]( <y `b`>) as a link, so b stays as text (main has it too).
  • dokimi/dokimi/memory/store.py:278: Input [x]( followed by 32,000 code spans of `a`: the check does not mark the ]( as dead, because the next span starts where the blanks end, so each later span rebuilds and rereads the line (main has it too).

What I checked on 0592203:

  • When I changed the code on purpose, the tests still passed for 1 of 12 changes, and no test pins this one down yet (non-blocking): dokimi/dokimi/memory/store.py:278 (the condition is negated).
  • The 157 changed tests pass, and fail on main as expected.
  • The full test suite passes with main merged in (32,859 tests), apart from failures main has too.
  • It merges cleanly onto main.

@antonwolfy

Copy link
Copy Markdown
Contributor Author

(commented) ### Change class: LOCAL (head 0592203)

@napetrov (#1264 (review)): The review's one open point is the inline question on the paragraph copy, answered above.

…uadratic

Settles the blocking review point: the changelog fragment said the time grows in step with the line, but a `](` followed by a space and many code spans is still quadratic (0.13 s, 0.51 s and 2.0 s at 2000, 4000 and 8000 spans). The fragment now says the linear claim holds for the `[x](<y>) ` shape and names the other.

Signed-off-by: Anton Volkov <antonwolfy@gmail.com>
@antonwolfy

Copy link
Copy Markdown
Contributor Author

(commented) @antonwolfy, could you fix the small thing I've marked fix before merge (in the line comments)? I'll approve once that's done.

@vchamarthi (#1264 (review)): The blocking point is the line comment above, fixed in c377836. The two optional notes: I reproduced the [x]( quadratic case and it is tracked in #1342. I also reproduced the [x]( a<yb>) reading, on this branch and on main alike: _blank_inline returns [x]( <y b>), so b stays text, whereas CommonMark has no link there (a is a destination and <y is no title), so both spans should be blanked. It is not this PR's change, so it is tracked in #1343. The surviving negated condition at store.py:278 is not changed here, which keeps to the issue's shape.

@antonwolfy

Copy link
Copy Markdown
Contributor Author

Ready for another look at c377836; CI is green.

Follow-up issues opened:

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.

memory lint: _angle_link_end copies the paragraph and scans back per backtick, quadratic on a line with a link before many code spans

5 participants