Skip to content

Re-read only what a closed tab's draw changed, so each document reports its draw as it lands - #5872

Merged
max-sixty merged 2 commits into
mainfrom
grind/closed-tab-passes
Nov 12, 2026
Merged

max-sixty merged 2 commits into
mainfrom
grind/closed-tab-passes

Conversation

@max-sixty

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

Copy link
Copy Markdown
Owner

After a closed tab's document draws, the document pass that follows takes 5–10 ms in Chromium and, at the median, 6–12 ms in Firefox, whether the page holds 3 text documents of 4 MB or 30. On main it took 340–410 ms in Chromium and 380–960 ms in Firefox, most of it re-reading documents that hadn't changed. With the pass that cheap, #5371's batching of closed tabs' draw reports goes: each document reports its draw as it lands. On #4781's page the longest task while Doc 5 is rewritten drops from 847–968 to 494–576 ms in Chromium and from 1,364–2,044 to 815–1,021 ms in Firefox, and the longest frame while the documents draw from 882–985 to 624–738 ms in Chromium. Load times move less: in Chromium the comment on Doc 23 stood on its words 0.2–0.9 s sooner round for round, and in Firefox the closed tabs' times overlap between arms, so no Firefox load-time win is claimed.

What re-read the page after each draw

A document's draw report (drawn, which stamps its data's provenance) starts a document pass. On #4781's page (30 tabs, each a 4 MB text document, one comment quoting a line of Doc 23), three parts of that pass read far more than the document that drew:

  • The annotation inventory keyed the comment's margin row by reading all of Doc 23 again (passageRows): 198 ms in Chromium. The key depends only on the target's words and the passage, so it is now kept under the target's piece of the page's reading (keptAnswer, from Keep the page's reading by part, so a load or a gesture reads only the documents it needs #5570), by where the passage starts and what it says. A collection reads a target again only when its words change or a passage in it is new.
  • Two labels in the same collection, the outline subject and the row's title, each collapsed and split all of Doc 23's words to cut a 60-character excerpt: 23 ms each. excerptWords now reads only as much of a value as holds more than its cap once collapsed, doubling the read until it does. On 200,000 random values (whitespace runs, surrogate pairs, caps 1–80) it cuts the same as the whole-value rule.
  • The visual action plan queried every element of the page for pictures (svg, img, figure and declared visuals): 57 ms on 30 documents. It now asks readingElements(visualTags()), new in passages.js, for the elements with those tags that stand where the page reading reads its words, in reading order. Each piece's are kept until that piece changes. The frame's are among the elements its walk enters, which its outline already records (over); a part's come from native queries of its trees and of what their slots show, keeping those the walk reaches: 2 ms for one document. It takes tag names rather than a selector because the watcher drops changes to id and class, so a selector on them would go stale. On all 24 shipped examples it returns the same candidates in the same order as the old query.

The Layout's header was found by :scope > header on the layout box, which walks every element under it, 6 ms a layout read here. It is now read off the box's children, as passages.js already does for main.

The report batching goes

#5371 made closed tabs' documents report their draws together once the last had drawn, because each report's pass grew with the page; its comment called the batching a stopgap until a pass costs what the change it follows does. With the pass at 5–12 ms, the batching only holds a comment on a closed document back until every other document has drawn. Each document now marks its text and dispatches drawn in the turn it draws. The queue that draws closed tabs after presentation, one a task, stays: it keeps presentation from waiting on them.

A third arm kept the batching on top of this branch's passes ("batched" below). It placed the comment later than this branch in two of three Chromium rounds and drew every document no sooner. Without the batching, Firefox drew the last document 0.2–0.5 s later than main in each of these rounds, about the 29 passes of 8–12 ms that now run as documents draw rather than none until the last; an independent review's own Firefox loads put the closed tabs' medians at 9.2 s and 9.8 s, overlapping, and saw no extra long frame per document.

Measurements

These numbers are from the first commit. The second changes only which elements readingElements keeps and their order; timed again on the 30-document page, two loads per engine, its pass after a closed document's report reads 6.3–6.7 ms median in Chromium and 8.0–10.5 ms in Firefox, and Doc 5's rewrite 5.5–5.9 ms and 7–11 ms.

Each arm served a fresh copy of #4781's page directory from its own tree (leaf server run --lifetime temporary, throwaway state home): main at ce24dda, this branch, and this branch with main's lf-text-document.js ("batched"), each on the 30-document page, and main and this branch on a copy holding only Docs 0, 5 and 23. Rounds alternated the arms in an order that rotated each round, one warmed browser per round and engine (Chromium 156 headless, Firefox 157, through Playwright), each load in a new context, load average 8–26 from other sessions. After every document drew and the comment stood on its words, a wheel over Doc 0 warmed the reading place, then leaf data set rewrote one line of Doc 5, a closed tab. Times are milliseconds on the page's clock; each cell is the range over three loads.

The document pass, timed by wrapping each region's paint in the arms' runtimes (semantic-state.js) and summing paints that run back to back. Main reports every closed document in one task, so it has no pass per document; its passes over the load follow that one task.

Chromium main, 30 this branch, 30 main, 3 this branch, 3
Pass after a closed document's report, but Doc 23's: median – 6.4 – 6.5–7.9
Same, longest – 7.5–10.3 – 6.5–7.9
Pass after Doc 23's report, which places the comment – 599–610 – 592–641
Passes over the load after presentation, summed 796–860 782–794 662–726 599–649
Doc 5 rewritten: passes 373–414 5.7–6.7 338–371 4.9–6.1
Firefox main, 30 this branch, 30 main, 3 this branch, 3
Pass after a closed document's report, but Doc 23's: median – 8–12 – 6–11
Same, longest – 9–35 – 6–11
Pass after Doc 23's report, which places the comment – 190–364 – 178–1,237
Passes over the load after presentation, summed 984–1,321 825–1,723 855–1,430 617–3,524
Doc 5 rewritten: passes 563–961 6–182 383–610 6–36

Firefox's ranges follow the host's load average: the 182 ms rewrite and the 1,237 ms placement are single loads at 24–26, whose arms' other loads read 6–12 ms and 178–230 ms.

Load and rewrite as the page shows them. The longest task is the longest gap in a chain of zero timeouts, which only main-thread work holds back.

Chromium main, 30 this branch, 30 batched, 30 main, 3 this branch, 3
Every document drawn 3,341–4,349 3,179–4,203 3,126–4,541 1,251–1,634 1,207–1,612
Comment on its words 3,341–4,349 2,753–3,725 3,127–4,541 1,251–1,634 1,208–1,612
Load: longest frame 882–985 624–738 815–872 720–754 645–673
Doc 5 rewritten: longest task 847–968 494–576 474–522 667–738 339–379
Firefox main, 30 this branch, 30 batched, 30 main, 3 this branch, 3
Every document drawn 9,367–12,465 9,840–13,005 10,402–16,421 2,560–4,097 2,567–5,266
Comment on its words 9,367–12,465 8,240–11,085 10,402–16,421 2,560–4,097 2,567–5,266
Load: longest frame 1,099–1,588 997–1,397 1,187–2,912 987–1,379 864–1,658
Doc 5 rewritten: longest task 1,364–2,044 815–1,021 812–1,744 1,113–1,866 587–1,034

In Firefox the time until every document has drawn is mostly the documents' own draws: 29 frames over 150 ms, 7.4–9.9 s together, in every arm.

Where Chromium's main thread went in the rewrite of Doc 5, from a CPU profile of each arm, two loads each, alternated:

ms main, 30 this branch, 30 main, 3 this branch, 3
The document pass 314–320 6–7 275–280 6
Doc 5's own draw 283–284 293–294 293 283–309
Decoding the state answer 101–105 103 10–11 11
Garbage collection 147–150 46–47 153–174 19–33
Native, unattributed 132–162 131–137 51–59 56–63
Total 1,084–1,128 680–689 839–862 422–472

The rewrite still costs 210–270 ms more on 30 documents than on 3, none of it in the pass. The state answer that follows any data change carries every source's value, since Leaf-Data-Version names the data as a whole (http.py): 124 MB here against 12 MB. Decoding it took 108 ms against 10 in Chromium and 143 against 24 in Firefox, besides the copy, its comparison with the held data (share) and its garbage. That is the data delivery, which this change leaves as it was.

Checks

  • tests/runtime/reading-pieces.test.mjs: after each kind of change it already applies, readingElements returns every element of the page with the asked tags outside chrome, each once, compared by identity, slotted and staged ones included; its order matches the outline's; a part's elements are those the walk reaches, in its order, leaving out a light child no slot shows; and a change inside one part asks nothing of another part's elements. The elements helper now reaches a declared tree inside another. Keeping a part's or the frame's matches past a change, or removing the reach filter, the reading-order sort or the frame's read filter, turns a test red.
  • tests/runtime/annotation-inventory.test.mjs, new test: a collection after a change elsewhere reads none of the quoted target's elements and keeps its key; a change to the target's words reads it again and renumbers the key. Removing the keeping turns it red.
  • tests/runtime/words.test.mjs, new: an excerpt of a long value cuts as one of all of it would, past a run of whitespace and a surrogate pair split where the first read ends.
  • tests/browser/widgets/test_code_and_documents.py: the drawing-order test now expects each closed document to announce as it draws, and the test of a tab opened while documents draw becomes one that a document reports its draw while later ones still wait. Both fail with main's lf-text-document.js.
  • pnpm run test:runtime (950 pass), pnpm run check:browser, pre-commit on the changed files.
  • Browser tests at this head, by -k: the text-document tests in test_code_and_documents.py (25), the visual-target tests in test_render_reactions.py, test_render_semantic_selection.py and test_render_pages.py with startup/test_readiness.py::test_visual_comments_arrive_with_the_initialized_document (26), and the quoted-passage rows, revisions and sticky headers in margin/, test_render_anchors.py and test_workspaces_and_bounds.py (13). Three fail, each the same way on main's tree here (c5739b8): test_render_pages.py::test_a_marked_scrolling_visual_keeps_its_keyboard_focus_ring (the diagram has no tabindex; it also fails at ce24dda), test_render_semantic_selection.py::test_dense_selection_hints_stay_short_and_reach_an_atomic_visual, and test_code_and_documents.py::test_a_text_document_update_keeps_a_selection_it_carries_into_another_run[browser], which passed here before the rebase onto c5739b8.

Calls Max may reverse

  • The batching goes. A page of many closed documents now runs a pass per document as each draws, 5–12 ms each here, where it ran one at the end; in Firefox the last document drew 0.2–0.5 s later than on main.
  • The plan's pictures are the page reading's. readingElements keeps the elements the walk reaches where the page reads its words, so a picture among a declared widget's light children that no slot shows, which nobody sees, or inside silenced words, no longer gets a "Respond to" proxy; candidates come in reading order rather than the light tree's followed by each shadow tree's. On the shipped examples nothing differs.

Not verified

  • WebKit.
  • A comment quoting prose whose block is in the frame and holds parts, such as words directly in a section of documents: its target is read again after every change, as on main.

Part of #5194. All four costs that issue names stay open after this: per-input work growing with the square of the widgets, images refetched on every load, /api/state resending the whole state on every change (here 124 MB after a one-line data change on the 30-document page), and forced layout while scrolling past comment threads. This PR cuts a neighbouring cost, the passes after a closed tab's document draws.

…ts its draw as it lands

After a closed tab's document draws, the document pass that follows takes 5–10 ms in Chromium and, at the median, 6–12 ms in Firefox, whether the page holds 3 text documents of 4 MB or 30. On main it took 340–410 ms in Chromium and 380–960 ms in Firefox, most of it re-reading documents that hadn't changed. With the pass that cheap, #5371's batching of closed tabs' draw reports goes: each document reports its draw as it lands. On #4781's page the comment on Doc 23 now stands on its words 0.2–0.9 s sooner in Chromium and 1.0–1.6 s sooner in Firefox, round for round.

## What re-read the page after each draw

A document's draw report (`drawn`, which stamps its data's provenance) starts a document pass. On #4781's page (30 tabs, each a 4 MB text document, one comment quoting a line of Doc 23), three parts of that pass read far more than the document that drew:

