Skip to content

Markdown: memoise settled blocks, list items and table rows, and measure only changed table rows - #1130

Merged
Hona merged 6 commits into
devfrom
stream-markdown-memo
Nov 18, 2026
Merged

Hona merged 6 commits into
devfrom
stream-markdown-memo

Conversation

@Hona

@Hona Hona commented Nov 17, 2026 •

Copy link
Copy Markdown
Member
  • stream_cached parses incrementally from the last block cut during streaming, and this PR wraps settled blocks in blocks_with_dirs, settled non-task list-item bodies in Block::List (keeping position_in_set(k + 1, real_n) live on the outer listitem row), closed code cards inside Block::Code (when code_frame is set), and settled header/body rows in Block::Table with rune::memo::cached (crates/rune-kit/src/markdown/memo.rs). Advancing next_source and next_task across hits keeps later code-card and task indices aligned while the runtime's reusable check reuses FlattenedMemo subtrees.
  • Precompute each block's, list item's, and table row's 128-bit content digest (PairHasher, two independent OS-seeded SipHash-1-3 lanes with no 64-bit intermediate narrowing) once at parse time (Rc<PreBlock>, PreItem, PreTable, PreRow, computed iteratively in O(1) call stack up to #1122's 511/770 bounds) and carry meta: Rc<[Rc<PreBlock>]> across cache::extend alongside starts, maps, and eof_deep_tail. Wrapping PreBlock in Rc<PreBlock> means cache::extend clones 8-byte Rc pointers instead of deep-cloning settled PreBlock trees per token; settled blocks and rows without code_link rewrites read their parse-cached 128-bit content digest directly (0 text re-hashing and 0 Vec allocations per token), and Renderer, Span, and Flow are destructured exhaustively so adding a field fails to compile until classified.
  • Add ROW_WIDTHS in crates/rune-kit/src/markdown/table.rs (keyed by 128-bit (u64, u64) with rw.natural.len() == n && rw.fit.len() == n bounds checks) to cache per-row unpadded (natural, fit) widths by (cells, font): measuring (content_widths) runs once per new or edited row, while per-token keying mixes each settled row's cached PreRow digest (O(R) fixed-size tuple hashes when the table is still open) and a newly settled row that widens a column changes cols in every row's digest so all rows rebuild with the wider column widths.
flowchart LR
  D["Streamed delta"] --> P["stream_cached + cache::extend / extend_eof_deep_tail<br/>(clone Rc&lt;PreBlock&gt; or rebuild trailing DeepTail spine)"]
  P --> B{"Block / item / row /<br/>closed card settled?"}
  B -->|Yes| M["rune::memo::cached hit<br/>(FlattenedMemo + ROW_WIDTHS hit)"]
  B -->|No: open tail| R["Build & shape open tail;<br/>measure only new table row"]
Loading

Scope in OpenCode (Rune)'s configuration

OpenCode (Rune) configures Markdown with code_frame, diagram_frame, link_icons + link_favicon, images + image_frame (via picture_body), zero-capture on_link_tx / on_link_secondary, link_help, reveal, selectable, and scroll_wide:

  • Memoised during streaming: settled paragraphs and headings (including those with external links and link_favicon, whose Pixels id/size/generation is hashed into the link digest), settled bullet and ordered list items without code/images/interactive tasks, settled table headers and rows (and closed tables as whole blocks), closed Block::Code inner cards inside code_frame, Block::Diff, and Block::Rule.
  • Where the memo intentionally refuses (builds as before):
    • Top-level Block::Code outer wrapper: code_frame builds the outer frame and Copy button overlay each commit; only the closed inner card is memoised (and open fences build until closed).
    • Lists and block quotes containing code or images: any list item, list, or quote containing Block::Code, Block::Mermaid (with diagram_frame), or Block::Image (with images/on_image) is refused at the enclosing item/block level.
    • Mermaid diagrams (diagram_frame) and images (images / on_image): never memoised here (rune-mermaid has its own scene cache).
    • Interactive task-list items (on_task without tasks_read_only): never memoised as items or enclosing blocks.
    • Whole-reply rebuilds (same cost as before): flipping streaming to false at stream end, changing Find highlights, changing theme, or resizing text_width changes base_hash and rebuilds every block once.

