Repository navigation
fix(memory): an index of many reference definitions is linted in linear time - #1430
Conversation
…ar time (intel#1263) Two sites made the time of `index_targets` grow faster than the square of the lines in an index of definitions. `_title_spans` joined `lines[k:end]` for every candidate end of every definition and matched the closed-title pattern on the join, so a definition with no title walked to the end of its paragraph. `_blank_blocks` walked back to the start of the paragraph and joined the lines above for every line, though `_blank_inline` reads that text only when a link check needs it. `_title_spans` now reads a title line by line with the body of the closed-title pattern for its opener: the first closer on a line decides, and only blanks may follow it. The paragraph start is tracked down the text (`_paragraph_start`), and the text above a line is passed to `_blank_inline` as a call that builds it once, when read. The spans and the blanked lines are unchanged. Signed-off-by: Anton Volkov <antonwolfy@gmail.com>
|
✅ OpenCodeReview: Review complete: 0 finding(s) across 3 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 #1430 +/- ##
==========================================
- Coverage 96.28% 96.28% -0.01%
==========================================
Files 513 513
Lines 68470 68499 +29
Branches 11884 11886 +2
==========================================
+ Hits 65924 65951 +27
Misses 1566 1566
- Partials 980 982 +2
🚀 New features to boost your workflow:
|
Mutation of changed lines80 mutants of the lines this PR changes, at 4b5bbec: 69 killed, 11 survived, 0 waived, 0 inconclusive, 0 timed out, 0 errors, 0 not run. The mutant cap left out 29 more mutants of covered changed lines. 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. |
napetrov
left a comment
There was a problem hiding this comment.
Change class: CONTRACT (head 4b5bbec)
- Contract: candidate (count signal only, no file:line)
- Responsibility: none
- Dependency: none
- Review depth: check the changed surface: callers, docs, tests pin new behaviour
- Coverage gaps: none
The title scan consumes each candidate line without repeatedly rebuilding the prefix. I checked joined destinations, escaping, closing delimiters and paragraph boundaries against the old implementation and reviewed the equivalence tests. CI passed. I did not rerun timings locally.
dokimi/dokimi/memory/store.py conflicted with intel#1430, which also stopped joining the paragraph above every line, by tracking the paragraph start (`_paragraph_start`) and passing a lazy join (`_paragraph_above`, `cache(partial(...))`) to `_blank_inline`. Resolution: this branch's `_Above` (the shared rows, the paragraph start, the row offsets and the last `](`) replaces both, since it also removes the join for a line with a code span; `_paragraph_start`, `_paragraph_above`, the callable `before` of `_blank_inline`, `_unpaired_run` and `_BlankedLine`, and the `cache`/`partial` import are dropped. intel#1430's `_title_last_line`, `_title_spans` and the title patterns are kept as main has them. dokimi/tests/test_memory_index_definitions_linear.py (from main) counted calls of `_paragraph_above`, which is gone: the two tests that did now count the reads of the paragraph above (`_Text.tail`) and assert none for the same inputs. Signed-off-by: Anton Volkov <antonwolfy@gmail.com>
Summary
index_targetson an index of n lines[li]: fi.mdtook more than quadratic time (#1263). Measured onmain(CPU seconds, one run each):[li]: fi.md[li]: fi.md 'a(unclosed title)Two sites account for it; the other two the issue names (
_definitions,_used_labels) were not hot._title_spansjoinedlines[k:end]for every candidate end of every definition and matched the closed-title pattern on the join: a definition with no title never matches, so each one walked to the end of its paragraph (and the unclosed title fails the same way, because its first closer on the next line is followed by text)._blank_blockswalked back to the start of the paragraph and joined the blanked lines above for every line, though_blank_inlinereads that text only when a link check needs it. Fix option taken from the issue: read each definition's neighbours by index instead of joining lines per definition, for the title; the paragraph start is tracked down the text for the other site. Both indexes now take 0.004, 0.008, 0.016 s (no title) and 0.005, 0.011, 0.024 s (unclosed title) at 500, 1000, 2000: about 2x per doubling. Nothing else was still quadratic on either index.Two other inputs, not named by the issue, are still quadratic and are tracked in #1428 (definitions with closed titles, then many links) and #1429 (many links on one line).
Closes #1263
Changes
dokimi/dokimi/memory/store.py:_title_spansfinds the closing line of a title with_title_last_line, which reads each line once with the body of_TITLE_CLOSEDfor the title's opener. The first closer on a line decides: only blanks may follow it, else (text after it, a(in a parenthesised title, a backslash that ends the line) there is no span. A line that reaches its end goes on to the next, up to a blank line or the end of the text. The spans are the ones the join scan returned.dokimi/dokimi/memory/store.py:_blank_blockstracks the paragraph start (_paragraph_start: a line starts one when the line above is blank or a fence, else the previous start) and passes_blank_inlinea memoised call that joins the lines above,_paragraph_above(out, start, i)._blank_inline,_BlankedLineand_unpaired_runtakebeforeas a string or such a call and read it only when a code span or comment is blanked or a backtick run is left; a string still works, sotests/test_blank_inline.pyis unchanged.dokimi/tests/test_memory_index_definitions_linear.py(new): the new title scan against a copy of the old function on 32 awkward paragraphs (with the definitions_definitionskeeps, and with every definition the pattern matches) and on 3000 seeded random paragraphs; the work counted for both repros (reads of_TITLE_CLOSED, reads of_FENCE, calls that join the paragraph above).Risk
None expected:
index_targets()returns what it returned for every input, which the title comparison and the existing memory lint, catalog link, code span and_blank_inlinetests pin._paragraph_abovechanged its signature and has no other caller. Open PR #1406 changesdokimi/dokimi/records.pyonly, so a merge with it meets none of these lines. Open issue #1261 (_blank_blocksjoins the paragraph above every line) is fixed here for lines with no code span or comment; a line that has one still builds the paragraph above, so #1261 stays open for that part.Testing
On Linux, at
4b5bbec7(onmaine260503):make ci-local: 38 passed, 0 failed, 16 skipped (external catalog smoke needs Docker and network; the pre-commit ruff, zizmor and secrets hooks; semgrep; micromamba; the fuzz job; the matrix, deploy and report jobs that only run elsewhere). 31557 tests passed, 27 skipped; changed-line coverage 100%.main's code: 7 failed, 68 passed. The first failure is the paragraph walk-back,assert 278.8 < 24(work at 16n over n for_FENCE); the title tests fail onassert 2350360310 <= 15140._title_spansrestored (2 failed), the walk back to the paragraph start restored (2 failed), the paragraph above joined for every line (3 failed), the join not memoised (1 failed), a closer followed by text accepted (11 failed), the blank-line stop of the title scan dropped (3 failed), the column of a title on the next line not corrected (5 failed).Open questions
None.
Revert
A plain revert restores the quadratic scans. 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)