Skip to content

Add memo_on for caching a section by an explicit input - #96

Merged
wtholliday merged 4 commits into
mainfrom
memo-on
Nov 28, 2026
Merged

wtholliday merged 4 commits into
mainfrom
memo-on

Conversation

@wtholliday

Copy link
Copy Markdown
Contributor

memo_on(input, f) caches the layout and accessibility nodes of the view f builds from input, apart from whatever the enclosing views read. f is a function pointer, so input is the only data that reaches it from the views around it; anything else it uses comes through the Context, whose reads are tracked. A write to an enclosing state leaves it alone unless input changed, even when the enclosing function read that whole state.

How it works

  • The input (which must be Eq) and the function's address are kept on a child node of the memo_on, so a list caching an item on the item's node can't overwrite them. A different input or function bumps write_gen without writing anything, making the results cached under it stale, and sets dirty.
  • Inside a memo_on, ancestor_gen starts at the input's generation, and a read of a state above it (through a handle in the input, say) is a foreign read, so it's still followed.
  • Lists inside a memo_on don't depend on what was read above it: the written-items check skips those reads, and the key sync keeps the keys without comparing them when nothing the memo_on depends on changed and every write under it was in the list's items.
  • StateHandle gets PartialEq/Eq (by id), so a handle can be part of an input.

gui_bench

The orders card is now a memo_on of what it shows (the rows, the shown ids by pointer, the sort). profile_hover gets tick (a live update's write to the app state) and hover_tick cases. Per update, release build:

case rows before after
tick 3,000 0.061 ms 0.017 ms
tick 1M 0.49 ms 0.018 ms
hover_tick, the hover event 1M 0.41 ms 0.0075 ms (same as a plain hover)

idle, hover and invalidate_all are unchanged. The gui-bench repo's copy of the app (apps/rui) still has the old orders card.

Tests

Unit tests in src/node_tests.rs cover: writes the input doesn't see, a new input, an enclosing state read through a handle in the input (directly and from a nested state), writes under it, accessibility, a memo_on as a list item root, switching functions, lists inside it (written items only, keys kept, keys from the input or a state under it), the redraw on a new input, the anim pass, and a new input first seen after layout. The boundary checks were mutation-tested: removing each makes a test fail. The app hasn't been run in a window.

🤖 Generated with Claude Code

wtholliday and others added 4 commits November 27, 2026 20:57
memo_on(input, f) caches the layout and accessibility nodes of the view
f builds from input. f is a function pointer, so input is the only data
that reaches it from the views around it. Inside a memo_on, writes to
the enclosing states don't invalidate anything unless input changed;
reads of states above it through handles in the input count as foreign
reads, so they're still followed. StateHandle gets PartialEq so a handle
can be part of an input.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
A list caches each item on the item's node, whatever its root. With a
memo_on at an item's root, the list overwrote the memo_on's cache there
with reads filtered for the list, which dropped reads of enclosing states
through handles in the input, and with a stamp older than the input's
generation. memo_on now keeps its input and cached results on its child
node and builds its view under that one.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The orders card was a memo reading the whole App state, so every write
to it, such as a live update tick every 100 ms, laid the card out again
and made the list compare all its keys. The card is now a memo_on of
what it shows (the rows, the shown ids, the sort), so those writes leave
it alone. profile_hover gets a tick case for such a write: 0.49 ms to
0.018 ms per update with 1M rows, 0.061 ms to 0.017 ms with 3000.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
A list inside a memo_on now keeps its keys without comparing them when
the memo_on's input, the states its last layout read from outside it,
and everything under it but the list's items are unchanged since the
list was synced in that layout. In gui_bench with 1M rows, the hover
event after a live update tick drops from 0.41 ms to 0.0075 ms.

Also from review:
- The list's written-items check ignores reads above the memo_on.
- memo_on's input must be Eq, so a NaN can't make it miss every walk.
- A new input seen after layout sets dirty, so a layout follows.
- profile_hover gets a hover_tick case; the README lists tick and
  hover_tick.
- Tests for the redraw on a new input and the anim pass in memo_on.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
wtholliday added a commit to audulus/gui-bench that referenced this pull request Nov 28, 2026
rui's memo-on branch (audulus/rui#96) is main after audulus/rui#95, which
caches each list item and adds memo, plus memo_on. The app is synced with
rui's examples/gui_bench: the orders card is a memo_on of what it shows,
so a live update's write to the app state no longer lays it out or makes
the list compare all its keys.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@wtholliday
wtholliday merged commit f3e273b into main Nov 28, 2026
4 checks passed
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.

1 participant