Skip to content

core: Look at what is near a scroll's viewport alone in each frame - #4273

Open
huacnlee wants to merge 1 commit into
core-text-made-as-estimatedfrom
core-overlays-local
Open

huacnlee wants to merge 1 commit into
core-text-made-as-estimatedfrom
core-overlays-local

Conversation

@huacnlee

@huacnlee huacnlee commented Dec 3, 2026

Copy link
Copy Markdown
Member

Stacked on #4248: the frames measured here are the ones that PR stopped laying out again. Retarget to main once it
merges.

Summary

After #4248, the cost of a scrolled frame still grew with the number of rows in the document. There were three causes.

Culling walked the whole column (core/cull.rs). sync_culled_in looked at every child of a long column to find
the few near the viewport.

A column of at least 64 children placed one below the other is now noted as a Column. Its children must be laid out,
sized and in flow, with no transform, sticky, overlay or follower. The note holds the children's tops and bottoms and
the range that holds every made one. Culling then binary-searches the rows within reach of the viewport and also looks
at the made ones.

The note is kept while cull_epoch stays the same. cull_epoch is raised by:

  • an op;
  • a layout pass;
  • a frame set;
  • a view made anywhere except by the walk itself.

Anything else falls back to walking every child.

Every view made dirtied every overlay (core/overlay.rs). ensure_created called dirty_overlays(), so with a hint
in each row every scrolled frame sorted all overlays by depth and worked out every host again. Now:

  • A made view queues the overlays it hosts: in it, or in its spans (queued).
  • A view given up queues the overlays in its subtree.
  • sync_overlays works out those hosts alone, outermost first. An overlay whose host changed queues the overlays in it.

Every other change still dirties all of them, as before.

The listener lists, overlay_scrolled, view_holders, sync_kept and order_overlays_for_tab only need shown or
in-view overlays. They now walk near, the overlays in the view, kept as in_view changes. They no longer walk every
overlay.

Rows with a hidden hint were never given up. reclaimable counted any overlay in a subtree as a view to keep, shown
or not. So rows with a hint stayed made, and the made rows grew as the scroll went on. An overlay with no view (a hint
not shown) no longer holds its row. When the row is made again, its hint's host is worked out again.

Public API

None.

Performance

cargo test --release -p nsh-core --lib bench_each_mount -- --ignored --nocapture (FakeEngine, 320 × 480, macOS
arm64). Three alternating runs of this branch's test binary and #4248's (9d574fccd). Values are µs per scrolled frame
(20 points per frame, 200 frames):

Rows of an each 1,000 10,000 50,000
In a scroll view 19.9–20.7 → 8.8–9.1 174.7–182.5 → 22.1–22.5 1,176.3–1,260.4 → 81.7–83.0
With a hint each 58.1–61.4 → 10.9–11.0 1,056.6–1,366.0 → 25.3–26.0 20,896.4–21,225.5 → 125.8–177.8

Against main before #4248, the 50,000-row frame goes from 37–41 ms to 82 µs, and with a hint each from 73–75 ms to
126–178 µs. The first mount and its 427 mount items are unchanged.

  • nsh test --trace: examples/counter/App.tsx (56 ops, 50 mount items) and examples/bench/RowsTs.tsx scrolled
    3,000 pt and back (1,040 ops, 915 mount items) keep their counts.

  • examples/bench/jank/jank.py --runs 3 on macOS, core: Lay nothing out again for text a scroll makes as it was estimated #4248 → this branch:

    • startup 701 → 698 ms;
    • scroll frame p95 1.25 → 1.38 ms;
    • gap p95 8.33 ms on both;
    • no frame over budget on either.

    The jank scene is short, so these differences are within run-to-run spread.

Content culling is on for iOS and Linux (GTK); the headless and native test drivers turn it off. This change is in the
platform-independent core, so the numbers above come from the core bench.

The record is in docs/dev/performance.md.

Tests

  • a_scroll_looks_at_the_rows_near_its_viewport_only: with and without a hint per row, 20 scrolled frames of 1,000 and
    4,000 rows. The children culling looks at plus the overlay hosts worked out must stay about the same. Without the
    change they grow four times: 44,170 → 164,170 without hints, 68,220 → 248,220 with them.
  • rows_with_a_hint_not_shown_are_given_up_far_away: 2,000 rows with a hint each, scrolled 8,000 pt. Fewer than 400 rows
    stay made, and near holds exactly their hints. Without the change, 563 stay made.

Other checks:

  • cargo test -p nsh-core: all 2,162 tests pass.
  • cargo test --workspace --no-fail-fast passes except nsh-cli a_built_record_survives_a_moved_home_and_a_new_time
    and nsh-platform-macos draw_on_show, which fail on main too.
  • cargo clippy --workspace --all-targets: no new warnings.

🤖 Generated with Claude Code

What a scrolled frame cost grew with the rows of the document, in three
ways:

- Culling looked at every row of a long column for the few near the
  viewport. A column whose children are one below the other is now
  noted with their tops, bottoms and which are made, kept while no op,
  layout or frame changes them, and only the rows within reach of the
  viewport and the made ones are looked at, found by binary search.
- Each view made marked every overlay dirty, so with a hint in each row
  every frame worked out every hint's host. A view made or given up now
  queues the overlays in it alone, and what listens, keeps rows or
  orders Tab looks at the overlays in the view.
- A row holding a hint not shown was never given up, so the made rows
  grew as the scroll went on. A hint not shown has no view to keep.

A scrolled frame of 50,000 rows takes about 82 µs where it took about
1,200, and with a hint in each row about 150 µs where it took 21 ms.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@huacnlee

huacnlee commented Dec 3, 2026

Copy link
Copy Markdown
Member Author

💡 Strong perf change (50k rows: 1.2 ms → 82 µs per scrolled frame), but it is stacked on #4248 (base core-text-made-as-estimated); fold it into #4248 or retarget to main after #4248 lands, since stacked PRs are not merged.

Reviewed head 6822c07 (diff read, partly).

  • Plausible risk, please confirm: cull.rs find_column treats a child as stable when view_style.transform is empty and caches the column until cull_epoch changes. If an animated transform / list motion moves rows without an op, layout or frame change, cached tops/bottoms go stale and a row animated into view stays culled until the next op. Worth checking the animation graph either counts as a transform here or bumps the epoch.
  • Pre-existing: a_built_record_survives_a_moved_home_and_a_new_time and draw_on_show also fail on main.

🤖 Noop (reviewer) · AI agent acting for @huacnlee

This branch has not been deployed

No deployments
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