Skip to content

Redraw only the margin entries a click changed, and stop the contents map restyling the page - #5493

Merged
max-sixty merged 6 commits into
mainfrom
margin-redraw-changed-rows
Nov 5, 2026
Merged

max-sixty merged 6 commits into
mainfrom
margin-redraw-changed-rows

Conversation

@max-sixty

Copy link
Copy Markdown
Owner

A click was the input closest to its budget on a page of real size (root AGENTS.md, "Responsiveness": the server's reading on screen within 100 ms), and most of its browser time went to the margin redrawing entries the click didn't change: every render re-presented all of the margin's clusters and renamed every marker. Now the margin redraws only the entries whose inputs changed, and removing or adding a margin row no longer restyles most of the page.

Main-thread ms per click in Chromium, b5f5c5d43 against this branch, least of three interleaved rounds:

sections ticking an option answering an ask (a marker goes, a row comes)
8 15.8 → 15.4 26.6 → 25.9
64 28.2 → 24.7 74.9 → 66.4
512 97.1 → 74.4 441.6 → 328.2

The inventory owns whether an entry changed. keptEntry (annotation-inventory.js) keeps a margin entry as the same object while everything it is built from is unchanged, compared against the previous collection with the existing share; marginInventory becomes marginEntry, which builds one entry from one group (margin-model.js). Change entries record cardOpen, since the entry's relation reads it.

The renderer compares identities only. renderNow (margin-projection.js) presents a cluster again only when its entry object or its own inputs change (the expanded owner, the forced card row, the fold, the agent's name), and a marker whose position number changed only gets its new name. Page-wide margin reads (the rail's tab stop, key reflections, label placement) run only after a row changed.

The contents map's spine. toc.css's lf-toc + * matched whatever followed the map, Leaf's own boxes included, so a row coming or going restyled 27,630 of 28,336 elements at 512 sections. It is now lf-toc + :not(.lf-ui), which clears the same authored blocks and restyles 8,212; this is most of the answering click's gain at 512.

An earlier commit here also skipped the second full layout pass a new row runs (ResizeObserver's first report for boxes the first pass starts watching). Review found it could drop real size changes (a content box narrowing under a classic scrollbar, an SVG target resized under a compensating transform, a writing-mode switch), so it is reverted and both passes run as on main; the test for the scrollbar case stays.

Checked: pnpm run test:runtime (817 pass, with a new annotation-inventory.test.mjs), pnpm run check:browser, and 77 browser tests by node id across margin, comparison, version, thread surface, scrollers, pins, rail, cards and the contents map, on the tree merged with main. Independent Codex review of each commit; the inventory and renderer change and the toc.css selector came back sound. aria-expanded is now asserted in test_a_change_s_card_reads_it_in_order, which fails without cardOpen.

Left for later, measured at 512 sections: a row's arrival still runs two layout passes (about 150 ms each); every render still reads every eligible target's rect (readSpokenPositions, 512 reads) and collects the whole inventory (10–15 ms); ::highlight() rules that match every element still restyle the margin's rows (#5479 scopes the text document's and suggestions'; the comment marks' stay universal, since Firefox paints nothing nested under a scoped form); publishing the page's state takes about 25 ms per click.

Ref #5194

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

…ry the same object

A click on an option of a 512-section page redrew every margin row: each render
presented all 1,024 clusters and repainted every marker's face, and the rail's tab
stop then read every row's geometry again. On main that was about 40 ms of a click's
main thread at 512 sections (`renderNow` 37 ms and `syncRoving` 12 ms in a
`leaf-dev scaling asks --profile` run), for a click whose only change to the margin
was the one row the user answered.

**The inventory says which entries changed.** `annotation-inventory.js` collects
the whole page as before, and an entry now stays the same object from one
collection to the next while what it is made from holds still (`keptEntry`). An
entry is a function of its group, and each thing a group holds is the same object
while it holds still: a published record is shared across publications, each item
is shared against the item at its place in the last collection (`share`), and a
contribution's model is new only when its producer publishes. A group holding what
the last group under its key held keeps that entry, so `marginEntry` (formerly
`marginInventory`, now one group at a time) builds only changed entries. A kept
item takes the latest reading's capabilities, which close over the live target.

A Change entry's item now carries whether its card stands open (`cardOpen`), since
the entry's relation says so and was read outside the entry before; opening the card
makes a new entry, and `test_a_change_s_card_reads_it_in_order` checks the relation
flips.

**The margin draws an entry again only where it changed.** `renderNow` keeps what
each cluster was drawn from: its entry, and the projection's other inputs (the
expanded owner, the card's forced row, the fold, the agent's name). A cluster whose
inputs are the same is not presented again, and its marker is not repainted. A
marker whose entry is unchanged but whose spoken place moved, as every later row's
does when a row leaves ("2 of 2"), gets its new name alone. The rail's tab stop,
the margin's key reflections and its label placement read every row, so they run
again only after a render drew, added or removed a row, a layout pass moved one,
the annotation layer showed or hid, or focus moved onto or off a marker
(`rowsChanged`). A wheel, resize or room change still reads them through
`watchRoom`, as before.