Verification, cache budget, & #1122 (307b2cd7) merge (2ec440b2)

  • Our earlier "43 of 43 tree checks" verification ran only the standalone review-gate driver (0 diff between token-streamed and from-scratch trees) and did not run the rune-kit test suite, which is why rune_kit::markdown::tests::a_stream_drops_the_strings_it_grew_from slipped at 859b23a1.
  • de8cfce0 → 7ab79a40 wrapped PreBlock in Rc<PreBlock> and added iterative PreBlock::heap_bytes() (memo.rs + cache.rs:267) counting nested items, child Rc<PreBlock>s, table rows, and copied link URLs (0 on plain top-level paragraphs, preserving a_stream_drops_the_strings_it_grew_from's formula while bounding list/table/link-heavy replies in the 16 MiB cache).
  • 2ec440b2 merges dev 307b2cd7 (#1122): preserves Parsed.eof_deep_tail, BlockMap::DeepTail + unselectable Role::Note MoreNestedLines marker, QuotePlainCache key, and 511/770 iterative bounds; invalidates and rebuilds Parsed.meta's trailing PreBlock/PreItem spine in cache.rs::replace_eof_deep_tail_meta whenever extend_eof_deep_tail replaces the tail block while render.rs::run_frames threads open_tail (!(self.streaming && is_deep_tail) + !child_open_tail) so nothing on the active tail spine calls rune::memo::cached and any superseded MemoStore entry is dropped on commit apply.
  • Verified (2ec440b2): all 439 / 439 existing rune-kit tests (0 failed, 3 ignored, a_stream_drops_the_strings_it_grew_from included) passed across all 50 binaries on adais (proof/proof-farm/03eea674-0c60-41eb-99b2-d860eca01c68), and proof/stream/pr1130-gate/summary.json (OPENCODE_RUNE_OTEL=0) passes 64 / 64 incremental-vs-fresh tree checks (0 mismatches) and 7 / 7 DX12 PNG pairs (0 differing RGBA pixels, including deep-tail-cap streaming across #1122's 255-list + 64-item cap and extend_eof_deep_tail with 0 settled-block rebuilds).

…ure only changed table rows

During streaming (`Markdown::streaming`), `stream_cached` parses incrementally from the last block cut, but `Renderer` previously rebuilt the `El` subtree of every settled block, list item and table row on every appended token, and `Table::columns` re-ran `rune::text::content_widths` across every settled row whenever a row finished.

- Memoise settled blocks in `Renderer::blocks_in`, settled list-item bodies in `Block::List`, closed code cards inside `Block::Code` (`code_frame`), and settled header/body rows in `Block::Table` via `rune::memo::cached`, advancing `next_source` and `next_task` deterministically across hits so `commit::flatten` and Taffy reuse unchanged `FlattenedMemo` subtrees.
- Cache per-row unpadded `(natural, fit)` column widths in `ROW_WIDTHS` (`crates/rune-kit/src/markdown/table.rs`) so adding or editing a row calls `rune::text::content_widths` only on the changed row (`O(1)` per row instead of `O(R)`).
Copilot AI balanced review requested due to automatic review settings November 17, 2026 11:14

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.

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota.

An organization owner can choose the repository organization as the billing source for reviews requested by members with a paid Copilot license. Learn about Copilot code review billing.

@Hona

Hona commented Nov 17, 2026

Copy link
Copy Markdown
Member Author

Windows whole suite (-p rune -p rune-kit) on adais, judged against dev's own failures at 32eb7811 (this lane, whole suite) and eb5ae6c9 (FUNCTION's run, rune only) and 3468f3a3 and cca5df6f: #1130 89857c6b (18 Nov 05:12): 2840 passed, 19 failed in 170 binaries; also on dev: alloc::disabled_trace_hooks_neither_format_nor_allocate, canvas_input::the_middle_button_drags_only_elements_that_take_it, rune::edit::tests::hidden_multiline_pristine_replace_and_delete, rune::edit::tests::pill_edge_cases_words_lines_bidi_and_direct_text_mutation, rune::edit::tests::the_caret_steps_over_a_pill_in_one_move, rune::input::keycaps::tests::repeated_chords_count_and_named_keys_use_glyphs, rune::native_surface::gpu_tests::mirrored_video_is_exact_cropped_centred_and_toggles_without_a_new_frame, rune::render::breaker::tests::a_refused_blur_pipeline_draws_unblurred, rune::render::layers::cache_tests::cache_matches_uncached_at_rest_and_skips_rerasterize_on_ancestor_live_offset, rune::render::layers::cache_tests::live_scale_zoom_sweep_buckets_during_motion_and_settles_crisp, rune::render::layers::filter_tests::filtered_video_and_image_page_match_primitive_source, rune::render::layers::rebind_tests::a_texture_switch_binds_no_parked_target_until_one_returns, rune::render::layers::tests::clipped_single_draw_fades_do_not_allocate_targets, rune::runtime::tests::layout_rows::send_flight_follows_the_measured_bubble_while_earlier_rows_land, rune::runtime::tests::picture_scale::a_changing_static_scale_keeps_the_glyph_atlas_bounded, rune::runtime::tests::picture_scale::text_inside_a_scale_draws_at_that_size, rune::runtime::tests::scroll_children::scroll_xy_with_direct_children_scrolls_both_axes_and_warns_on_trailing_growth_sibling, rune::text::macos::smoothing::tests::pipelines_never_share_masks, selectable_text::copy_select_all_and_the_context_menu; none new. Base 3d037b49. Proof 2c460ce7.

Hona added 5 commits November 18, 2026 07:50
…hs, and link_favicon memoisation

Address review findings on settled Markdown block/item/row/card memoisation:
- Guard `ROW_WIDTHS` and `WIDTHS` lookups and folding by column count (`rw.natural.len() == n && rw.fit.len() == n`) and key both by full 128-bit `PairHasher` digests so a cache collision cannot panic or narrow to 64 bits.
- Replace intermediate 64-bit `DefaultHasher` reductions with a 128-bit OS-seeded `PairHasher` (`(RandomState, RandomState)`), and destructure `Renderer`, `Span`, and `Flow` exhaustively so any future field fails to compile until classified.
- Precompute each parsed block's, list item's, and table row's 128-bit content digest once at parse time (`PreBlock` / `PreItem` / `PreTable` / `PreRow`) and carry `meta` across `cache::extend` alongside `starts` and `maps`, avoiding per-token text re-hashing and `Vec` allocations across settled blocks and rows.
- Hash `link_favicon` (`Pixels` id, dimensions, and generation) into link digests without heap allocation instead of refusing paragraphs or list items with favicons.
@Hona
Hona merged commit 1505f92 into dev Nov 18, 2026
1 check passed
Hona added a commit that referenced this pull request Nov 21, 2026
…1292)

* feat(rune-kit): native code-block soft wrapping and text-wrap icon

Add native code-block soft wrapping to `rune-kit::markdown` and the
`text-wrap` icon (`icons::TEXT_WRAP`) to `rune-theme-opencode` for
`anomalyco/opencode#64545`:

- `MarkdownStyle::wrap_code(bool)`, `Markdown::wrap_code(bool)`, and
  `Markdown::code_wrap(Fn(usize, &str) -> bool)` wrap long lines inside
  `Renderer::code` via `.by_line().wrap_anywhere().wrap_greedy()` while
  keeping `#1133` per-line shaping, screen-reader `TextRun` line runs,
  `#1130` `settled_code_card_digest` memoisation, Find highlights,
  streaming reveal, `.copy_breaks(2, 2)`, non-mono tab stops, and shell
  block colours.
- Unwrapped blocks (`!wrapped`) remain identical to before.
- `Source` computes longest-line tab-expanded stats once at parse time;
  `CodeFrame::overflows` answers overflow against `text_width - flow.inset`
  in O(1) on the ASCII monospace fast path (falling back to
  `rune::text::measure` only near the boundary or for non-ASCII/non-mono).
- `CodeFrame::wrapped` reports the block's active wrap state.
- `rune-theme-opencode::icons::TEXT_WRAP` adds Desktop's `text-wrap` SVG.

* fix(rune-kit): address review #1292 findings (stable body path, lazy exact overflows, lock-free font_epoch, line reuse, scoped memo)

* fix(rune-kit): revert paragraphs.rs scope creep, clear ascii/paragraphs on font switch, Acquire/Release font_epoch, bound control chars
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.

2 participants