Skip to content

Keep each unchanged record of a reading when a list loses one before it - #6001

Merged
max-sixty merged 2 commits into
mainfrom
grind/send-change-record
Nov 15, 2026
Merged

max-sixty merged 2 commits into
mainfrom
grind/send-change-record

Conversation

@max-sixty

@max-sixty max-sixty commented Nov 15, 2026 •

Copy link
Copy Markdown
Owner

After a sent comment on leaf-dev bench's 1,024-section asks page, the readings the page publishes keep every record the send left alone as the object it was, so every reader that keeps what it read by identity skips them. The publication that takes in the server's reading of the send drops from a median 16.9 ms to 9.8 at 1,024 sections, and the publications from the press until that reading is taken in from 20.5 ms to 14.6 (timed in the page; an independent profile of that window found a 2–6 ms saving, Measurements). Sharing an event log grown by one event also gets cheaper than on main. The other costs #5899 left are measured below; two of them no longer hold on current main.

Why

Commenting on a question hands its obligation to the agent, so the obligation leaves readObligations().user. share (framework/snapshot.js) compared each item of a list with the item at the same index of the prior list. Each record after the one that left was compared with its neighbour's, found different, and published as a new object, so every reader that keeps a reading by identity (kept.js) read all 1,023 of them again. Found by keeping the annotation inventory's entries by their records: the collection after the server's reading remade all 1,024 entries, though only one obligation had changed.

Change

share names a list's records by the rule the server's patch protocol names a list's items by (src/leaf/served_state/tail.py, _named): each record's string id, else its event's id, and only where no two records of the list share a name. Where both lists are named by one rule, a record whose name differs from the one at its index is compared with the prior list's record of its name. A list whose records go by no name, or share one, is compared place by place, as on main. Deep equality still decides whether a subtree is replaced, so only the choice of which prior record each new one is compared with changes.

While every item stands for the prior list's item at its index, by identity or by name, an item past the prior list's end is returned without being compared, so sharing a log grown by an event builds no index of the log.

Lines of mechanism, counting code and leaving out comments, blank lines and tests: 36 added and 8 removed, all in snapshot.js. 20 are the helpers that name a list's records and pair them, and 16 replace the 8-line loop that pairs each item with a prior one.

Measurements

Timed in the page with performance.now() around each outermost call of publish (framework/application.js), unprofiled, at 1,024 sections. No publication ran nested inside another during these sends (counted), so each is counted once. Main and this branch alternated, two runs of five sends each, with the host under heavy load (load average 66–122). Each cell is the median of the ten sends, with the lowest and highest in brackets:

Publications main (ms) this branch (ms)
The press's own 3.2 [2.7–4.7] 2.7 [2.5–4.5]
The one that takes in the server's reading 16.9 [15.1–21.5] 9.8 [8.5–11.6]
From the press until the reading is taken in, summed 20.5 [18.4–27.9] 14.6 [13.1–20.0]
Every one of the send's, until the page settles 23.6 [20.3–30.1] 16.5 [13.6–22.3]

An independent review profiled every publication from the press until the page took in the reading, the third row's window, with V8's sampling profiler, two runs each, on main and on this PR's first commit: a send 17.0 and 16.4 ms against 14.8 and 12.2, a Resolve 18.1 and 14.4 against 12.7 and 11.6, a pick 28.4 and 24.0 against 23.1 and 20.6. That is 2–6 ms over the window, against the timers' 6 ms for a send; the two differ in method, in host load and in the branch's commit.

Sharing a list alone, in Node, the median of 15 interleaved trials (load average about 31), in ms:

List main first commit this branch
1,000 events, grown by one in place 0.040 0.071 0.017
4,000 events, grown by one in place 0.149 0.231 0.027
16,000 events, grown by one in place 0.659 1.330 0.106
16,000 new records, the first one gone 4.8 6.4 7.3

On the last row this branch keeps 15,999 records' identity, where main keeps none; the extra time is naming both lists. A list of fresh records grown by one costs the same as on main (4.9 and 5.1 ms at 16,000).

Chromium, uv run leaf-dev bench asks --sizes 64,256,1024 --input comment --rounds 5 --base 7ec8751318, on this PR's first commit against main, both arms interleaved, load average 5.2 at the start and 17.9 at the end. CPU is the send's main-thread time from the press until 400 ms after its reading shows, less the page's idle time. Each cell is the median of five rounds, with the lowest and highest in brackets.

