Repository navigation
Make long lists fast: virtual layout, scrolling and hovering up to a million rows - #94
Merged
Merged
Conversation
…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>
- 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>
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>
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>
- 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>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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)
Context::update. Nearly all of it wasListlaying out every card: rebuilding each card's view and measuring its text again.Event::Animpass visiting every card.sync_keyed_childrenand the item loops inList::drawandList::process.Changes
Commit 1: no relayout, no full anim pass
ScrollViewused to write its position withcx[handle] = …, which invalidated the cached layout of every enclosingstateview. It now stores the position without marking it written, sets the content's offset itself, and callsset_dirtyto get a redraw.animview.animviews 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 newContext::marked_genrecords when that layout happened.List::processreturns at once forEvent::Animwhen its node carries no mark, and skips items without one.Commit 2: costs that no longer grow with the list
Vec<u64>, so checking that the keys are unchanged doesn't load every child node.List::dirtyreturns at once when nothing under the list was written.Commit 3: a million items, nothing per item per walk
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_sharedtakeArc<Vec<ID>>, so an app can hand a long list the same ids on every build instead of a newVec.listand friends still take aVecand wrap it.list_shared.examples/gui_benchmirrorsapps/ruiin 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.
Event::Animpass: for a list with noanimview 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
renderdidn't record the window size it laid out at. So the firstupdatesaw a new size and calledinvalidate_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.updateandrendernow share the size check, so the first update reusesrender'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.
Context::grow_list) to learn its new size.Commits 7 and 8: review fixes
rui_with_config, which built no menu, is removed.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)
updaterenderBefore:
updateaveraged 19.6 ms.render21 ms.updatetook 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_layouttest_anim_pass_skips_lists_without_animtest_anim_pass_reaches_anim_in_plain_list_itemstest_items_near_an_area_of_a_vertical_list,…horizontal_list,…z_list_are_alltest_key_hashes_differtest_list_keys_are_matched_again_after_a_writetest_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_ittest_list_dirty_walk_visits_only_written_items,test_anim_pass_visits_only_written_itemstest_long_list_lays_out_only_what_is_shown(renders with a realVger),test_long_list_estimates_the_rows_not_laid_outtest_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_remaintest_commands_reach_list_items_before_any_layout,test_list_keys_changed_unseen_are_reported_in_debug_buildscargo test --workspacepasses.🤖 Generated with Claude Code