- **The annotation inventory** keyed the comment's margin row by reading all of Doc 23 again (`passageRows`): 198 ms in Chromium. The key depends only on the target's words and the passage, so it is now kept under the target's piece of the page's reading (`keptAnswer`, from #5570), by where the passage starts and what it says. A collection reads a target again only when its words change or a passage in it is new.
- **Two labels** in the same collection, the outline subject and the row's title, each collapsed and split all of Doc 23's words to cut a 60-character excerpt: 23 ms each. `excerptWords` now reads only as much of a value as holds more than its cap once collapsed, doubling the read until it does. On 200,000 random values (whitespace runs, surrogate pairs, caps 1–80) it cuts the same as the whole-value rule.
- **The visual action plan** queried every element of the page for pictures (`svg, img, figure` and declared visuals): 57 ms on 30 documents. It now asks `readingElements(selector)`, new in `passages.js`, which keeps each piece's matches until that piece changes. The frame's come from the elements its walk already enters, and a part's from native queries of its trees and of what their slots show: 2 ms for one document. On all 24 shipped examples it returns the same candidates in the same order as the old query.

The Layout's header was found by `:scope > header` on the layout box, which walks every element under it, 6 ms a layout read here. It is now read off the box's children, as `passages.js` already does for `main`.

## The report batching goes

#5371 made closed tabs' documents report their draws together once the last had drawn, because each report's pass grew with the page; its comment called the batching a stopgap until a pass costs what the change it follows does. With the pass at 5–12 ms, the batching only holds a comment on a closed document back until every other document has drawn. Each document now marks its text and dispatches `drawn` in the turn it draws. The queue that draws closed tabs after presentation, one a task, stays: it keeps presentation from waiting on them.

A third arm kept the batching on top of this branch's passes ("batched" below). It placed the comment later than this branch in two of three Chromium rounds and all three Firefox rounds, and drew every document no sooner. Without the batching, Firefox drew the last document 0.2–0.5 s later than main in each round, about the 29 passes of 8–12 ms that now run as documents draw rather than none until the last.

## Measurements

Each arm served a fresh copy of #4781's page directory from its own tree (`leaf server run --lifetime temporary`, throwaway state home): main at ce24dda, this branch, and this branch with main's `lf-text-document.js` ("batched"), each on the 30-document page, and main and this branch on a copy holding only Docs 0, 5 and 23. Rounds alternated the arms in an order that rotated each round, one warmed browser per round and engine (Chromium 156 headless, Firefox 157, through Playwright), each load in a new context, load average 8–26 from other sessions. After every document drew and the comment stood on its words, a wheel over Doc 0 warmed the reading place, then `leaf data set` rewrote one line of Doc 5, a closed tab. Times are milliseconds on the page's clock; each cell is the range over three loads.

The document pass, timed by wrapping each region's paint in the arms' runtimes (`semantic-state.js`) and summing paints that run back to back. Main reports every closed document in one task, so it has no pass per document; its passes over the load follow that one task.

| Chromium | main, 30 | this branch, 30 | main, 3 | this branch, 3 |
|---|---|---|---|---|
| Pass after a closed document's report, but Doc 23's: median | – | 6.4 | – | 6.5–7.9 |
| Same, longest | – | 7.5–10.3 | – | 6.5–7.9 |
| Pass after Doc 23's report, which places the comment | – | 599–610 | – | 592–641 |
| Passes over the load after presentation, summed | 796–860 | 782–794 | 662–726 | 599–649 |
| Doc 5 rewritten: passes | 373–414 | 5.7–6.7 | 338–371 | 4.9–6.1 |

| Firefox | main, 30 | this branch, 30 | main, 3 | this branch, 3 |
|---|---|---|---|---|
| Pass after a closed document's report, but Doc 23's: median | – | 8–12 | – | 6–11 |
| Same, longest | – | 9–35 | – | 6–11 |
| Pass after Doc 23's report, which places the comment | – | 190–364 | – | 178–1,237 |
| Passes over the load after presentation, summed | 984–1,321 | 825–1,723 | 855–1,430 | 617–3,524 |
| Doc 5 rewritten: passes | 563–961 | 6–182 | 383–610 | 6–36 |

Firefox's ranges follow the host's load average: the 182 ms rewrite and the 1,237 ms placement are single loads at 24–26, whose arms' other loads read 6–12 ms and 178–230 ms.

Load and rewrite as the page shows them. The longest task is the longest gap in a chain of zero timeouts, which only main-thread work holds back.

| Chromium | main, 30 | this branch, 30 | batched, 30 | main, 3 | this branch, 3 |
|---|---|---|---|---|---|
| Every document drawn | 3,341–4,349 | 3,179–4,203 | 3,126–4,541 | 1,251–1,634 | 1,207–1,612 |
| Comment on its words | 3,341–4,349 | 2,753–3,725 | 3,127–4,541 | 1,251–1,634 | 1,208–1,612 |
| Load: longest frame | 882–985 | 624–738 | 815–872 | 720–754 | 645–673 |
| Doc 5 rewritten: longest task | 847–968 | 494–576 | 474–522 | 667–738 | 339–379 |

| Firefox | main, 30 | this branch, 30 | batched, 30 | main, 3 | this branch, 3 |
|---|---|---|---|---|---|
| Every document drawn | 9,367–12,465 | 9,840–13,005 | 10,402–16,421 | 2,560–4,097 | 2,567–5,266 |
| Comment on its words | 9,367–12,465 | 8,240–11,085 | 10,402–16,421 | 2,560–4,097 | 2,567–5,266 |
| Load: longest frame | 1,099–1,588 | 997–1,397 | 1,187–2,912 | 987–1,379 | 864–1,658 |
| Doc 5 rewritten: longest task | 1,364–2,044 | 815–1,021 | 812–1,744 | 1,113–1,866 | 587–1,034 |

In Firefox the time until every document has drawn is mostly the documents' own draws: 29 frames over 150 ms, 7.4–9.9 s together, in every arm.

Where Chromium's main thread went in the rewrite of Doc 5, from a CPU profile of each arm, two loads each, alternated:

| ms | main, 30 | this branch, 30 | main, 3 | this branch, 3 |
|---|---|---|---|---|
| The document pass | 314–320 | 6–7 | 275–280 | 6 |
| Doc 5's own draw | 283–284 | 293–294 | 293 | 283–309 |
| Decoding the state answer | 101–105 | 103 | 10–11 | 11 |
| Garbage collection | 147–150 | 46–47 | 153–174 | 19–33 |
| Native, unattributed | 132–162 | 131–137 | 51–59 | 56–63 |
| Total | 1,084–1,128 | 680–689 | 839–862 | 422–472 |

The rewrite still costs 210–270 ms more on 30 documents than on 3, none of it in the pass. The state answer that follows any data change carries every source's value, since `Leaf-Data-Version` names the data as a whole (`http.py`): 124 MB here against 12 MB. Decoding it took 108 ms against 10 in Chromium and 143 against 24 in Firefox, besides the copy, its comparison with the held data (`share`) and its garbage. That is the data delivery, which this change leaves as it was.

## Checks

- `tests/runtime/reading-pieces.test.mjs`: after each kind of change it already applies, `readingElements` returns every element of the page that matches, each once, compared by identity, slotted and staged ones included; its order matches the outline's; and a change inside one part asks nothing of another part's elements. The `elements` helper now reaches a declared tree inside another. Keeping a part's or the frame's matches past a change turns a test red.
- `tests/runtime/annotation-inventory.test.mjs`, new test: a collection after a change elsewhere reads none of the quoted target's elements and keeps its key; a change to the target's words reads it again and renumbers the key. Removing the keeping turns it red.
- `tests/runtime/words.test.mjs`, new: an excerpt of a long value cuts as one of all of it would, past a run of whitespace and a surrogate pair split where the first read ends.
- `tests/browser/widgets/test_code_and_documents.py`: the drawing-order test now expects each closed document to announce as it draws, and the test of a tab opened while documents draw becomes one that a document reports its draw while later ones still wait. Both fail with main's `lf-text-document.js`.
- `pnpm run test:runtime` (949 pass), `pnpm run check:browser`, pre-commit on the changed files.
- Browser tests by `-k`: the text-document tests in `test_code_and_documents.py` (25), the visual-target tests in `test_render_reactions.py`, `test_render_semantic_selection.py` and `test_render_pages.py` (25), and the quoted-passage rows, revisions and sticky headers in `margin/`, `test_render_anchors.py` and `test_workspaces_and_bounds.py` (13). All pass but `test_render_pages.py::test_a_marked_scrolling_visual_keeps_its_keyboard_focus_ring`, which fails the same way on main's tree here: the diagram has no `tabindex`.

## Calls Max may reverse

- **The batching goes.** A page of many closed documents now runs a pass per document as each draws, 5–12 ms each here, where it ran one at the end; in Firefox the last document drew 0.2–0.5 s later than on main.
- **The plan's pictures are the page reading's.** `readingElements` reaches what the page reading reaches, so a picture among a declared widget's light children that no slot shows, which nobody sees, no longer gets a "Respond to" proxy, and candidates come in reading order rather than the light tree's followed by each shadow tree's. On the shipped examples nothing differs.

## Not verified

- WebKit.
- A comment quoting prose whose block is in the frame and holds parts, such as words directly in a `section` of documents: its target is read again after every change, as on main.

Part of #5194
… in its order

Review of the first commit found three ways `readingElements` could disagree with
the page reading it keeps pieces of:

- It took any selector, and its JSDoc allowed one on the markers the watcher reads.
  The watcher drops a change to `id`, `class`, and `name` outside a slot, so a
  selector such as `p[id], p.marked` went stale: kept ["One"], fresh ["One", "Two",
  "In part"]. It now takes tag names, which no change makes an element gain or lose
  in place, and keeps only elements standing where the reading reads its words, whose
  arrival and departure the watcher hears. The plan asks it for `visualTags()`, the
  tags its `visualSelector` joined.
- Inside a part it returned a declared host's light child that no slot shows, which
  the frame leaves out, and listed the part's light tree before each shadow tree. A
  part's matches now keep only elements the walk reaches (`walkedTo`) and come in
  reading order (`readingOrder`).
- The frame's walk recorded its elements in a list of its own, which repeated the
  keys of its outline's `over` map, entered in the same order with each part's root
  included. The list is gone and `readingElements` reads the outline.

`tests/runtime/reading-pieces.test.mjs` adds a part holding a declared host with a
shown and an unshown child and a paragraph after it, and holds the membership check
to tags outside chrome. Removing the walk filter, the reading-order sort or the frame's
read filter turns a test red.
@max-sixty
max-sixty marked this pull request as ready for review November 12, 2026 07:54

@leaf-agent leaf-agent left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The description says Part of #5194 but not what of that issue remains once this lands. #5194 names four costs: per-input work growing with the square of the widgets, images refetched on every load, /api/state resending the whole state on every change, and forced layout while scrolling past comment threads. This PR cuts a related cost, the document pass after a closed tab's document draws. A line saying which of the four are still open would let whoever lands the last part tell that it finishes the issue.

@max-sixty
max-sixty merged commit a9b6042 into main Nov 12, 2026
6 checks passed
@max-sixty
max-sixty deleted the grind/closed-tab-passes branch November 12, 2026 08:04
max-sixty added a commit that referenced this pull request Nov 15, 2026
…it (#6001)

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 7ec8751`, 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 (#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 #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.
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