Skip to content

Make long lists fast: virtual layout, scrolling and hovering up to a million rows - #94

Merged
wtholliday merged 8 commits into
mainfrom
scroll-without-layout
Nov 28, 2026
Merged

wtholliday merged 8 commits into
mainfrom
scroll-without-layout

Conversation

@wtholliday

@wtholliday wtholliday commented Nov 27, 2026 •

Copy link
Copy Markdown
Contributor

Scrolling gui_bench's messages and overview pages was slow, and got slower with the row count: every wheel step laid out every message card again, and several other per-frame walks visited every card.

Profile (gui_bench messages page, 10,000 rows, synthetic wheel scrolling)

  • 92% of samples were in Context::update. Nearly all of it was List laying out every card: rebuilding each card's view and measuring its text again.
  • About 17% was the Event::Anim pass visiting every card.
  • After those two fixes, what was left was sync_keyed_children and the item loops in List::draw and List::process.

Changes

Commit 1: no relayout, no full anim pass

  • Scrolling isn't a state write. ScrollView used to write its position with cx[handle] = …, which invalidated the cached layout of every enclosing state view. It now stores the position without marking it written, sets the content's offset itself, and calls set_dirty to get a redraw.
  • The anim pass skips lists with no anim view. anim views mark their ancestors (has_anim) when laid out, so the marks are complete while nothing has been written since the last update's layout. A new Context::marked_gen records when that layout happened. List::process returns at once for Event::Anim when its node carries no mark, and skips items without one.