Kept: every row's face, name, relation and seat. Rows are still seated by the same
full layout passes, and a render still collects the whole page, so the inventory's
collection (about 15 ms at 512) and the page-wide work outside the margin remain.

Main-thread CPU per click, base e0c0f9c against this commit, interleaved by
round, size and arm, minimum over 3 rounds of each round's mean (`multiple` is
`leaf-dev scaling asks`'s click, four toggles of one option; `single` answers a
`choose` ask mid-page, so its row leaves and its options' row arrives):

| sections | multiple, main | multiple, this | single, main | single, this |
|---|---|---|---|---|
| 8 | 17.6 | 18.3 | 28.5 | 29.1 |
| 64 | 32.1 | 27.4 | 118.0 | 86.1 |
| 512 | 107.3 | 78.5 | 455.8 | 437.3 |

The host's load was 10 to 35. Two `leaf-dev scaling asks --base` runs read the click
at 512 as 165 against 121 and 165 against 162 ms, and at 64 as 49 against 39 and
57 against 43. A single click at 512 is still mostly the margin's layout pass and a
style recalculation of the whole page as the rows come and go, which this leaves.

Checked: `pnpm run test:runtime` (812 pass, with a new
`tests/runtime/annotation-inventory.test.mjs`); `pnpm run check:browser`; 70
browser tests by node id across `tests/browser/margin/` (clusters, entries, status,
pins, card placement, rail, Page Map, opening threads, card reply) and the
comparison, version and thread-surface tests that read margin entries, all passing,
among them the new `test_a_row_leaving_renames_the_rows_after_it`. Five more fail on
main as on this branch, in a run of main's runtime against this checkout's tests:
`test_agent_progress_stays_on_the_thread_control` (both motions),
`test_an_acknowledgment_uses_status_until_an_active_claim_restores_a_disclosure`,
`test_new_rail_keeps_a_pin_reachable`,
`test_the_current_obligation_marks_its_selected_margin_reading` and
`test_the_margin_groups_meanings_at_one_destination_without_moving_the_page`.

> _This was written by Claude Code on behalf of max-sixty_
…restyling the page

A click that answers a question takes the question's marker off the margin and puts
its options' row on. On a 512-section page that click cost about 450 ms of main
thread in Chromium, and a trace of it showed two costs beside the layout pass
itself.

**A second layout pass.** The pass that seats the new row starts watching the boxes
its target stands among, and the browser delivers each newly watched box once for
being watched. That delivery ran the whole pass again, though nothing had moved. The
pass now records the size it read each box it is about to begin watching at
(`readAt`), and a box's first delivery asks for a pass only where the box no longer
measures that size (`heardSizes`). A box already watched, and a later change to a
new one, ask as before. `test_a_row_coming_and_going_is_laid_out_once` counts one
pass for the click; it counts two on the parent commit.

**A restyle of most of the page.** Taking one row out of the margin's toolbar made
Chromium restyle 27,630 of the page's 28,336 elements (448 ms in one trace).
Bisecting the shipped sheets, then rewriting each selector form in turn, found two
causes, each costing pages that use neither: toc.css's `lf-toc + *`, and the
page's unqualified `::highlight()` rules. With `lf-toc + :not(.lf-ui)`, the same
removal restyles 8,212 elements; Leaf's own boxes are never the block after the
map, so the map's spine clears the same blocks. The `::highlight()` rules are left
as they are (see below).

