Skip to content

each: Mount an each's rows in time linear in their number - #4261

Open
huacnlee wants to merge 1 commit into
mainfrom
core-each-mount-in-order
Open

huacnlee wants to merge 1 commit into
mainfrom
core-each-mount-in-order

Conversation

@huacnlee

@huacnlee huacnlee commented Dec 3, 2026

Copy link
Copy Markdown
Member

Summary

Opening a long list or document built with each / <Each> took time in the square of its rows. Two places did it,
one for each kind of each.

Rust each (core/views.rs). reconcile_region placed a region's nodes from the last, each before the one placed
last ("from the last, each before the next, from the anchor"). On a first mount every node is new, so every insert went
in front of all the rows placed so far and moved them along the parent's children (and the layout tree's): n rows moved
n²/2 children.

When every node of the region is new (no parent yet), they now go in order, each right before the anchor. The
reconciliation of kept and moved nodes is unchanged.

TSX <Each> (nsh-js dom.rs). insert_before looked for the node it inserts before among the parent's children
from the front. Rows go before what follows them, near the end, so n rows looked at n²/2 children.

child_position now looks from both ends, as the core's position_from_ends does. It is used by insert_before,
next_sibling and the removal from a parent.

Public API

None.

Performance

cargo test --release -p nsh-core --lib bench_each_mount -- --ignored --nocapture. Setup: FakeEngine, macOS arm64,
three alternating runs of the two test binaries against 7d33c400b. Values are ms for the first mount:

Rows of an each 1,000 10,000 50,000
In a view 4.1–13.8 → 4.7–14.5 52.5–55.0 → 47.1–58.6 506.9–511.4 → 249.2–306.5
In a scroll view 4.3–5.0 → 4.1–4.6 56.5–58.7 → 47.9–51.8 506.4–524.4 → 259.7–270.4

A TSX <Each> of texts between two texts in a scroll view, nsh test --trace (release, headless, three alternating
runs). Values are ms for the first frame, and for the JS evaluation in it:

Rows 20,000 80,000
First frame 131.8–135.3 → 69.2–74.9 1,340.5–1,347.9 → 334.1–341.5
JS evaluation 91.3–93.9 → 28.9–33.9 1,154.7–1,165.3 → 150.2–160.0

nsh test --trace of examples/counter/App.tsx and of examples/bench/RowsTs.tsx scrolled 3,000 pt down and back
keeps their counts (56 ops, 50 mount items; 1,040 ops, 915 mount items). Their times stay within run-to-run spread:
counter 90–197 → 91–143 µs, rows 1,183–1,538 → 1,099–1,428 µs.

The record is in docs/dev/performance.md. Nothing here touches window showing, frame scheduling or scrolling.

Tests

Both new tests fail without the change: 400 rows → 80,200 and 1,600 rows → 1,280,800 children moved; 80,600 and
1,282,400 children looked at.

  • an_each_mounts_its_rows_in_order_before_what_follows (nsh-core): 400 and 1,600 rows of a Rust each before a
    last child mount in order. The children moved along grow about four times, not sixteen (shift_work, test only).
  • rows_inserted_before_a_last_child_look_at_few_children (nsh-js): the same for nodes inserted before a last child
    through nsh:rt's insert (CHILDREN_LOOKED, test only).

Other checks:

  • 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 and cargo clippy -p nsh-platform-web --target wasm32-unknown-unknown --features compiler: no new warnings.

🤖 Generated with Claude Code

Mounting n rows of an each cost n² in two places. A Rust `each`'s region
placed its new nodes from the last, each before the one placed last, so
every insert moved every row placed so far; new nodes now go in order
before the anchor. The renderer looked for the node it inserts before
among the parent's children from the front, though rows go before what
follows them, near the end; it now looks from both ends.

50,000 rows of a Rust `each` mount in about 260 ms where they took about
510 ms, and 80,000 rows of a TSX `<Each>` show their first frame in
about 340 ms where it took 1,345 ms.

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

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