Commit 2: costs that no longer grow with the list

  • Faster key hash. List keys are hashed with a multiply-rotate hasher (as in rustc-hash) instead of SipHash. For integer keys it's a bijection, so distinct integers can't collide.
  • Contiguous keys. Each list node keeps its children's keys in one Vec<u64>, so checking that the keys are unchanged doesn't load every child node.
  • Binary search. A vertical or horizontal list finds the items to draw (the visible area, mapped into the list's space) and the items near the pointer for motion and wheel events by binary search over their laid-out positions. Items that were under the pointer before, or that hold a touch, are still added for those events, so hovers still see the pointer leave. A z-list still visits everything.
  • List::dirty returns at once when nothing under the list was written.

Commit 3: a million items, nothing per item per walk

  • Keys are only compared after a write. A list skips the key compare entirely when nothing has been written since its last sync (or invalidate_all), the same rule cached layouts already follow. Views are built from the states, so their keys are too. A changed number of keys is always caught.
  • list_shared, hlist_shared, zlist_shared take Arc<Vec<ID>>, so an app can hand a long list the same ids on every build instead of a new Vec. list and friends still take a Vec and wrap it.
  • Items that were under the pointer remember their slot, so finding them again doesn't look through every child.
  • gui_bench: the orders table found its shown orders by filtering every order on every walk (43% of the 1M-row profile). It now recomputes them only when the status filter, the search or the sort changes. Both long lists use list_shared. examples/gui_bench mirrors apps/rui in audulus/gui-bench, so that copy needs the same change.

Commit 4: a write inside one item costs nothing per item

Hovering a row in the orders table at 1,000,000 rows cost 115 ms per update. The hover's write invalidated the views around the list, which then visited every item again.

  • Write counts. Each node counts the writes under it. When every write since a list's last sync was in its items, its keys aren't compared. This tightens commit 3's rule: the views around a list must not build its ids from the states inside its items.
  • Written items only. A list records which items had writes under them. When its proposed size, the enclosing states, and the states its items read outside themselves are all unchanged, its layout visits only those items. If one of them changes size, it lays out every item. Each item's reads are now collected separately, so an item reading its own state doesn't count as a read from outside.
  • Dirty walk: visits only the written items.
  • Event::Anim pass: for a list with no anim view in it, visits only the written items, which are the only ones that can have gained one.

Commit 5: no second layout after the first frame

The first redraw arrives before the first update, and render didn't record the window size it laid out at. So the first update saw a new size and called invalidate_all: it laid out the whole UI again and visited every item in the animation pass. At 1,000,000 rows that update took 10 s, after the UI was already on screen. update and render now share the size check, so the first update reuses render's layout and takes 0.08 ms. The app now responds as soon as the UI appears (11.3 s after launch, down from 22.6 s). Most of those 11.3 s are building the data and the first layout of every row.

Commit 6: lay out a long vertical list only as far down as has been shown

A list in a scroll view starts at the top and is scrolled continuously. A row's place depends only on the rows above it, and those have all been reached by the time it's on screen.

  • Layout: a vertical list builds and lays out its items from the top down to as far as drawing has reached, plus one window height. The rest count at the average height of the rows laid out. That estimate only sets how far the scroll view can scroll, so rows on screen never move because of it.
  • Draw: when drawing reaches past the rows laid out, the list lays out more right away, so they show in the same frame. It then asks for a layout (Context::grow_list) to learn its new size.
  • Lazy nodes: item nodes are made the first time an item is reached, so a million-row list doesn't make a million nodes.
  • Walks: only rows that have been laid out take events, hit tests, accessibility nodes and menu commands. Behaviour change: the menu is gathered once, before any layout, so commands declared inside list items no longer reach it. Nothing in the repo does that. Before, gathering it walked every row, which was 67% of startup at 1M rows.
  • Unchanged: horizontal lists and z-lists still lay out every item.

Commits 7 and 8: review fixes

  • Proposed size: rows laid out while drawing get the same proposed size as in the list's layout.
  • Menu commands: before a list's first layout, every item takes part in walks again, so commands in list items are gathered. The unused startup commands walk in rui_with_config, which built no menu, is removed.
  • Offscreen: a list drawn with no visible area lays out every row.
  • Wider rows: a row reached while drawing that's wider than the list widens it, and every row is re-centered in the same frame.
  • Scroll position: the estimated height doesn't shrink mid-list, so the scroll view doesn't clamp its position. It becomes exact once every row is laid out.
  • Drag: a touch holder's slot comes from its node, so a drag no longer scans every child on each move.
  • Debug check: debug builds sample up to 64 keys when the compare is skipped, and panic if they changed without a write outside the items.
  • Key hash: it now xor-shifts after each multiply, so keys of several words don't combine linearly. Integer keys still can't collide.

The snapshots change only in the hashes of list keys, and so in the order of their lines. I checked that the set of layouts in each one is unchanged.

Results (median ms per frame, after all the commits)

case event update render
overview scroll, 10,000 rows 0.094 0.010 1.34
overview scroll, 1,000,000 rows 0.089 0.009 1.18
messages scroll, 1,000,000 rows 0.035 0.006 1.09
overview row hover, 10,000 rows 0.103 0.106 1.21
overview row hover, 1,000,000 rows 0.083 0.094 1.24

Before:

  • messages scroll, 10,000 rows: update averaged 19.6 ms.
  • overview scroll, 1,000,000 rows: the scroll event took 19 ms and render 21 ms.
  • overview hover, 1,000,000 rows: update took 115 ms.

Startup at 1,000,000 rows: the first render now takes 6 ms (it was 7.2 s), and the app responds 1.4 s after launch. At the start of this PR it responded after 22.6 s. The remaining 1.4 s is gui_bench building its data.

Changing the filter, search or sort still visits every row, because what's shown depends on all of them.

Tests

  • test_scrolling_moves_the_content_without_layout
  • test_anim_pass_skips_lists_without_anim
  • test_anim_pass_reaches_anim_in_plain_list_items
  • test_items_near_an_area_of_a_vertical_list, …horizontal_list, …z_list_are_all
  • test_key_hashes_differ
  • test_list_keys_are_matched_again_after_a_write
  • test_list_lays_out_only_written_items, test_list_item_that_changes_size_moves_the_others, test_list_lays_out_every_item_after_a_write_around_it
  • test_list_dirty_walk_visits_only_written_items, test_anim_pass_visits_only_written_items
  • test_long_list_lays_out_only_what_is_shown (renders with a real Vger), test_long_list_estimates_the_rows_not_laid_out
  • test_long_list_drawn_offscreen_lays_out_every_row, test_long_list_widens_for_a_wider_row_reached, test_long_list_estimate_does_not_shrink_while_rows_remain
  • test_commands_reach_list_items_before_any_layout, test_list_keys_changed_unseen_are_reported_in_debug_builds

cargo test --workspace passes.

🤖 Generated with Claude Code

wtholliday and others added 2 commits November 27, 2026 11:37
…nim pass

Scrolling wrote the scroll position as state, which invalidated the cached layout of every
enclosing state view, so each wheel step rebuilt and measured everything in the scroll view.
Scrolling now stores the position without a write, moves the content's offset itself and asks
for a redraw.

The Event::Anim pass of every update still visited every item of a list whose items aren't
state views. A list, or an item, with no anim view seen in it is now skipped while nothing was
written since the last update's layout, when the has_anim marks are complete.

In gui_bench's messages page at 10,000 rows, update per scroll frame goes from 19.6 ms to
0.13 ms.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
- List keys are hashed with a fast multiply-rotate hasher instead of SipHash. A list hashes
  all its keys on every walk.
- The keys of a list's children are kept in one Vec<u64> on its node, so checking that they
  are unchanged no longer visits every child node.
- A vertical or horizontal list finds the items to draw, and the items near the pointer for
  motion and wheel events, by binary search over their laid-out positions.
- List::dirty returns at once when nothing under the list was written.

The snapshots change only in the hashes of list keys, and so in the order of their lines.

In gui_bench's messages page, the median render per scroll frame is 0.47 ms at 3,000 rows and
0.50 ms at 100,000; the scroll event is 0.1 ms at 100,000 rows, down from 1.7 ms.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@wtholliday wtholliday changed the title Scroll without laying out, and skip lists without anim views in the anim pass Make scrolling a long list fast: no relayout, no full anim pass, binary-searched items Nov 27, 2026
- A list only compares its keys after a write (or invalidate_all), the rule cached layouts
  already follow: views are built from the states, so their keys are too.
- list_shared, hlist_shared and zlist_shared take ids in an Arc, so an app can hand a long
  list the same ids on every build instead of a new Vec.
- The items that were under the pointer keep the slot they were in, so finding them again
  doesn't look through every child.
- gui_bench: the orders table finds the shown orders again only when the filter, the search
  or the sort changes, and both long lists use list_shared.

In gui_bench at 1,000,000 rows, a scroll frame on the overview costs 0.05 ms for the event
and 0.9 ms to render, the same as at 10,000 rows; before, 19 ms and 21 ms.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@wtholliday wtholliday changed the title Make scrolling a long list fast: no relayout, no full anim pass, binary-searched items Make scrolling long lists fast, up to a million rows Nov 27, 2026
A hover in the orders table at 1,000,000 rows cost 115 ms per update: the write invalidated
the views around the list, which then visited every item again.

- Each node counts the writes under it. When every write since a list's last sync was in its
  items, its keys can't have changed and aren't compared.
- A list records which items had writes under them. When its proposed size, the enclosing
  states and the states its items read outside themselves are unchanged, its layout lays out
  only those items, and falls back to every item when one changes size. Each item's reads
  are collected on their own, so an item's own state doesn't count as read from outside.
- The dirty walk visits only the written items.
- The Event::Anim pass visits only the written items of a list with no anim view in it.

Hovering rows at 1,000,000 rows now costs 0.09 ms per update, as at 10,000.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@wtholliday wtholliday changed the title Make scrolling long lists fast, up to a million rows Make scrolling and hovering long lists fast, up to a million rows Nov 28, 2026
wtholliday and others added 2 commits November 27, 2026 16:31
The first redraw comes before the first update, and render didn't record the window size it
laid out at, so the first update saw a new size and called invalidate_all, laying out the whole
UI again and visiting every item in the animation pass. At 1,000,000 rows in gui_bench that
update took 10 s after the UI was already on screen; now 0.08 ms.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
A list in a scroll view starts at the top and is scrolled continuously, so a row's place
depends only on the rows above it, which have all been reached by the time it shows. A
vertical list now builds and lays out its items from the top down to as far as drawing has
reached plus a window; the others count at the average height of those laid out, which only
sets how far the scroll view can scroll, and get no node. When drawing reaches past the rows
laid out, the list lays out more right there, so they show in that frame, and asks for a
layout to learn its new size.

Only the items laid out take events, hit tests, accessibility nodes and menu commands. The
menu is gathered once before any layout, so it no longer walks every item of a long list.

In gui_bench at 1,000,000 rows, the first render takes 6 ms instead of 7.2 s, and the app
responds 1.4 s after launch instead of 11.3 s; scrolling and hovering stay as fast.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@wtholliday wtholliday changed the title Make scrolling and hovering long lists fast, up to a million rows Make long lists fast: virtual layout, scrolling and hovering up to a million rows Nov 28, 2026
wtholliday and others added 2 commits November 27, 2026 18:25
- Rows laid out while drawing are proposed what the list's layout proposes, kept from its last
  layout, instead of the list's own estimated size. They were getting a height of
  total / n, missing their cached layouts at the next layout and able to change size, and
  with no window size the reach was the list's whole height.
- Before a list's first layout every item takes part in walks again, so menu commands in list
  items are gathered. The unused commands walk at startup in rui_with_config, which built no
  menu and walked every row of a long list, is removed.
- Drawn with no visible area, as offscreen, a vertical list lays out every row, so a tall list
  centered on the target isn't drawn blank.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
- A row reached while drawing that is wider than the list widens it, and every row laid out is
  centered across the new width at once, so the rows line up in that frame as at the next
  layout.
- While rows remain to be laid out, a vertical list's estimated height doesn't shrink for the
  same proposal, so a scroll view doesn't clamp its position mid-list and move what shows. It
  becomes exact once every row is laid out.
- A touch holder's slot comes from its node, so a drag in a long list doesn't look through
  every child on each move.
- Debug builds check up to 64 keys, and the last, when a list skips comparing them, and panic
  with a message pointing at invalidate_all if they changed without a write outside the items.
  Two tests that reordered keys from outside rui now call invalidate_all, as that requires.
- The key hash xor-shifts after each multiply. Each step is still a bijection, so integer keys
  never collide, but keys of several words no longer combine linearly. The snapshots change
  only in the key hashes and the order of their lines.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@wtholliday
wtholliday merged commit b7bed1b 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