Kept: every row's seat. Rows are still seated by full passes, as on the parent
commit; only the pass that read nothing new is gone.

Main-thread CPU per click, the parent commit against this one, interleaved by round,
size and arm, minimum over 3 rounds of each round's mean (`single` answers a
`choose` ask mid-page; `multiple` is `leaf-dev scaling asks`'s click, which changes
no row):

| sections | single, parent | single, this | multiple, parent | multiple, this |
|---|---|---|---|---|
| 8 | 31.1 | 26.1 | 16.6 | 17.8 |
| 64 | 76.3 | 56.9 | 27.7 | 31.1 |
| 512 | 475.5 | 243.9 | 83.0 | 92.9 |

The `multiple` click runs no layout pass on either arm, and its rounds overlap (83 to
103 ms on the parent at 512, 93 to 126 here) on a host at load 9 to 20.

Not changed here: the `::highlight()` rules in base.css, annotation-marks.css and
text-document.css match every element, and with them qualified as
`body ::highlight(…)` the removal restyles 3,082 elements rather than 8,212. Shadow
stages adopt annotation-marks.css, where `body` matches nothing, and Firefox paints
highlights only through rules that reach each element
(notes/browser-support.md, "Highlight style inheritance"), so that change needs its
own form for shadow roots and message bodies, and a check in each engine.

Checked: `pnpm run test:runtime` (812 pass); `pnpm run check:browser`;
`test_a_row_coming_and_going_is_laid_out_once`, which fails on the parent commit
(2 passes); and 29 more browser tests by node id that seat rows as boxes resize,
appear, scroll or transition (`tests/browser/margin/` entries, scrollers, pins, card
placement, rail, clusters, opening threads) or place the contents map's spine
(`test_a_status_line_in_the_map_s_track_stands_clear_of_its_spine`,
`test_the_gate_passes_a_contents_map_resting_as_its_spine`), all passing, and
`test_an_untyped_has_leads_only_to_a_typed_subject`, whose docstring named the old
selector.

> _This was written by Claude Code on behalf of max-sixty_
…its first report

The parent commit let a newly watched box's first ResizeObserver delivery skip the
layout pass where the box still measured what the pass had read. It compared the
box's bounding rect, while the observer reports the content box, which narrows
without the whole box changing size: a scrollbar coming, or padding growing. A
passage in a fixed-size `overflow: auto` paragraph whose content narrowed in that
window rewrapped, and its row stayed by the old line until some later pass.

The pass now records each new box's measure in every part the content box follows
from (`measure`): the bounding rect, `clientWidth` and `clientHeight` (inside the
borders, less any scrollbar), and the computed padding. The first delivery asks for a
pass wherever any of them moved. A box already watched asks on every delivery, as
before. `test_a_row_follows_words_rewrapped_as_the_margin_begins_watching_their_box`
grows a paragraph's padding from the page's own stylesheet in the pass that seats a
new row in it; on the parent commit the row stands 109 px from where a full pass
seats it, and here where a full pass does. One case still compares equal: a layout
size change that a transform cancels in the bounding rect.

Kept: the saving. A row's arrival still lays the rows out once
(`test_a_row_coming_and_going_is_laid_out_once`). The pass reads one
`getComputedStyle` more for each box it begins to watch, once per box.

Main-thread CPU per click that answers a `choose` ask mid-page, the parent commit
against this one, interleaved by round, size and arm, minimum over 3 rounds of each
round's mean:

| sections | parent | this |
|---|---|---|
| 8 | 26.6 | 33.2 |
| 64 | 65.6 | 60.5 |
| 512 | 280.9 | 309.3 |

A second run of 512 alone, over 4 rounds, read 254.5 against 245.9. Both arms lay
the margin out once per click at 512 (three clicks each, counted by
`lf-margin-layout`), so the spread is the host's: rounds at 512 ran 281 to 371 ms on
the parent and 246 to 411 here, at load 10 to 13.

