Repository navigation
fix(memory): lint a paragraph of many lines in linear time - #1439
Conversation
_blank_blocks joined the blanked paragraph above for every line (_paragraph_above walked back to the paragraph start and joined the rows), _BlankedLine scanned that text again for every ](, and _unpaired_run built it a second time, so a paragraph of n lines cost O(n^2): "word `a` word\n" * n took 0.011, 0.036, 0.129 and 0.486 s at n = 250, 500, 1000 and 2000. _blank_blocks now keeps an _Above for the paragraph: the start of the paragraph (reset after a raw blank line or a fence line, not after a line blanked to spaces), the offset of each row and the last ](. _BlankedLine, _unpaired_run and _angle_link_end read the last ]( of the rows above and the ]( of the line, and the opener of a ]( is looked up by offset through _Text, which reads the rows without joining them. The text of a link is joined from the row of its ]( on, only when a ]( that may still match is read. _blank_inline, _unpaired_run and _angle_link_end still take the paragraph above as a string, and the output is unchanged. test_blank_inline.py: the test that counted a newline appended to the paragraph above (twice) now asserts that the text read for a link on the line holds no row above, under the name test_the_paragraph_above_is_not_copied_for_a_link_on_the_line. Test-narrowed: test_the_paragraph_above_is_copied_with_its_newline_once_not_once_per_span is renamed, not removed: the paragraph above is no longer passed as a string that a newline is appended to, so the test now checks that no row above is joined to the line. tests/test_blank_paragraph.py pins the growth in the lines of a paragraph and the output against the joined paragraph, with counterexamples and seeded random texts. Signed-off-by: Anton Volkov <antonwolfy@gmail.com>
|
✅ OpenCodeReview: Found nothing that must be fixed in this commit. CI at Review limit reached. This PR has had 6 reviews, so it will only be reviewed again when you ask. Other findings not posted (1)1 should fix Test notes (2)
Review details
|
Mutation of changed linesAt b75f458: 76 caught · 0 missed · 4 waived Not checked:
Marked harmless with
Warning only: a missed break does not fail the job. 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 #1439 +/- ##
=======================================
Coverage 96.31% 96.32%
=======================================
Files 515 517 +2
Lines 68952 69031 +79
Branches 11936 11949 +13
=======================================
+ Hits 66413 66492 +79
+ Misses 1564 1560 -4
- Partials 975 979 +4
🚀 New features to boost your workflow:
|
napetrov
left a comment
There was a problem hiding this comment.
Change class: CONTRACT (head 0b8913e)
- Contract: candidate. defaults dokimi/dokimi/memory/store.py:236; defaults dokimi/dokimi/memory/store.py:362
- Responsibility: none
- Dependency: none
- Review depth: check the changed surface: callers, docs, tests pin new behaviour
- Coverage gaps: none
Tracking row offsets removes the unconditional prefix scan. One common multiline path still builds the same growing suffix once per continuation line. This finding comes from source inspection. CI passed.
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>
… line below (intel#1261) Settles the review thread on `_Above.last_open` and the review summary that found the same growing suffix: the last `](` of the rows above stayed as the paragraph went on, so every later line with a code span or an unpaired backtick joined the rows from that link's row to the line, even when the link had ended on its own row. `"[x](<y>) z\n" + "word `a` word\n" * n` read 1.76, 7.02 and 28.0 million characters at n = 500, 1000 and 2000. `_Above.sync` now drops the last `](` once the rows alone decide it: its link closes in them, or what follows the blanks after it is no `<`. Text added after the rows cannot change either answer, and a link that ends in the rows ends before any line below, so the output is unchanged (the differential test against the joined paragraph passes, with counterexamples for a title that closes rows later and a destination that starts after blanks at the end of the rows). A link still open is read from its own row on, as before. Those lines read no row above now. Test-narrowed: test_the_paragraph_above_is_not_joined_for_a_line_with_no_code_span_or_comment is renamed, not removed: main's `_paragraph_above` join, which it counted, is gone with the merge of this branch's `_Above`, so test_the_paragraph_above_is_not_read_for_a_line_with_no_code_span_or_comment counts the reads of the paragraph above (`_Text.tail`) on the same inputs and asserts none. Test-narrowed: test_the_paragraph_above_is_joined_once_for_a_line_that_reads_it_twice is renamed, not removed: the paragraph above is no longer joined at all for that input, so test_the_paragraph_above_is_not_read_for_a_backtick_left_over_when_no_link_is_above asserts that no row above is read for it. Signed-off-by: Anton Volkov <antonwolfy@gmail.com>
@napetrov (#1439 (review)): Same point as the thread above: the growing suffix was the rows from the |
|
Ready for another look at Follow-up issues opened:
|
vchamarthi
left a comment
There was a problem hiding this comment.
@antonwolfy, could you fix the problem I found (in the line comments)? Then push and I'll take another look.
Optional, non-blocking (1)
dokimi/dokimi/memory/store.py:392: A](<left open by a title, as in[x](<y> ", followed by lines with a code span, makes each such line match the rows from that link on; the description tracks this as #1459 (main has it too).
What I checked on 14c0afe: the 278 changed tests pass, and fail on main as expected; the full test suite passes with main merged in (38,101 tests), apart from failures main has too; it merges cleanly onto main; when I changed the code on purpose, the tests caught all 12 changes.
… above it (intel#1261) Settles the review point that `[x](<y>` followed by plain lines made each line join the rows from the open link on: the scan settled the link in `_Above.sync` for every line, so the work grew with the square of the lines. `_Above.last_open` now settles the link when it is read, and the readers (`_BlankedLine` through a lazy `_found`, `_unpaired_run` after a backtick check) ask only for a line that holds a backtick or reads a link. A plain line never joins the rows. A new test counts the angle-link matches over such a paragraph and expects none; the settled-from-its-own-row test now uses rows with a code span, since only those read the link. Signed-off-by: Anton Volkov <antonwolfy@gmail.com>
@vchamarthi (#1439 (review)): The blocking point is answered in the thread above (2ce5c7f). The optional |
There was a problem hiding this comment.
Warning
Copilot couldn't run its full agentic review because it didn't start before the timeout. Make sure your repository has a runner available, or add a copilot-code-review.yml file specifying one with the runs-on attribute. See the docs for more details.
Copilot review overview
2 open issues
What changed in this PR
This PR addresses quadratic-time behavior in dokimi memory when blanking multi-line paragraphs by restructuring how the “paragraph above” is represented and read, aiming to make paragraph scanning linear-time while preserving output.
Changes:
- Replace joined-paragraph rescans with an
_Above/_Textoffset-based representation to avoid per-line paragraph joins and scans. - Update blanking/link-detection internals (
_blank_blocks,_blank_inline,_unpaired_run,_angle_link_end) to operate on the new representation. - Add/adjust tests to lock in output equivalence and verify linear-growth and reduced work for key shapes; add a changelog fragment.
| File | Description |
|---|---|
| dokimi/dokimi/memory/store.py | Core refactor introducing _Above and _Text to avoid quadratic joins/scans while blanking paragraphs. |
| dokimi/tests/test_blank_paragraph.py | New regression + growth/work-count tests to ensure output equivalence and linear behavior. |
| dokimi/tests/test_blank_inline.py | Updates tests to reflect the new “no paragraph copying” behavior, adds an additional shape. |
| dokimi/tests/test_memory_index_definitions_linear.py | Updates linearity tests to count paragraph-above reads via _Text.tail instead of removed _paragraph_above. |
| dokimi/changelog.d/20261130_100000_memory-paragraph-linear.md | Changelog entry describing the performance fix. |
Review effort: Lite
…racter at a time (intel#1261) Review point: _has_opener read each character of a paragraph through _Text.__getitem__, which does a bisect per character. It now scans the rows above one piece at a time with a regex for the brackets, so the offset of a row is looked up once per call. The test counts the _Text reads of one lookup over 300 rows, and a table checks the same answers on the rows and on the joined text. Signed-off-by: Anton Volkov <antonwolfy@gmail.com>
Signed-off-by: Anton Volkov <antonwolfy@gmail.com>
…e paragraph change pushed past the 800-line cap (intel#1261) CI on Python 3.14 and the wheel leg failed tests/test_growth_ratchet.py: dokimi/memory/store.py was 817 lines, over the 800 cap, with no baseline entry. The title scan (`title_spans`, `title_last_line` and the two tables they read) moves unchanged to dokimi/memory/titles.py, which leaves store.py at 779 lines. The one test that imported `_title_spans` from store imports `title_spans` from the new module. Signed-off-by: Anton Volkov <antonwolfy@gmail.com>
Conflicts in dokimi/dokimi/memory/store.py, resolved by keeping both sides: main's filled lines (`_FILL`, `_blank_blocks` returning `(blanked, filled)`, `_links`, `_fill_blanks`, the `linked` argument of `_unpaired_run`) and this branch's `_Above`/`_Text`/`_LinkCache` paragraph scan. - imports: `Callable`, `Iterable` and `Iterator` are all used. - constants: `_FILL` from main kept next to `_BRACKET`. - `_blank_blocks`: the shared `_Above` now reads the filled rows, extended with the blanked-whole lines at the top of each iteration, so a link check reads the paragraph above with a blanked span as text, as main does. Main's `_paragraph_above`/`_paragraph_start` joins are not kept: `_Above` replaces them. - `_unpaired_run`: takes `linked` and reads the links, and the `](` offsets, on it. - `_blank_inline`: passes `blanked.text(fill=_FILL)` as `linked`. tests/test_blank_paragraph.py: the joined-paragraph reference takes `linked` and the comparison covers the filled lines too. Signed-off-by: Anton Volkov <antonwolfy@gmail.com>
…ts before it; move the block helpers to blocks.py (intel#1261) Review point 4754519307: `_has_opener` listed every bracket of a row before it looked at the one next to the `]`, so a row of many links cost the square of its links. It now walks the row backwards one character at a time and stops at the opener. A test times the opener lookup of every link of a row of `[x](a.md) ` and bounds its growth. Review point 4754598705: after merging main, store.py was over the 800-line cap. The line-level block helpers (fence, list item, paragraph break, indented code, comment resume, the `-->` and span look-ahead) move unchanged, bar their names, to `memory/blocks.py`; nothing else reads them. `tests/test_memory_blocks.py` pins each of them on its own, since the mutation job reads the moved lines as changed. Signed-off-by: Anton Volkov <antonwolfy@gmail.com>
|
Ready for another look at Declined, with the evidence in the reply:
Follow-up issues opened:
|
napetrov
left a comment
There was a problem hiding this comment.
Change class: ARCHITECTURAL (head b75f458)
- Contract: candidate. defaults dokimi/dokimi/memory/store.py:258; defaults dokimi/dokimi/memory/store.py:423
- Responsibility: changed. move span_end from dokimi/dokimi/memory/store.py to dokimi/dokimi/memory/blocks.py; move indented_code from dokimi/dokimi/memory/store.py to dokimi/dokimi/memory/blocks.py
- Dependency: none
- Review depth: design read: new dependency/responsibility, ask for rationale
- Coverage gaps: none
Checked how paragraph offsets advance, reset and discard a completed link, including the case from the previous review. All 167 focused tests passed and CI is green. The remaining cost of a link left open across lines with code spans is tracked in #1459.

Summary
dokimi memoryblanks code, comments and fences line by line, and for every line it joined the blanked paragraph above the line (_paragraph_above, which also walked back to the paragraph start), builtbefore + "\n" + linein_BlankedLineand scanned it for](, and built it a third time in_unpaired_run. A paragraph of n lines cost O(n^2) (#1261). Onmain,prose_lines("wordaword\n" * n)takes:mainThat is 3.7 times per doubling. The fix takes the option the issue lists: the paragraph above is no longer passed as joined text.
_blank_blockskeeps the start of the current paragraph as the scan goes (reset after a raw blank line or a fence line, not after a line blanked to spaces, as_paragraph_abovedid), the offset of each row above, and the offset of the last](in them. A line with no](and no backtick in an angle destination never copies or scans the paragraph above, also when a link is left open above it ("[x](<y>\n" + "word word\n" * 400joined 804,807 characters at 400 lines and none now):_Abovesettles its last](when a line reads it, not on every line. The opener of a](is looked up by offset in the rows, and the text of a link is joined only from the row of its](on, when a](that may still match is read. A](whose link the rows above already decide (it closes in them, or what follows its blanks is no<) is dropped as the scan goes, so a link finished on an earlier row is not read again by every later line with a code span:"[x](<y>) z\n" + "wordaword\n" * njoined 1.76, 7.02 and 28.0 million characters at n = 500, 1000 and 2000 before, and none now. The blanked lines, the open-comment state and the unpaired run are unchanged for every input.Closes #1261
The search for the same cause in
_span_closes_ahead,_close_ahead,_title_spans,_title_closes_belowand_used_labelsfound_title_spans, which joined the lines of a title again for every line the title runs over, quadratic in the lines of one title; #1430 (merged intomain, and into this branch) rewrote it so that each line of a title is read once, so it is not a follow-up of this PR. An index of many definitions that each open a title ([a0]: x0 "tand so on) was worse than quadratic (0.36, 2.3 and 16 s at 500, 1000 and 2000 lines): that was #1263, fixed by #1430. One shape is left: a link that stays open over the rest of its paragraph ([x](<y> "and no closing quote) is joined from its row on by every later line with a code span, 1.76, 7.02 and 28.0 million characters at n = 500, 1000 and 2000, tracked in #1459.Changes
dokimi/dokimi/memory/store.py:_Abovekeeps the shared blanked rows, the paragraph start, the row offsets and the last](whose link the rows have not settled (_Above._settle, run whenlast_openis read, which a line with a backtick or a](does and a plain line does not);_Textreads the paragraph above and the line by offset without joining them;_LinkCache.endreads the link from the row of its](on._blank_blocks,_BlankedLine,_unpaired_runand_angle_link_enduse them, and_paragraph_aboveis gone._blank_inline,_unpaired_runand_angle_link_endstill take the paragraph above as a string (tests/test_blank_inline.pycalls them so), and also an_Above.dokimi/tests/test_blank_paragraph.py(new): the reads of the paragraph above counted for a link settled in the rows above (closed on its row, closed by a title two rows long, a destination that is no<, followed by code spans or unpaired backticks), the text each settling reads counted for a link on every row (each with a code span), a line with no backtick that reads no link above it ([x](<y>and plain lines), a link still open read from its own row on, and the growth ofprose_linesin the lines of a paragraph for three shapes (wordawordper line,[x](<y>) wordawordper line, a[opener at the top), withtests/growth_timing.py; the output against the old joined-paragraph behaviour kept in the test, for counterexamples (a](<destination that goes on at the next line, an opener many lines above, a fence in the middle, a line blanked to spaces, a paragraph where every line holds a](, a second paragraph) and 2500 seeded random multi-line texts.dokimi/tests/test_blank_inline.py:test_the_paragraph_above_is_copied_with_its_newline_once_not_once_per_spancounted a newline appended to thebeforestring (twice). The paragraph is no longer a string that is appended to, so it is nowtest_the_paragraph_above_is_not_copied_for_a_link_on_the_line: it asserts that every text read for a link on the line holds no row above. The_has_openercounting tests (two lookups) are unchanged. One shape (](<a>)b``, a](at column 0) is added to `SHAPES`.dokimi/tests/test_memory_index_definitions_linear.py(from fix(memory): an index of many reference definitions is linted in linear time #1430): two tests counted calls of_paragraph_above, which this branch replaces; they count the reads of the paragraph above (_Text.tail) on the same inputs and assert none, under the namestest_the_paragraph_above_is_not_read_for_a_line_with_no_code_span_or_commentandtest_the_paragraph_above_is_not_read_for_a_backtick_left_over_when_no_link_is_above.dokimi/dokimi/memory/store.py(_has_opener,_pieces,_Text.pieces): the walk back from a]to its[reads the text one row at a time and, inside a row, backwards from the]one character at a time, stopping at the opener, instead of indexing each character through_Text.__getitem__(abisectper character) or listing every bracket of the row before looking at the nearest (which made a row of many links quadratic in its links). The answers are the same;_has_openertakes a string or a_Text. Ona](bc`` repeated per line with no[, `prose_lines` takes 0.05, 0.20 and 0.76 s at 500, 1000 and 2000 lines at this head (0.33, 1.37 and 5.58 s before the row-wise lookup and 0.09, 0.37 and 1.45 s on `main`, each measured earlier). The walk is still quadratic on that input, as on `main`. On `x `c` ` repeated 500, 1000 and 2000 times on one line it takes 0.004, 0.008 and 0.017 s.dokimi/tests/test_blank_inline.py: the characters read for one opener lookup over 300 rows are counted (under 10), a table gives the same answers on the rows and on the joined text, and the text and tail of the rows above are compared with the joined text.test_the_opener_of_each_link_of_one_row_is_found_without_reading_the_brackets_before_ittimes the lookup of the opener of every link of a row of[x](a.md)withtests/growth_timing.py.dokimi/dokimi/memory/titles.py(new) anddokimi/dokimi/memory/store.py: with the paragraph changestore.pywas 817 lines, over the 800-line cap (tests/test_growth_ratchet.pyfailed on the Python 3.14 and wheel legs).title_spans,title_last_lineand the two tables they read move unchanged totitles.py, which leavesstore.pyat 779 lines.tests/test_memory_index_definitions_linear.pyimportstitle_spansfrom the new module.dokimi/dokimi/memory/blocks.py(new),dokimi/dokimi/memory/store.pyanddokimi/tests/test_memory_blocks.py(new): after the second merge ofmain(fix(memory): a code span before an angle destination is not whitespace to the link check (#1343) #1436 added the filled lines)store.pywas 824 lines, over the 800-line cap again. The line-level block helpers (fence_opener,closes_fence,span_end,indented_code,paragraph_break,close_ahead,span_closes_ahead,resume_commentand the fence, list-item and block-start patterns) move toblocks.pywith their code unchanged, which leavesstore.pyat 758 lines.test_memory_blocks.pychecks each on its own.Risk
The output is unchanged. The differential test compares
prose_lineswith the old behaviour (the joined paragraph fed to the line scan, with the_has_openerand link checks rewritten as stateless text functions) on the counterexamples and on 2500 seeded random texts, and the existing exhaustive and seeded tests of_blank_inlinepass.This branch merges
main, which has #1430 (the lazy join of the paragraph above, and the line-by-line title scan). The conflict was indokimi/dokimi/memory/store.py:_Abovereplaces #1430's_paragraph_start,_paragraph_aboveand callablebefore(a code span still read the joined paragraph there, so thewordawordrepro above stays quadratic with them), and #1430's_title_last_lineand_title_spansare kept asmainhas them (now inmemory/titles.pyastitle_last_lineandtitle_spans). #1436 (the filled lines, in which a blanked span is_FILLto a link check) has since merged, and so has this branch's second merge ofmain: the conflicts instore.pywere kept both ways._Abovenow holds the filled rows, extended with the lines blanked whole at the top of each iteration, in place of #1436's joined_paragraph_above;_unpaired_runtakeslinked(the line with_FILLfor a blank) and reads its links and](offsets on it;test_blank_paragraph.pycompares the filled lines too.Testing
On a Linux review host, at
b75f4586(onmainbac7f7ec5f0c):make ci-local(run at the commit before a comment-only waiver inblocks.py;--offline --jobs static,typecheckrun again atb75f4586, 17 passed, 0 failed, 3 skipped): 38 passed, 0 failed, 16 skipped (the steps that need docker, a network or a tool this host lacks, the matrix legs on other Pythons, the PR-only steps). 32228 tests passed, 27 skipped, includingtests/test_growth_ratchet.py; mutation of changed lines: 76 killed, 1 survived (a flip of the initialFalseofclose_ahead, now waived as equivalent), 3 waived.titles.py(12 tests oftest_memory_index_definitions_linear.pyfail), and 30 lines added tostore.py(test_no_file_or_function_grew_past_its_baselinefails).main's code the three growth tests fail (ratio 170, 205 and 167 against the limit of 64); on this branch they pass. The differential test and the counterexample tests pass on both, as they pin the output.](ignored, off by one, or left over from the paragraph before, a character off by one in the rows, the opener lookup always true, the paragraph rescanned per line, a wrong row index and a wrong offset of the link text.mainwithout the settling (the tree before the last commit), six new tests oftest_blank_paragraph.pyfail (test_a_link_settled_in_the_rows_above_is_not_read_again_for_each_lineon its five shapes, where the rows above are joined for every code span or unpaired backtick, andtest_a_link_is_settled_from_its_own_row_not_from_the_start_of_the_paragraph, where nothing is settled), and pass with it;"[x](<y>) z\n" + "wordaword\n" * njoins 1.76, 7.02 and 28.0 million characters at n = 500, 1000, 2000 without it and none with it.](with no<after its blanks left in place, every](dropped (a link still open is lost), the check on what follows the blanks off by one at the end of the rows, the row of the](not recorded (the rows are joined from the paragraph start), its offset added instead of subtracted, and the paragraph above read for every line._Above.syncon every line again (test_a_line_with_no_backtick_does_not_read_the_link_left_open_above_itfails), and settling from the start of the paragraph (test_a_link_is_settled_from_its_own_row_not_from_the_start_of_the_paragraphfails)._Text.__getitem__returningNonefor a character of the line or of a row, and the row index of_Text.__getitem__,_Text.tailand_Text.piecestaken with the wrong sign._has_openerlisting every bracket of the row again (test_the_opener_of_each_link_of_one_row_is_found_without_reading_the_brackets_before_ittimes out at 60 s),paragraph_breakwithout the heading and quote start (test_a_paragraph_ends_at_a_blank_line_a_fence_a_list_item_a_heading_or_a_quote, 2 cases fail), andclose_aheadthat does not reset at a paragraph break (test_a_close_ahead_is_a_close_later_in_the_same_paragraph, 3 cases fail).Open questions
None.
Revert
A plain revert restores the quadratic scan and the old
_paragraph_above. 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)