Skip to content

Lay the margin out once per resize, with one observer owning what it watches - #5544

Merged
max-sixty merged 2 commits into
mainfrom
margin-one-observer
Nov 5, 2026
Merged

max-sixty merged 2 commits into
mainfrom
margin-one-observer

Conversation

@max-sixty

Copy link
Copy Markdown
Owner

The margin watched resizes with two ResizeObservers, one for row targets and one for the body and the column, so a resize both saw ran two full margin passes over the same layout, and the load and each revision that adds rows paid an extra pass. This merges them into one observer and gives one function, subscribe in margin-layout.js, sole ownership of what it watches: each pass states the set (row targets, the body, the column, every live lane's scroller), and subscribe observes or unobserves the difference.

Measured on the generated asks page (least of interleaved rounds, a loaded host): the load and each one-paragraph revision run one pass fewer (2 passes with rows per revision instead of 3), with every row's rectangle identical after the load, the addition and the removal. A revision's main-thread CPU in Chromium went 175→154 ms (add) and 173→145 (remove) at 64 sections, and 1,915→1,205 and 1,427→916 at 512. Load went 821→508 ms at 64 sections in Chromium and 817→492 in Firefox.

Checks: pnpm run test:runtime (820), pnpm run check, 35+ lane, pane-switching, rail, pin, scroller and resize browser tests by node id, the three render-gate pages #5500 names, and leaf-dev stills over 30 margin and thread states in Chromium and Firefox with byte-identical screenshots. An independent review found the lane-retirement loss in the first version; its review of the fix found nothing.

This was written by Claude Code on behalf of max-sixty

The margin watched its boxes with two ResizeObservers: one for each target's
ancestors and their siblings, and one for the body and the column. The body and
the column are almost always in the first set too, so a resize they saw reached
both observers in one delivery round, and each ran the whole layout pass. That
round comes with the page's first paint and with every revision that adds or
removes content, and the second pass read the layout the first had just seated
every row by.

The body and the column now join the targets' boxes on the layer's one observer
(`layer.sizes`), so such a round runs one pass. The first row still starts their
watch as it registers, which #5500 found keeps their first deliveries out of a
settling page's resize loop; each pass then keeps the column in the watched set
with the rest, and a pass with no rows left stops watching everything. Every box
is watched as before and every delivery still runs a pass; only the second
callback for the same round is gone.

Not changed: the pass that a newly watched box's first delivery runs. It usually
reports the layout the pass that began the watch had read, but #5493 found no
measure Leaf can take that matches what the observer reports in every case (a
content box narrowing under a scrollbar, an SVG under a transform, a writing-mode
switch), so that pass stays.

On `leaf-dev scaling`'s asks page, in Chromium and Firefox at 64 and 512
sections, the load and an agent's revision adding or removing one paragraph each
run one pass fewer: two passes with rows rather than three for each revision.
Every row's seat reads the same on both arms after the load, the addition and
the removal (64 sections, Chromium, exact rectangles).

Checked: `pnpm run check`; 55 margin, scroller, pin, rail, card and revision
browser test results by node id; and
`tests/journeys/test_render_gate.py::test_page_fixture_renders` for
pr-walkthrough, notification-playground and thread-panel-gallery, the pages
#5500 named, which pass here and raise the ResizeObserver loop error once the
registration's early start is removed.
The margin's resize subscriptions had two owners. The pass's geometry set, which
since the previous commit holds the body and the column, was subscribed by its
install step, and each lane subscribed its scroller itself and unsubscribed it on
retiring. A reading region whose scrolling body is `main` and that switches to
flowing layout while rows remain retires its lane, and the lane's unsubscribe
dropped `main` while the geometry set still held it, so no later install put it
back and a resize only the column saw moved no row. The separate column observer
had covered that before the previous commit; the same loss already applied to any
lane scroller that is also an ancestor of a row's target.

`subscribe` is now the only code that observes or unobserves, diffing the set
each pass states against the set it last subscribed. The pass adds every live
lane's scroller to that set after it creates and retires lanes, and the first
row's early start of the body's and column's watch goes through `subscribe` too.
A lane scroller is now subscribed by the pass's install step, in the same frame
for a pass that runs in the rendering pass.

No test drives this directly: margin-layout.js needs a laid-out document, so
tests/runtime/ can't load it, and the user-visible case needs a region whose body
is the column to switch to flow and then a column resize the page's mutation
watch doesn't hear. Checked: `pnpm run test:runtime`, `pnpm run check:browser`,
35 lane, pane scroll-behavior, rail, pin and resize browser test results by node
id, and the three render-gate pages #5500 named.
@max-sixty
max-sixty merged commit 7a1846d into main Nov 5, 2026
5 checks passed
@max-sixty
max-sixty deleted the margin-one-observer branch November 5, 2026 20:51
max-sixty pushed a commit that referenced this pull request Nov 5, 2026
…own controls wear their keys (#5551)

Three of the seven tests #5486 tracks fail because #5361's single cover
box for the floating panel is read wrongly in two places, and one of
them also hides a test step #5392 made stale. This fixes those three;
the other four have separate mechanisms that are still being diagnosed
under #5486.

-
**`test_a_seat_send_at_the_window_foot_shows_the_thread_it_started[verdict]`.**
`seenRect` kept the larger part of a box above or below the panel's
cover, so the 14px strip between the panel card (842–886) and the
window's foot (900) counted as seen. After a verdict seat's send, its
reply box showed only that strip, so `returnToThread` skipped the
landing and left the sent message under the panel. A cover now takes
everything from itself to the window edge it stands nearer (`coverCut`).
`shownWindow` already used that rule, and now `uncovered` (behind
`seenRect`, `inFront` and paint cuts) uses it too.
-
**`test_required_page_failure_retains_composer_seat_focus_and_caret`.**
`covered` read an empty box as covered, because nothing is left of a box
with no height. The widget's empty seat `b` (720×0) was refused the
comment editor, although nothing stood over it. `covered` now asks
whether a cover cuts the box. The test's later step expected the thread
that loses its seat to wait behind "1 new" in the rail. #5392
deliberately replaced that hold with drawing the thread at once, so the
step now expects both threads drawn.
-
**`test_a_thread_obligations_done_press_keeps_its_keys_and_one_workflow`.**
Key badge placement kept every chip off every cover. Since #5361 the
panel's cover spans its open view, so the Done button inside the panel's
own thread view lost its key face. A chip now keeps off only the covers
over its own control (`coverBoxes(owner)`, which already leaves out a
surface for what it holds). Chips for page controls still keep off the
whole panel, as #5361 intended.

What the user sees: a message sent from a seat at the window's foot
lands clear of the panel, the comment editor takes an empty widget seat,
and controls in the panel's thread view show their digits again. #5361's
rule that what a panel view covers counts as unseen is unchanged.

**Verification.** Each of the three fails on `main` and passes on this
branch's head, rebased on `8bece180c`. `pnpm run test:runtime`,
`check:browser` and `test:browser` pass. `test_go_to.py`,
`test_key_surfaces.py`, `test_composing.py`, `test_reading_place.py` and
`test_render_floating_panel.py` show no new failures. The four failures
there also fail on unchanged `main`: three are in main's run
42074354782, and `test_sending_flashes_only_the_new_message[card]` fails
the same way locally on `main`.

<details><summary>The four #5486 tests this leaves</summary>

These still fail with this change, each with its original message:

-
`test_render_aim.py::test_a_pointed_comment_keeps_the_rows_still_owed[thread]`:
`t` now gates its start landing on `alreadyShown`, a nearest-landing
question. A section taller than the window that fills it counts as
shown, so `t` no longer lands the section's top. The old
`readableDestination` asked whether the whole destination stood in the
landing band.
-
`test_current_location.py::test_the_obligation_walk_keeps_its_place_when_a_version_lands`
-
`test_render_diff_controls.py::test_file_tree_lands_at_the_file_start_after_reading_down`
-
`test_render_controls.py::test_sample_host_view_changes_leave_its_scroll_and_keyboard_position`
(ResizeObserver error at teardown, still present after #5544)

</details>

Refs #5486 — automated triage

---------

Co-authored-by: leaf-agent <[email protected]>
max-sixty added a commit that referenced this pull request Nov 11, 2026
…e rows a change names (#5817)

## Why

Sending a comment on a long page changes the margin's rows twice. The
send adds the new thread's row before the next frame, and the server's
reading about 80 ms later replaces it with the accepted thread's row
(and on the generated asks page takes the section's question row away).
Each change ran a pass that read every row again. Nearly all of that
pass was each pin reading the words and controls around its target
(`coverIn`): profiled at 1,024 sections, the two passes took 520 ms, 402
ms of it in `pinSeating`, of which 149 ms was rebuilding the child
index. Root `AGENTS.md` ("Responsiveness") asks that an input's work
depend on what it changes, not on how much the page holds.

## Design

Every pass seats every row in the one shared held-and-arrival order.
What changes is how much it reads.

- **A record per row** (`seatings` in `margin-layout.js`) holds what the
last pass read to seat it: its read, its home before any push, and a
pin's prepared seats and cover, at that pass's scroll. A row with a
record is seated from it, carried by the document's scroll since
(`shiftedPin`). Any other row is read afresh, as before.
- **Each change's owner says what it moves.** A change that may move the
page clears every record, so the pass after it reads every row as
before. Every page observer the margin already has does this: a resize
the layer watches, a page mutation, `lf-layout`, a slot change, a window
resize, a scroll whose stir reading flips a row, a scaled scroll ending.
A scroll in any box inside the page other than the document, and a
transition or animation ending outside Leaf's chrome, clear every record
too. A change to one row (registered with another target, let go, or
drawn at another size) drops only that row's record.
- **Only a row the document alone scrolls keeps a record.** A row the
user holds, a withheld row, and one in a pane, carrier or header frame
are read every pass, as before.

So a pass for a change to the rows alone reads only the rows that change
names and seats everything, and ends with every row where a pass that
read them all would stand it. Before a pass, the margin first hears the
face changes still waiting to be delivered, so a face drawn and laid out
in one task is read at its new size.

Two smaller changes ride along:

- Held rows are found with one query over the layer that names the held
row or the part holding it, instead of asking `:has()` of every row:
1.4–2.1 ms → 0.4 ms at 1,024 sections.
- `leaf-dev bench` gains a `resolve` input, which measures a row leaving
beside `comment`'s row arriving.

## What the review found

An independent review confirmed the saving in Chromium and Firefox, and
found two cases where the first version left rows where a full pass
would not. Both now pass a test that fails on that version.

- **A hold ending.** The first version kept a row's seat while no
changed footprint reached the room its seating had read (`seatReach`,
`packReach`). After a hold ended, the released row moved later in the
seating order, and the kept rows before it were never asked again, so a
row stayed 30 px above its line. That reach was also a second statement
of which rectangles `seatRow` and `packRow` test, which every change to
them would have had to keep in step. **Fix:** the reach is gone (−150
lines with its tests). Every pass seats every row, so the order and the
occupied footprints are a full pass's by construction.
- **A face grown at once.** An immediate contribution update draws a
face and flushes the layout in one task (`application.js`), before the
face observer delivers. The pass's own `faces.takeRecords()` then
discarded those records, so a pin grown from 31 to 62 px stayed past the
window's right edge. **Fix:** a pass first runs the waiting records
through the observer's handler and checks the restyled rows' sizes,
dropping the record of each that changed. The review also tried
re-reading every restyled row instead: that read 595 rows afresh rather
than 28 and lost the saving.

The two pieces of state the first version added fold into the one map.
Clearing it replaces the "page moved" flag, and dropping a row's record
replaces the set of unsettled rows.

The re-review confirmed both fixes in Chromium and Firefox. Its
full-pass comparison showed no differences on the asks journey, a
hide-and-show journey or 8 shipped examples, and the saving held. It
found one new case, which main does not have:

- **A pane scrolled beside a pin.** A page pin's cover counts the words
a neighbouring scrolling pane shows, and a pane's scroll dropped no
record. On the review's page, the next pass for the rows alone kept a
pin at (1367, 153) over words the pane had scrolled under it, where main
and a full pass stand it at (225, 183). **Fix:** a scroll in any box
inside the page other than the document, outside Leaf's chrome, clears
every record. The document's own scroll still carries them.
- **The same gap at a transition or animation ending**, which feeds the
same handler. A probe confirmed it: a transform transition and a
keyframe animation each slid words under a kept pin, and the next pass
left it there (1367 px, against 303 and 247 px for a full pass).
**Fix:** that handler, which already reads the rows under the moving box
again, now clears every record too. The cost was measured first:
- The asks journey ends no transition or animation outside the chrome,
so it keeps the same 2,553 rows from their records.
- Sweeping the pointer and the wheel down five pages ended 0 to 4 each,
all on Leaf's own controls in the page.

## Measurements

`uptime` first (load 2.6–3.0), then `uv run leaf-dev bench asks --sizes
64,256,1024 --input comment --input resolve --rounds 5 --base
origin/main`: Chromium, both arms interleaved in one run, head 611fec5
against main at 7d3a418. Each cell is the median of 5 rounds, with the
range in brackets. Paint is from the input to the first frame showing
its own result; shown is to the frame presenting the server's reading of
it; CPU is the input's main-thread time.

| Input | Sections | Paint, main → branch (ms) | Shown, main → branch
(ms) | CPU, main → branch (ms) |
| --- | ---: | --- | --- | --- |
| Send | 64 | 89 [88–105] → 72 [70–74] | 122 [121–139] → 93 [90–95] |
115 [113–117] → 89 [87–92] |
| Send | 256 | 172 [170–173] → 119 [116–123] | 261 [260–263] → 157
[154–161] | 253 [252–257] → 154 [152–155] |
| Send | 1,024 | 553 [538–931] → 354 [351–355] | 930 [912–933] → 497
[493–501] | 987 [982–992] → 481 [479–486] |
| Resolve | 64 | 105 [105–109] → 120 [114–122] | 160 [154–164] → 120
[114–122] | 171 [169–171] → 127 [123–131] |
| Resolve | 256 | 301 [298–301] → 244 [242–248] | 496 [495–498] → 309
[304–311] | 522 [517–527] → 322 [321–332] |
| Resolve | 1,024 | 1,114 [1,103–1,121] → 896 [884–901] | 1,877
[1,860–1,893] → 1,130 [1,116–1,140] | 1,625 [1,622–1,630] → 1,166
[1,157–1,176] |

The pane and motion clears (9a65849) came after this run. They change no
pass on these inputs: the bench scrolls no pane, and the asks journey
ends no transition or animation outside the chrome, so it kept the same
rows from their records with them. The re-review measured send paint at
172 → 122 ms at 256 sections and 555 → 353 ms at 1,024.

Against the first version, measured the same way, the send's paint
saving is unchanged at 256 sections (51 → 53 ms, within noise) and goes
from 231 to 199 ms at 1,024. Its CPU saving at 1,024 goes from 554 to
506 ms.

The resolve's paint at 64 sections is the one cell that reads slower, as
it did for the first version. When the server's answer arrives before
the resolve's first frame, the browser presents the reading in that same
frame, so its paint equals its shown. A 7-round recheck of the first
version found both arms doing this in 4 to 5 of 7 rounds at 8 and 32
sections. At 64 sections the branch did it in 6 of 7 and main in 3,
because the reading's pass is now cheap enough to fit. At 128 sections
neither did, and paint read 159 → 139 ms.

**Cost of seating every row.** Timed directly on the asks page through
four sends and a resolve, a pass for the rows alone takes a median of
3.3 ms at 256 sections and 22 ms at 1,024. With the reach it took 1.8
and 5 ms in the review's timing. A full pass takes 52 and 223 ms.

**Follow-up: index the occupied footprints.** Seating every row grows
with the square of the page, as the full pass always has. `seatRow`
copies and scans every footprint already seated for each pin, which is
also most of the 22 ms. Indexing the occupied footprints by height on
the page, so each seating reads only those near its target, would take
that out of both passes.

## Checked

-
`test_a_pass_for_a_change_to_the_rows_seats_them_as_a_whole_pass_would`
(new, `tests/browser/margin/test_pins.py`). It compares every row with
where a forced full pass stands it, after each of:
  - a hold ending;
  - a face growing at once;
  - a row leaving;
  - a pane beside a pin scrolling;
  - a transition beside a pin ending;
  - a scroll and a sent comment;
  - a resolve.

Each fix has a step that fails without it. On the first version the test
fails at "a hold ends" (line-1's row) and "a face grows at once" (the
question's pin, 1397 → 1367 px). With the face records left unheard it
fails at the face step alone. Without the pane-scroll clear it fails at
the pane step, and without the transition-end clear at the transition
step. It replaces the earlier pushed-row test, whose case is its "a row
leaves" step.
- The `shiftedPin` test names every positional field of a pin, and fails
for each of the seven fields left unshifted. The earlier test caught 3
of 7.
- `pnpm run test:runtime` (913 pass), `pnpm run check:browser`,
pre-commit on every changed file.
- 58 focused browser tests by name: rail, pin, cluster, entry, scroller
and card-placement tests in `tests/browser/margin/`, and the
floating-panel, outbox, margin and send-placement tests that add,
resolve or undo rows. After the pane and motion clears, 37 pane, scroll,
transition and placement tests among them were run again.
- The same before-and-after comparison on four shipped examples
(`release-notes`, `review-a-fix`, `rust-sort`, `pr-walkthrough`). Each
added rows by contribution, scrolled, added more, then took two batches
away. No row moved under the forced full pass after any step.

Not checked:

- Firefox and WebKit on this version. The review measured Firefox on the
first one: send CPU 321 → 194 ms at 256 sections and 1,183 → 673 at
1,024. The focused tests ran in Chromium.
- The broad suite and the main-push browser tier.
- A row added where the margin watches no box yet still brings one full
pass in the next frame, from the observer's first delivery for the boxes
it starts watching. On the asks page the bench's passage shares its
section with a watched question, so the send avoids it. #5544 records
why that first delivery is not suppressed.

What remains of the send and resolve at 1,024 sections, from a profile
of the first version, is outside this pass:

- The presentation and repaint passes.
- The projection's `holdSelection` restyling every margin row once (46
ms).
- On a resolve, one forced restyle of all 185,554 elements, read by
`landingBand` (744 ms when profiled), which main has too.

> _This was written by Claude Code on behalf of max-sixty_
https://claude.ai/code/session_01GQAnFBpRfi8Gh5cgPofNzs
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.

2 participants