Repository navigation
fix(memory): look up the opener and the link of a ]( once, not per code span (#1215) - #1264
antonwolfy wants to merge 3 commits into
Conversation
…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>
|
✅ OpenCodeReview: Review complete: 0 finding(s) across 1 selected item(s). CI at Review details
|
|
Codecov Report✅ All modified and coverable lines are covered by tests. 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
🚀 New features to boost your workflow:
|
napetrov
left a comment
There was a problem hiding this comment.
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.
… 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
left a comment
There was a problem hiding this comment.
@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 spanais blanked before the link check runs, and the check then reads[x]( <y `b`>)as a link, sobstays 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.
@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>
@vchamarthi (#1264 (review)): The blocking point is the line comment above, fixed in c377836. The two optional notes: I reproduced the |
|
Ready for another look at Follow-up issues opened:
|
Summary
dokimi memory lintblanks the code spans of a line in_blank_inline. For every code span that follows a](,_angle_link_endbuilt the paragraph (beforeplus 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_rundid 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. Onmain,_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
mainstay so here (the same time as onmain): a line of[x](<y>)arepeated, an unclosed destination ([x](<ythen 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 onmainand unchanged here:_blank_inline("[x](a<yb>)", False)returns[x]( <yb>), because the blanked first span lets the angle-link check read a link, sobstays text; it is tracked in #1343.Changes
dokimi/dokimi/memory/store.py: new_LinkCachekeeps, 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._BlankedLinetracks the last live](as the scan passes it, instead of searching back for it per span._unpaired_runbuilds the text once and finds the](of a run by bisection on their offsets._angle_link_endkeeps 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_openercalls, 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.Risk
None: the output is unchanged. On 200000 random lines over
[ ] ( ) < >newline backslash a,<!--,-->and a quote, with and withoutbefore,mainand this branch return the same_blank_inline,_angle_link_endand_unpaired_runresults; the existing tests oftests/test_blank_inline.pyand thedokimi memory linttests are unchanged and pass.Testing
On a Linux review host, at
c377836d(onmain4abc47d):make ci-local: 35 passed, 3 failed, 15 skipped. The failures areInstall gitleaks(download),Scan commits(needs gitleaks), andtests/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.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.pyonmain'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_openerwhere one or two are expected.](per span (the growth test, ratio 124 > 64); the unblanked line read for the opener (the opener-in-a-span test); the](ofbeforeignored (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).Open questions
None.
Revert
A plain revert restores the per-span lookup. Nothing is stored.
Checklist
pytestpassesruff,mypy, and schema checks passAGENTS.mdraise/exit and test names the doc, contract or issue it traces to.github/.licenserc.yaml)