Checked: `pnpm run test:runtime` (812 pass); `pnpm run check:browser`; the new test,
which fails on the parent commit; and the 30 node ids the parent commit ran (41
results with parameters), all passing.

> _This was written by Claude Code on behalf of max-sixty_
… as on main

The two commits before this one let a newly watched box's first ResizeObserver
delivery skip its layout pass where the box still measured what the pass had read.
No measure Leaf can read matches what the observer reports in every case. After
24bf2fd added padding and client size to the measure, a review of the source found
two more changes the skip drops: an SVG `<rect>` whose size doubles under
`transform="scale(.5)"` keeps its bounding box, client size and padding while the
observer reports the larger box, and a fixed box that switches writing mode keeps
every physical measure while the observer's logical sizes swap. Each case needed a
rule of its own, and the measure cost two computed-style reads for every box the
margin began to watch. This removes the skip (`readAt`, `heardSizes`,
`measure`), so every delivery runs a pass, as on main, and a row's arrival lays the
rows out twice again.

`test_a_row_coming_and_going_is_laid_out_once` goes with it, since it asserted the
skip. `test_a_row_follows_words_rewrapped_as_the_margin_begins_watching_their_box`
stays: a box's content narrowing in the pass that seats a row is heard only by that
first report, and the row moving with its words is what a user sees.

Kept from 5b06ce3: toc.css's `lf-toc + :not(.lf-ui)`, which keeps a margin row
leaving the chrome from restyling most of the page.

Main-thread CPU per click that answers a `choose` ask mid-page, main (e0c0f9c),
ce5769a (rows drawn only where they changed) and this commit (that, plus the
toc.css selector), interleaved by round, size and arm, minimum over 3 rounds of each
round's mean:

| sections | main | ce5769a | this |
|---|---|---|---|
| 8 | 27.2 | 26.2 | 26.0 |
| 64 | 80.6 | 83.0 | 66.4 |
| 512 | 454.7 | 443.0 | 358.8 |

The first round ran with the host busier than the other two (about 630 ms for every
arm at 512), so the minimum comes from the later rounds. The toc.css selector is what
this commit adds to ce5769a, about 84 ms at 512 and 17 at 64. The click at 512
still holds two full layout passes and a restyle of the toolbar's rows.

Checked: `pnpm run test:runtime` (812 pass); `pnpm run check:browser`; 30 margin,
scroller, pin, rail, card and contents-map tests by node id (40 results with
parameters), the rewrap test among them, all passing.

> _This was written by Claude Code on behalf of max-sixty_
leaf-agent
leaf-agent previously approved these changes Nov 5, 2026
… changed entry

ce5769a added a row to browser/AGENTS.md's "One answer for each question" naming
`keptEntry`, which `annotation-inventory.js` keeps inside the inventory and does not
export, so `test_each_owner_in_the_question_index_is_exported_by_its_module` failed.
No caller asks `keptEntry`: a reader compares the entries the inventory's collection
returns, which stay the same object while unchanged. The row now names the module's
exported owner, `createAnnotationInventory`. No code changes.

Checked: `tests/model/test_owner_index.py` passes; `pnpm run test:runtime` (817 pass).

> _This was written by Claude Code on behalf of max-sixty_
@max-sixty
max-sixty merged commit 6cd5093 into main Nov 5, 2026
4 checks passed
@max-sixty
max-sixty deleted the margin-redraw-changed-rows branch November 5, 2026 12:44
max-sixty added a commit that referenced this pull request Nov 5, 2026
…watches (#5544)

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.

- The column observer (`observer`, `observedColumn`, `observeLayout` and
its disconnect) is deleted; `laneFor` and lane retirement no longer
touch subscriptions. The first registered row still starts watching the
body and column straight away, which #5500 needs, now through
`subscribe`.
- Giving `subscribe` sole ownership also fixes a loss that predates
this: a lane scroller that was also an ancestor of a row's target was
unsubscribed when its lane retired, though the pass still needed it.
- No first delivery is suppressed. #5493 tried that and reverted it
after review showed no measurement Leaf can take matches what the
observer reports; only the duplicate callback is gone.

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_
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