Row recycling (spike S2)
Use the hybrid renderer.
Context
- Spike: S2 in
docs/plan.md, section 11 - Spike code:
test-app/app/spikes/s2/grid.gts - Tests:
test-app/tests/spikes/s2-row-recycling-test.gts
The plan (section 4.6) renders a fixed pool of row slots, keyed by slot. On scroll, a slot
gets a new row. This is cheap, but a slot keeps its component instances. A cell component
with its own @tracked state can then show that state on a different row (decision 26).
The spike compares three renderers. Each renderer shows a grid of 100,000 rows and 20
columns in a viewport of 400 px. The row height is 32 px. The spike has its own small
fixed-height virtualizer, because @ember-lattice/virtual is not ported yet.
- A,
slots: a pool of 16 row slots,{{#each slots key="@index"}}. Slotsshows the rowrin range wherer % 16 === s. - B,
keyed: rows keyed by row ID,{{#each rows key="id"}}, with an overscan of 10 rows on each side. - Hybrid,
hybrid: the fix from the plan, section 11, row S2:- Rows recycle in slots, as in A. "Cheap" cells recycle with the row.
- "Full" cells (components with
@trackedstate) are keyed by row ID inside the slot:{{#each (array row) key="id"}}. A new row gives a new component instance. cell.state: a store keyed by row ID and column ID. Cheap cells keep their state there.- The range extractor holds back the focused row and the edited row. These rows render in a separate layer keyed by row ID. Their slot is empty while they are held.
- The editor renders in an overlay outside
role="grid", at the position of the cell. The Formwright draft lives in an edit store (the stand-in for the edit machine), not in the cell. The form has@resetOnDestroy={{false}}, because the draft is not the form's.
Decision drivers
Not recorded in the original ADR.
Options considered
Not recorded in the original ADR.
Decision
- Use the hybrid renderer. Slots recycle rows and cheap cells. Full cells are keyed by row
ID. Cell state goes in
cell.state. The focused row and the edited row are held in a layer keyed by row ID. Editors render in an overlay. - Do not use pure slot recycling (A). It leaks component state to other rows. It also moves DOM focus and the editor to a different row.
- (This needs the maintainer's agreement. It changes the S2 fallback in
docs/plan.md.) Do not use the fallback from the plan (B, keyed rows with a larger overscan) alone. It is correct for cell state, but it is 4 to 8 times slower on scroll. It also loses the focused cell and the editor when their row leaves the range. B needs the same held layer and overlay as the hybrid, so B gives no simplification. - The Grid does not move the active cell to a new element (see Amendment 1). The active row keeps its slot and its element, so DOM focus stays on the cell. The Grid has no code that restores focus after a row moves. The earlier guess (give focus back after a move) was removed. The Grid focuses a cell only when a key or a click makes it the active cell. Column range changes keep the cell elements too. The Grid renders a stable keyed list of all leaf columns in each row. A change of the column range only adds or removes cells.
Amendment 1 (2026-12-01, accepted by the maintainer)
Decision 1 changes for the focused row and the edited row. They do not move into a separate held layer. The Grid uses sticky slots:
- A row keeps its slot while it is in the range.
- The active row keeps its slot, marked
data-held, when it scrolls out of the range. - The element stays the same, so DOM focus and component state stay.
Reason: in the M2 Grid, a click on a button in a full cell makes its row active. With a
separate held layer, the row moved to a new element, and the cell lost its focus and its local
state. The M2 Grid tests (test-app/tests/grid/) cover the sticky slots. The edited row in M3
uses the same sticky slot. Decision 4 (focus goes back to the cell in a new element) no longer
applies to the active row.
Amendment 2 (2026-12-02): the cost tests and the testem heartbeat
Problem
In the full pnpm test:ember run, the cost test [keyed] took 9.3 to 9.5 s. The run often
stopped at about test 275 with "Browser timeout exceeded: 10s".
Cause
rerender()isrenderSettled(). It settles in microtasks only. One cost test was thus one main-thread task. A Chrome long-task observer showed tasks of 11.3 s (keyed), 3.2 s (slots) and 2.9 s (hybrid).- Testem sets the socket.io
pingTimeoutto 5 s (socket_heartbeat_timeout) whenbrowser_disconnect_timeouthas its default of 10 s. A task longer than 5 s that overlaps a ping stops the pong, and the server drops the socket. - After a disconnect, testem waits 10 s, and it attaches the browser again only 3 times
(
browser_reconnect_limit). The error "Browser timeout exceeded" comes from that 10 s timer. The cause is the long task, not the 10 s limit.
Change
The fix is option (b): the cost tests stay in the Ember suite. No limit changed.
measure()waits for one timer task (setTimeout(0)) at the start of each run, before the clock starts. The timed loop is the same, so the numbers are the same. The longest task is now one run (1.9 s for[keyed] jumpsin the same observer).- Each cost test is now one renderer and one scenario:
[mode] idle,[mode] jumpsand[mode] rows, 9 tests. The renderers still run in the order A, B, hybrid. - The assertions are the same: A creates no elements on jumps and on rows, B and the hybrid create elements on jumps, and each median is a finite number. The idle tests also check that no element is created. Each test ends in an axe audit.
Where the evidence is, and how to reproduce it
The cost numbers are in the browser console of the Ember suite, one line for each test:
S2 cost {"mode":…,"scenario":…,"medianMs":…,"created":…,"textUpdates":…}. The testem TAP
output shows these lines. To run only the S2 tests:
cd test-app
NODE_ENV=development volta run --node 24.11.1 pnpm exec vite build --mode development
volta run --node 24.11.1 pnpm exec testem ci --port 0 \
--test_page 'tests/index.html?hidepassed&filter=S2%20row%20recycling'
The element counts and text counts are the same as in the table above. The times change with the machine and its load.
Consequences
- A full cell loses its local
@trackedstate when its row leaves the range. This is correct: state that must stay goes incell.state. The docs must tell this to authors. - While a held row is in range, its slot is empty and the held layer shows the row. The
rows do not show twice. The test for focus checks this (one row with
aria-rowindex="3"). - The plan item "dev mode warns when a cheap cell component has
@trackedfields" is not in the spike. It is still part of the plan. - The recycling contract suite (plan, section 11, S2 (e)) starts from the four contract tests in this spike. They run against each renderer.
- The overlay positions itself in the scroll content, so it moves with the cell and needs no scroll listener. The edit test checks that the overlay rect starts at the cell rect. The real overlay uses Joinery anchor positioning (plan, section 7).
Confirmation
Evidence
Correctness
Each contract test runs against A, B and the hybrid. A renderer that fails a test is the
losing alternative. Its test is test.skip, with the reason in the test name. Each skipped
test was run first and failed for the stated reason.
| Contract test | A slots |
B keyed |
Hybrid |
|---|---|---|---|
| a stateful cell does not carry its state to another row | fails (skip) | passes | passes |
| cell.state keeps state by row ID and column ID under recycling | passes | passes | passes |
| the focused cell survives a far scroll and back, with DOM focus kept | fails (skip): the element shows row r5010 |
fails (skip): the element is removed | passes |
| a cell in edit mode keeps its editor, its focus and its draft through a far scroll | fails (skip): the editor is removed | fails (skip): the editor is removed | passes |
The test "evidence: pure slot recycling leaks @tracked cell state to another row" passes. It shows the leak in A as a positive fact.
The focus test keeps a reference to the focused element. After a scroll of 5,000 rows and
back, it checks that the element is in the DOM, has DOM focus and still shows row r2. It
also checks that the held row renders once.
The edit test uses Formwright <Form> and <f.Field> on the draft. After the scroll, it
checks the input element, its focus, its value and the value in the draft object.
Cost
The test [mode] scroll through 50 viewports and 200 single rows measures the cost in
headless Chrome 158. Each step sets scrollTop, sends a scroll event, waits for
rerender() and reads document.body.offsetHeight to force layout. The time is the median
of 5 runs. A MutationObserver counts the elements that the first run creates and the text
nodes that it changes. An "idle" baseline of 200 steps with no scroll change takes under 3 ms.
The cost tests always run in the order A, B, hybrid. A runs on the coldest JIT, so the order can only make A look slower. The ranking is thus conservative.
The table shows the median time in ms for four suite runs. The machine had other loads, so the absolute times change between runs. The ratios stay stable.
| Scenario | A slots |
B keyed |
Hybrid |
|---|---|---|---|
| 50 viewport jumps (400 px) | 83 / 99 / 170 / 180 | 645 / 665 / 891 / 937 | 147 / 167 / 219 / 306 |
| 200 single-row steps (32 px) | 43 / 43 / 73 / 112 | 188 / 262 / 336 / 451 | 55 / 59 / 102 / 146 |
| Elements created, 50 jumps | 0 | 42,370 | 625 |
| Elements created, 200 rows | 0 | 9,400 | 200 |
| Text nodes changed, 50 jumps | 11,250 | 0 | 11,250 |
- B is 5 to 8 times slower than A on viewport jumps, and 4 to 6 times slower on single rows.
- The hybrid takes 1.3 to 1.8 times the time of A on viewport jumps. On single rows, the ratio is 0.8 to 2.4, which is in the noise of the machine. The extra cost comes from the one keyed full column: one new button for each new row.
- The cost of the hybrid grows with the number of full columns. This supports the dev-mode warning for full renderers in columns with many visible cells (plan, section 4.6).
Interview record
Each question of the interview with its answer and its source. A fact from the code or a document, a question that the author did not ask, and an assumption are rows too.
| Question | Answer | Source |
|---|---|---|
| Where does the text of this page come from? | From the Markdown file docs/adr/0002-row-recycling.md at commit 4c77ac4. The text of each section is the text of the Markdown file. A section that the Markdown file does not have says so. | Document: docs/adr/0002-row-recycling.md |
Changelog
| Date | Author | Change |
|---|---|---|
| 2026-12-01 | Joe Gaudet | Status in the Markdown file: Accepted |
| 2026-12-01 | Joe Gaudet | Amendment 1 (2026-12-01, accepted by the maintainer) |
| 2026-12-02 | Joe Gaudet | Amendment 2 (2026-12-02): the cost tests and the testem heartbeat |
| 2026-12-03 | Joe Gaudet | Commit 7ce4af2: Lattice 1.0 (#1) |
| 2026-12-04 | Joe Gaudet (agent) | Converted from docs/adr/0002-row-recycling.md to this page. The text is the text of the Markdown file. |
The adr skill of skillsaw wrote this page. The HTML in this file is the source of truth. The diagrams are the Mermaid sources in it.