Sections CPU, main → branch (ms) Shown, main → branch (ms)
64 79.5 [73.3–82.6] → 78.0 [70.7–86.5] 96.7 [87.6–113.0] → 97.0 [88.2–102.6]
256 90.5 [88.1–106.6] → 93.5 [86.1–106.2] 113.2 [112.6–126.7] → 110.5 [109.5–113.1]
1,024 178.1 [160.1–204.3] → 167.1 [158.0–219.2] 185.4 [171.7–204.9] → 187.5 [166.5–196.7]

The rounds' spread at 1,024 sections is wider than the saving, so the CPU column shows the direction and not the size. The paint pass's long-frame attribution put applying the server's reading (paintedOwed) at 39 ms of script per window on main and 15 on this branch, and 42 and 12 in an earlier three-round run. It counts only frames of 50 ms or more, so a frame that drops just under that line leaves the count; it shows that the reading's frames got shorter, not by how much. Paint, the frame that draws the sent message, moves within the rounds' spread: the change is in the reading's application, after that frame.

The costs #5899 listed, on current main

Timed in the page on main (7ec8751318) with performance.now() around each function, unprofiled, median of five sends, before the frame that draws the message:

Step 64 sections 1,024 sections
Margin pass (layoutMarginRows) 1.4 ms 9.3 ms
Annotation inventory (collectEntries) 0.6 ms 6.1 ms
Reading place (recordPlaces; its lineAnchor 0.0 and 0.1 ms) 1.7 ms 3.1 ms
Forced style in the press's task (trace, 22 recalcs) 15.5 ms 17.9 ms
  • Margin pass. Spread over every row. Timed by phase over a send's two passes at 1,024 sections, 15.9 ms in all: seating itself is 2.5 ms, and the rest is finding held rows, carrying each row's record, writing each seat, the new pin's cover reading every child of main, and the layout event's listeners. Seating only the rows a new row can reach would cut the 2.5 ms and leave the rest; making the whole pass follow the change is a restructure of layoutMountedRows, judged not worth it for about 2–6% of a send's CPU.
  • Annotation inventory. I built keeping each item and entry by the records and label it was made from (kept.js), with the kept entries left in their order and only the remade ones placed among them. It cut a collection from 6.1 to 4.5 ms, but an interleaved five-round A/B against this branch read the send's CPU at 1,024 sections as 171.8 ms without it and 170.6 with it, for about 75 lines, so it is not here. The cost is visiting every obligation and resolving its target (getElementById, targetPath, the label, the group map), about 4.5 µs each; making entries is about 0.6 ms of it. Skipping records whose list didn't change would have to resolve targets by id and read labels from attributes the page's reading doesn't hear, and an entry stays one object until what it is made from changes holds that an aria-label change shows at the next collection.
  • Reading place text search. Gone on current main (Find a selection's copies by string search, and search the page a part at a time #5875 changed that search): lineAnchor takes 0.1 ms. capturePlace still grows, 0.7 → 2.2 ms, from visiting every text block to find those on screen (textBlocks, renderedBlocks).
  • Forced style. It does not grow with the page, and no rule restyles more than the card: each of the 22 recalcs touches at most 97 elements, all in the thread card, the composer, one margin row or their ancestors (invalidation tracking in an unprofiled trace). --lf-ring-at is registered as non-inheriting (base.css, since Say where a box's marks stand in --lf-ring-at, and draw the location ring as a mark over the decision #5116), so focus rules setting it restyle only the box they match. The 16–18 ms is the number of places a script reads style or layout after a write in the press's task: scrollToEnd in showArrival, motionTime reading a token twice, focusBox, cardMinimum. That is a question of the card's build order rather than of CSS.

The margin projection's per-row work grows too: renderNow 2.1 → 6.3 ms, of which readSpokenPositions (each target's place down the page) is 2.6 and nameMarkers 1.8, and the Tab stop (syncRoving) 0.3 → 2.5.

One record of what changed?

The brief asked whether one shared record of what changed would replace readingElements (#5872), the derive steps' memo (#5899), Footprints (#5866) and drawnElements (#5871). It would not:

  • drawnElements keeps nothing between calls. It walks what the browser draws, asking checkVisibility, and skips the rest.
  • Footprints is an index of seats built in each margin pass and dropped at its end. The margin's record across passes is seatings, which the page's observers drop: a resize, a mutation, a scroll inside a box, a transition's end.
  • readingElements and the derive memo are records of change for different inputs. The memo compares immutable state by identity, which needs no observer because publication shares every unchanged subtree. readingElements keeps its answers on pieces of the page's reading, which the reading's mutation observer drops.

These already compose. The page's reading exposes its state as an identity token, pageReadingVersion, which an identity-keyed reading takes as one more input, as gatherConversations (thread/placement.js) and prepareVisualActions (anchor-controls.js) do. Geometry is a third kind of change, reported by resize observers, scroll events and transition ends rather than by a mutation, so folding seatings into the same record would move it only partway. What made a cost like these hard to remove was a fault in the identity record itself, which this fixes: positional sharing broke identity for every record after one that moved, and each identity-keyed reader paid for it.

Checked

  • pnpm run test:runtime (970 pass) and pnpm run check; pre-commit on both files.
  • The new test, sharing a list keeps each record equal to the prior one of its name (framework-snapshot.test.mjs), fails on main and on this PR's first commit: records lost, gained mid-list and reordered as new objects, records named by their events, a list of unnamed records, and two records sharing an id, which stay two objects.
  • The review's fuzz of 20,000 lists passes: every result deep-equals the new list, reuses a prior subtree only where it is equal, and in no list do two places become one object (104 to 108 lists did on the first commit).
  • 41 browser tests, all passing on the final head, selected with -k from widgets/test_obligations.py, threads/test_resolving.py, test_render_semantic_news.py, margin/test_page_map.py, margin/test_entries.py, test_render_options.py, threads/test_panel_list.py, projection/test_widget_actions.py, projection/test_thread_replies.py and projection/test_live_versions.py: obligations answered, cleared and walked; threads resolved and reopened; agent news; Page Map reconciliation; margin rows leaving; undo and refused picks; live revisions.

Not checked

  • Firefox and WebKit.
  • The broad suite and the main-push browser tier.
  • Which lists besides the user's obligations shift in a send's reading; the publication time is measured, not attributed list by list.

Calls Max may reverse

  • The annotation inventory's kept entries are left out, measured as above.
  • Where the two lists are named by different rules, share compares records place by place, where the server's patch treats every item as new. Both are correct; place by place can still find an equal record.
  • A long list of new records that loses its first one costs more than on main (the table above), in exchange for keeping every record's identity.

Part of #5194.

Publishing the server's reading of a sent comment on `leaf-dev bench`'s
1,024-section asks page took 18.3 ms; it now takes 9.9 ms, and every
reader that keeps what it read by identity keeps the records the send
left alone.

Commenting on a question hands its obligation to the agent, so it leaves
`readObligations().user`. `share` (framework/snapshot.js) compared each
item of a list with the item at the same index of the prior list, so each
record after the one that left was compared with its neighbour's, found
different and published as a new object, and every identity-keyed reader
(kept.js) read all 1,023 of them again. `share` now compares a record
that has an `id` with the prior list's record of that id when the item at
its index has another id. Deep equality still decides whether a subtree is
replaced, so only the choice of which prior record a new one is compared
with changes.
…g for a list grown at its end

Review of the first commit found three faults in it.

A list grown at its end built an index of the prior list by id as well as
the set of its items, and the lookup then missed. The event log meets this
at least twice in each reading that adds events, so sharing it cost twice
what it did on main, a cost that grows with the session. `share` now
returns an item past the prior list's end without comparing it, as long as
every item before it stood for the prior list's item at its index, by
identity or by name. Sharing a 16,000-event log grown by one event takes
0.11 ms, from 0.66 on main and 1.33 at the first commit (Node, the median
of 15 interleaved trials).

Two records sharing an id were both paired with one prior record, so two
places in the list became one object. `share` now names a list's records
by the rule the server's patch protocol names them by
(`src/leaf/served_state/tail.py`, `_named`): each record's string `id`,
else its event's, and only where no two records share a name. Elsewhere it
compares each record with the one at its index, as on main.

The test's last assertion held for any implementation. It now checks the
record kept against the prior list, and adds a record gained mid-list, a
reorder of new objects, records named by their events, and a list where two
records share an id.
@max-sixty
max-sixty marked this pull request as ready for review November 15, 2026 01:40
@max-sixty
max-sixty merged commit 0a657b1 into main Nov 15, 2026
6 checks passed
@max-sixty
max-sixty deleted the grind/send-change-record branch November 15, 2026 01:47
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