Skip to content

fix(memory): lint a paragraph of many lines in linear time - #1439

Merged
antonwolfy merged 9 commits into
intel:mainfrom
antonwolfy:fix/memory-paragraph-above-linear
Dec 1, 2026
Merged

antonwolfy merged 9 commits into
intel:mainfrom
antonwolfy:fix/memory-paragraph-above-linear

Conversation

@antonwolfy

@antonwolfy antonwolfy commented Nov 30, 2026 •

Copy link
Copy Markdown
Contributor

Summary

dokimi memory blanks 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), built before + "\n" + line in _BlankedLine and scanned it for ](, and built it a third time in _unpaired_run. A paragraph of n lines cost O(n^2) (#1261). On main, prose_lines("word a word\n" * n) takes:

n CPU time on main
250 0.011 s
500 0.036 s
1000 0.129 s
2000 0.486 s

That 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_blocks keeps 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_above did), 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" * 400 joined 804,807 characters at 400 lines and none now): _Above settles 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" + "word a word\n" * n joined 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_below and _used_labels found _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 into main, 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 "t and 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: _Above keeps the shared blanked rows, the paragraph start, the row offsets and the last ]( whose link the rows have not settled (_Above._settle, run when last_open is read, which a line with a backtick or a ]( does and a plain line does not); _Text reads the paragraph above and the line by offset without joining them; _LinkCache.end reads the link from the row of its ]( on. _blank_blocks, _BlankedLine, _unpaired_run and _angle_link_end use them, and _paragraph_above is gone. _blank_inline, _unpaired_run and _angle_link_end still take the paragraph above as a string (tests/test_blank_inline.py calls 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 of prose_lines in the lines of a paragraph for three shapes (word a word per line, [x](<y>) word a word per line, a [ opener at the top), with tests/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_span counted a newline appended to the before string (twice). The paragraph is no longer a string that is appended to, so it is now test_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_opener counting 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 names test_the_paragraph_above_is_not_read_for_a_line_with_no_code_span_or_comment and test_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__ (a bisect per 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_opener takes a string or a _Text. On a](b c`` 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_it times the lookup of the opener of every link of a row of [x](a.md) with tests/growth_timing.py.
  • dokimi/dokimi/memory/titles.py (new) and dokimi/dokimi/memory/store.py: with the paragraph change store.py was 817 lines, over the 800-line cap (tests/test_growth_ratchet.py failed on the Python 3.14 and wheel legs). title_spans, title_last_line and the two tables they read move unchanged to titles.py, which leaves store.py at 779 lines. tests/test_memory_index_definitions_linear.py imports title_spans from the new module.
  • dokimi/dokimi/memory/blocks.py (new), dokimi/dokimi/memory/store.py and dokimi/tests/test_memory_blocks.py (new): after the second merge of main (fix(memory): a code span before an angle destination is not whitespace to the link check (#1343) #1436 added the filled lines) store.py was 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_comment and the fence, list-item and block-start patterns) move to blocks.py with their code unchanged, which leaves store.py at 758 lines. test_memory_blocks.py checks each on its own.
  • Changelog fragment (Fixed).

Risk

The output is unchanged. The differential test compares prose_lines with the old behaviour (the joined paragraph fed to the line scan, with the _has_opener and 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_inline pass.

This branch merges main, which has #1430 (the lazy join of the paragraph above, and the line-by-line title scan). The conflict was in dokimi/dokimi/memory/store.py: _Above replaces #1430's _paragraph_start, _paragraph_above and callable before (a code span still read the joined paragraph there, so the word a word repro above stays quadratic with them), and #1430's _title_last_line and _title_spans are kept as main has them (now in memory/titles.py as title_last_line and title_spans). #1436 (the filled lines, in which a blanked span is _FILL to a link check) has since merged, and so has this branch's second merge of main: the conflicts in store.py were kept both ways. _Above now 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_run takes linked (the line with _FILL for a blank) and reads its links and ]( offsets on it; test_blank_paragraph.py compares the filled lines too.

Testing

On a Linux review host, at b75f4586 (on main bac7f7ec5f0c):

  • make ci-local (run at the commit before a comment-only waiver in blocks.py; --offline --jobs static,typecheck run again at b75f4586, 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, including tests/test_growth_ratchet.py; mutation of changed lines: 76 killed, 1 survived (a flip of the initial False of close_ahead, now waived as equivalent), 3 waived.
  • Two mutants of the split, each fails a test: a closing quote followed by text accepted as the end of a title in titles.py (12 tests of test_memory_index_definitions_linear.py fail), and 30 lines added to store.py (test_no_file_or_function_grew_past_its_baseline fails).
  • On 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.
  • Eleven mutants, one break each, each fail a test: the fence not ending the paragraph, a blanked line ending it, the line's own link read with the rows above joined, the last ]( 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.
  • On the merge of main without the settling (the tree before the last commit), six new tests of test_blank_paragraph.py fail (test_a_link_settled_in_the_rows_above_is_not_read_again_for_each_line on its five shapes, where the rows above are joined for every code span or unpaired backtick, and test_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" + "word a word\n" * n joins 1.76, 7.02 and 28.0 million characters at n = 500, 1000, 2000 without it and none with it.
  • Seven more mutants, one break each, each fails a test: a link that closes in the rows left in place, a ]( 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.
  • Two more mutants, each fails a test: settling in _Above.sync on every line again (test_a_line_with_no_backtick_does_not_read_the_link_left_open_above_it fails), and settling from the start of the paragraph (test_a_link_is_settled_from_its_own_row_not_from_the_start_of_the_paragraph fails).
  • Nine more mutants for the row-wise opener lookup, each fails a test: a blank row not ending the search, an escaped bracket counted, every character of the text read before the lookup, a row skipped in the walk, _Text.__getitem__ returning None for a character of the line or of a row, and the row index of _Text.__getitem__, _Text.tail and _Text.pieces taken with the wrong sign.
  • Mutants of the opener lookup and the split, each fails a test: _has_opener listing every bracket of the row again (test_the_opener_of_each_link_of_one_row_is_found_without_reading_the_brackets_before_it times out at 60 s), paragraph_break without 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), and close_ahead that does not reset at a paragraph break (test_a_close_ahead_is_a_close_later_in_the_same_paragraph, 3 cases fail).
  • Not run: Python 3.11, 3.12 and 3.14 (this ran on one version), a Windows runner.

Open questions

None.

Revert

A plain revert restores the quadratic scan and the old _paragraph_above. 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)

_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>
@antonwolfy
antonwolfy requested a review from a team as a code owner November 30, 2026 13:14
@antonwolfy antonwolfy self-assigned this Nov 30, 2026
@exai-bot

exai-bot commented Nov 30, 2026 •

Copy link
Copy Markdown

✅ OpenCodeReview: Found nothing that must be fixed in this commit.

CI at b75f458: 21 passing, 4 skipped.

Review limit reached. This PR has had 6 reviews, so it will only be reviewed again when you ask.
To ask: re-request review from exai-bot in the Reviewers list.

Other findings not posted (1)

1 should fix

Test notes (2)
  • dokimi/dokimi/memory/blocks.py:83: With if end < 0: changed to if end <= 0:, the tests still pass: dokimi/tests/test_blank_paragraph.py, dokimi/tests/test_memory_blocks.py, dokimi/tests/test_memory_index_definitions_linear.py. A value exactly at the boundary gives the same result either way here, so no test says which one is meant.
  • dokimi/dokimi/memory/store.py:265: With at = above.head + found if found >= 0 else above.last_open changed to at = above.head + found if found > 0 else above.last_open, the tests still pass: dokimi/tests/test_blank_paragraph.py, dokimi/tests/test_memory_blocks.py, dokimi/tests/test_memory_index_definitions_linear.py. A value exactly at the boundary gives the same result either way here, so no test says which one is meant.
Review details
  • Commit reviewed: b75f458 against merge base bac7f7e
  • Coverage: 8 files in 1 groups
  • Engine: OpenCodeReview v1.13.5-exai.4.8f73eada, us.openai.gpt-6.1-sol
  • Usage: 449k tokens (289k from cache), 1m23s
  • Tool calls: 28 (16 file_read, 4 code_search, 3 pr_threads, 2 ci_status, 2 local_check_results, 1 code_comment)
  • Linear paragraph memory storage, block and title handling, and regression tests: 8 files

@github-actions

github-actions Bot commented Nov 30, 2026 •

Copy link
Copy Markdown

Mutation of changed lines

⚠️ No break was missed, but only 76 of 80 were caught; the rest were waived, timed out or errored, and 353 more were over the limit

At b75f458: 76 caught · 0 missed · 4 waived

Not checked:

  • 353 more breaks of covered changed lines were over the limit and not tried
  • 4 changed lines were not mutated

Marked harmless with mutation-ok:

Where Operator Change Reason
dokimi/dokimi/memory/blocks.py:56 flip-bool False → True the loop sets every row
dokimi/dokimi/memory/store.py:210 drop-clause code and not pos → (code) a span is open only after a prose line, which clears ``code``
dokimi/dokimi/memory/store.py:269 flip-bool True → False the cache lives for this one call, and ``final`` only marks a ``](`` dead for th
dokimi/dokimi/memory/store.py:286 delete-statement self._open_row = 0 → pass read only once _open is set, with the row

Warning only: a missed break does not fail the job. Updated on every push; the full report is in the ci run summary.

@codecov-commenter

codecov-commenter commented Nov 30, 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.32%. Comparing base (bac7f7e) to head (b75f458).
❗ Your organization needs to install the Codecov GitHub app to enable full functionality.

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     
Files with missing lines Coverage Δ
dokimi/dokimi/memory/blocks.py 100.00% <100.00%> (ø)
dokimi/dokimi/memory/store.py 98.66% <100.00%> (+0.75%) ⬆️
dokimi/dokimi/memory/titles.py 100.00% <100.00%> (ø)

... and 7 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: 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.

Comment thread dokimi/dokimi/memory/store.py Resolved
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>
@antonwolfy

Copy link
Copy Markdown
Contributor Author

(commented) ### Change class: CONTRACT (head 0b8913e)

@napetrov (#1439 (review)): Same point as the thread above: the growing suffix was the rows from the ]( of a link already finished on its own row, joined by every later line with inline code. Fixed in 14c0afe, with read-count tests for a link closed on its row, below the top of the paragraph, closed by a two-row title, a plain destination and a following unpaired backtick, and a count of the text read per settled link. main was also merged first (14f7833): _Above replaces #1430's lazy join, whose join a code span still triggered. The same cause in dokimi/dokimi/memory/store.py:328 is tracked in #1459.

@antonwolfy

Copy link
Copy Markdown
Contributor Author

Ready for another look at 14c0afe; CI is green.

Follow-up issues opened:

@antonwolfy
antonwolfy requested a review from napetrov November 30, 2026 19:12

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

Comment thread dokimi/dokimi/memory/store.py Resolved
… 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>
Copilot AI balanced review requested due to automatic review settings December 1, 2026 10:55
@antonwolfy

Copy link
Copy Markdown
Contributor Author

(commented) @antonwolfy, could you fix the problem I found (in the line comments)? Then push and I'll take another look.

@vchamarthi (#1439 (review)): The blocking point is answered in the thread above (2ce5c7f). The optional ](< left open by a title followed by lines with a code span is #1459, which the description already tracks; this PR leaves it.

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 / _Text offset-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

Comment thread dokimi/dokimi/memory/store.py Resolved
Comment thread dokimi/dokimi/memory/store.py Resolved
…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>
Comment thread dokimi/dokimi/memory/store.py Resolved
…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>
Comment thread dokimi/dokimi/memory/store.py Resolved
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>
@antonwolfy

Copy link
Copy Markdown
Contributor Author

Ready for another look at b75f458; CI is green.

Declined, with the evidence in the reply:

Follow-up issues opened:

@antonwolfy
antonwolfy requested a review from vchamarthi December 1, 2026 15:23

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

@antonwolfy
antonwolfy merged commit df0d798 into intel:main Dec 1, 2026
25 checks passed
@antonwolfy
antonwolfy deleted the fix/memory-paragraph-above-linear branch December 1, 2026 16:08
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: _blank_blocks joins the paragraph above every line, quadratic in the lines of a paragraph

6 participants