Repository navigation
fix(memory): find the opener of every index link in one pass (#1262) - #1434
Conversation
) index_targets called _has_opener once per link, and the scan back runs to the start of the paragraph whenever a link has no unmatched `[` before it (`a](b)`, an escaped `\[`), so an index of many such links took quadratic time in that step. It also tested each link against every definition title with a linear any(). The openers are now found by one left-to-right pass (_closing_openers) with a count of open brackets, reset at a blank line, with the same answers as _has_opener (a differential test pins that over a table and 400 seeded random strings). The title test is a bisect over the sorted, merged title spans. _has_opener stays for the per-span lookups of _blank_inline. Signed-off-by: Anton Volkov <antonwolfy@gmail.com>
After the opener walk, index_targets on `"[a](b) " * n` was still quadratic: _used_labels tested every label of a line against every link span of the line with `any(...)`, and a line of n links has n spans and n labels. The spans are now sorted and merged once per line and each label is looked up with bisect_right, with the same answer as the any() test. Measured, `"[a](b) " * n` takes 0.001, 0.003, 0.005 s at n = 500, 1000, 2000. `"[a](b)\n" * n` is not changed here: its time is in prose_lines/_blank_blocks (intel#1261). Signed-off-by: Anton Volkov <antonwolfy@gmail.com>
|
✅ OpenCodeReview: Review complete: 0 finding(s) across 4 selected item(s). CI at Test notes (1)
Review details
|
Mutation of changed lines75 mutants of the lines this PR changes, at a144aa9: 71 killed, 4 survived, 0 waived, 0 inconclusive, 0 timed out, 0 errors, 0 not run. A survivor is a change to that line that the tests covering it do not notice: add an assertion that fails on it or, if the change cannot alter behaviour, put
Warn-only: a survivor does not fail the job yet. Updated on every push; the full report is in the ci run summary. |
|
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #1434 +/- ##
==========================================
- Coverage 96.28% 96.26% -0.02%
==========================================
Files 513 513
Lines 68470 68498 +28
Branches 11884 11892 +8
==========================================
+ Hits 65924 65943 +19
- Misses 1566 1570 +4
- Partials 980 985 +5
🚀 New features to boost your workflow:
|
napetrov
left a comment
There was a problem hiding this comment.
Change class: LOCAL (head a144aa9)
- Contract: none
- Responsibility: none
- Dependency: none
- Review depth: skim: local change, check diff + tests
- Coverage gaps: none
I traced opener matching, escaped brackets, the paragraph reset and membership in merged half-open spans against the old implementation. CI passed. I reviewed the growth and equivalence tests from source. I left a small follow-up for the already tracked label scanner.
@napetrov (#1434 (review)): Approval with no change asked. The follow-up it mentions is the label scanner tracked in #1429; the nit in the thread above is the only change this round. |
Summary
MemoryStore.index_targetstook quadratic time on an index of many links, in two places. It asked_has_opener(text, m.start())for each link: that scan goes back from the]until it finds an unmatched[or the paragraph begins, so for a link with no opener (a](b), an escaped\[a](b)) it runs to the start of the paragraph. And_used_labelstested each[label]of a line against every link span of that line withany(...), so a line of n links did n times n tests, which is the time the issue measured on its own shape. Onmain,index_targets()CPU time:"[a](b) " * n(the issue's shape), onmain"[a](b) " * n, with this change"[ " + "a](b) " * n, onmain"[ " + "a](b) " * n, with this change"\\[a](b) " * n, onmain"\\[a](b) " * n, with this change"[a](b)\n" * n(one paragraph), onmainand with this changeThe last row is not changed: its time is in
prose_linesand_blank_blocks(a profile at n = 2000 puts 1.59 of 1.62 s there), which is #1261, and a long run of definitions is #1263. The"\\[a](b) "row after the change is the backslash reads of_escapedplus the rest of the lint, not the opener pass.The fix for the opener is the issue's first option: a left-to-right walk with a count of open brackets, instead of a stack or a per-offset cache (a count is all
_has_openerreads: whether an unmatched[stands before the]). The per-link test of a link against every definition title and the per-label test against every link span (any(...)over a list) are lookups by bisection over the sorted spans now.Closes #1262
Changes
dokimi/dokimi/memory/store.py:_closing_openers(text, closes)makes one pass over the joined index and returns the]offsets inclosesthat close an opener: an unescaped[adds one to the count, an unescaped]removes one when the count is above 0 (and is a hit, when asked about, before it removes), a blank line resets it.index_targetscalls it once, not_has_openerper link._has_openerstays:_LinkCache.endstill calls it per](of a paragraph._merged(spans)sorts spans and joins overlapping ones;index_targetsfinds the title that holds a link bybisect_right, and_used_labelsfinds the link span that holds a label the same way, with the starts built once per line, and the current and next line it passes to_inline_link_atare joined once per line, not once per label.dokimi/tests/test_blank_inline.py: a differential test (a table of shapes and 400 seeded random strings over[ ] ( ) \ \n a: the pass gives_has_opener's answer at every], soindex_targetsgives the same links as before), a table ofindex_targetsresults (a](b),[[a]](b), escaped brackets, an opener before a blank line, a leading newline, a link inside a definition title), a test that no_has_openercall is made per link, a test of_merged, growth tests of the opener pass on three shapes, a table of_used_labelsresults with a label inside a link destination or a title, and an end-to-end growth test ofindex_targets()on"[a](b) " * n, all undertests/growth_timing.py.Risk
None: the links
index_targetsreturns and the labels_used_labelscounts as used are unchanged for every input the tests cover (the differential test compares the new pass with_has_openerat every]of 400 random strings and the shapes above; the link spans are disjoint, so the bisect gives the answer of theany(...)test). A hit is answered before the bracket is removed, as_has_openerdoes. Backslash runs are still read per bracket (_escaped), as before; a line of many backslashes is not made worse. The open #1430 also changesmemory/store.py(many reference definitions);git merge-treeof its head with this branch is clean.Testing
On Linux, at
29abcd36(onmaine260503):make ci-local: 37 passed, 1 failed, 16 skipped (steps that need a tool the host lacks, another job's matrix, or a PR-only condition). 31502 tests passed, 1 failed, 27 skipped. The one failure istests/test_repo_typecheck_grader.py::test_a_stub_in_the_workspace_that_hides_a_module_fails_the_gradehitting the 60 s timeout under load, in a file this change does not touch; that file alone passes (116 passed). An earlier run at the same code failed four other tests of untouched files on the same timeout, and those four pass alone (246 passed).main's code (the test file copied in): the end-to-end growth test fails on the ratio (248.8 against a limit of 64); the opener-pass growth tests, the no-call test and the_mergedtest of the first commit fail onmaintoo (5 failed, 172 passed there, with_closing_openersand_mergedgiven_has_opener's per-close answer and a plain sort).\[not escaped, a hit asked after the removal, no join of overlapping title spans, titles ignored,_has_openerper link inindex_targets,_has_openerper close in the pass (growth test),inside = Falsein_used_labels(both table rows with a label inside a destination or a title), theany(...)over spans in_used_labels(end-to-end growth test, ratio 236).Open questions
None.
Revert
A plain revert restores the per-link backward scan, the linear title test and the linear label test. Nothing is stored.
Checklist
pytestpasses [the full run had one 60 s timeout under host load in an untouched file, which passes alone]ruff,mypy, and schema checks passAGENTS.mdraise/exit and test names the doc, contract or issue it traces to.github/.licenserc.yaml)