Skip to content

Label every revision in one pass over the log - #5889

Merged
leaf-agent merged 1 commit into
mainfrom
fix/issue-5886
Nov 12, 2026
Merged

leaf-agent merged 1 commit into
mainfrom
fix/issue-5886

Conversation

@leaf-agent

Copy link
Copy Markdown
Collaborator

The served state's revision_labels map and every descriptor labelled a revision with revision_label, which scanned the whole log twice (once for its own stamp, once for earlier stamps) per revision. A page with many revisions and a long log therefore paid revisions × events per reading. files.revision_labels now sorts the log's note stamps once and labels each requested revision by bisecting them, so labelling every revision costs (events + revisions) × log. The labels themselves are unchanged: vN for a stamped revision, Draft after vN naming the newest stamp on an earlier revision, else Draft. Each revision carries at most one stamp, which publishing._stamp_reading enforces.

On this runner, uv run leaf-dev bench revisions --read server --sizes 256,1600 --rounds 1 --profile reads state ms at 1600 revisions as 161.8 before and 26.0 after. After the change, --profile no longer reports revision_label or stamped_version as superlinear, and it reports no other superlinear function for state or agent.

Checks run
  • New test_revision_labels_name_each_draft_after_the_newest_earlier_stamp covers stamped revisions, drafts after a stamp and drafts before any stamp.
  • tests/model/test_model_folds.py and tests/process/test_interact_server.py (which asserts Draft after v1 on a descriptor): 511 passed.

Closes #5886 — automated triage

The served state and `leaf page state` labelled each revision with
`revision_label`, which scanned the whole log twice per revision, so a
reading grew with revisions times events. `revision_labels` sorts the
log's stamps once and labels each revision by bisecting them, so the
reading grows with the page times its logarithm, as root AGENTS.md,
"Responsiveness", requires. Labels are unchanged.

Closes #5886
@leaf-agent
leaf-agent merged commit 5bb337b into main Nov 12, 2026
6 checks passed
@leaf-agent
leaf-agent deleted the fix/issue-5886 branch November 12, 2026 08:56
max-sixty added a commit that referenced this pull request Nov 12, 2026
… the margin once per card open, and skip clip walks for boxes off screen (#5893)

A sent comment on the generated asks page at 1,024 sections now draws in
a median 136 ms in Chromium, down from 171 ms on main, and shows the
server's reading at 214 ms, down from 271 ms. A Resolve shows the
server's reading at 135 ms, down from 177 ms. That is still above the
100 ms root `AGENTS.md` ("Responsiveness") asks for; what remains is
listed at the end. The largest part of the gain comes from DOM writes,
which were slow on a long page whatever made them, so it reaches beyond
the send. In Firefox the send's main-thread time at 1,024 sections fell
from 299 ms to 269 ms, measured with this branch's first commit.

## Why

Profiling the send at 1,024 sections on main after #5866 left these
growing with the page in the press's task and the frame after it, in ms
of real (unprofiled) time from timers placed around each function,
against the same at 8 sections:

| Work | 8 sections | 1,024 sections |
| --- | ---: | ---: |
| Thread card build (`buildThreadCard`) | 7.8 | 17.6 |
| Annotation paints, two in the press's task (`paintAnnotations`) | 2.8
+ 2.0 | 21.3 + 12.9 |
| The send's publication (`publish`) | 1.0 | 15.1 |
| Recording the reading place (`recordPlaces`) | 0.5 + 1.8 | 10.7 + 3.1
|
| Margin pass (`layoutMarginRows`) | 1.3 | 9.3 |
| Tab stop (`syncRoving`) | 0.0 | 6.1 |

The card build's growth had no reason in the card. Timing a DOM write
alone found it: on the 1,024-section page, inserting and removing one
empty element took half a millisecond, against none at 8 sections, and
the same for an element never attached to the document.

## Changes

**Measuring a node's lines reuses one range.** A `Range` stays live
until a garbage collection reclaims it, and Chromium updates every live
range of a document on each insertion and removal, attached or not. The
margin's cover reading (`childIndex`, margin-layout.js) made a range for
each text node it measured: 18,432 of them during load at 1,024
sections, by a count of `createRange` calls, and every pass that reads
all the rows makes its own. Until a major collection, every DOM write on
the page walked all of them. 2,000 insert-and-remove pairs on a detached
element took 1,110 ms on the loaded page, 35 ms after forcing a
collection, and take 24 ms with this change, uncollected. shown-box.js
now keeps one range for reading a node's contents (`contentsRects`,
`contentsBox`, and `elementBox` for a `display: contents` element),
which lets go of the node once read. A thread card's head fit
(`wordsWidth`), a heading link's first line (`firstLine`) and a text
field's send corner (`#cornerTaken`) measured through ranges of their
own and now read through the shared one, and `browser/AGENTS.md` names
it under "One answer for each question". A count of the ranges made
while typing, sending, resolving and scrolling now finds tens per
gesture; the largest left are `caretAt` (editor-view.js), about 4 per
keystroke, and `rangeOf` (passages.js), 54 per send, listed below. The
review measured a keystroke at 1,024 sections at 6.5 ms of main thread,
from 6.9 ms.

**Opening a thread card paints the margin once.** `showPreview` redrew
the whole margin after the card opened (#5420), only so that the
redraw's tracking would follow the card's message ages. A send paid that
as a second full annotation paint in its task. The card's thread now
follows its own reads instead (`follow`, application.js, the mechanism
the margin's direct redraws already used), so the card's ages still
advance with the clock and the margin is painted once.
`test_message_age_may_shift_metadata_but_leaves_the_thread_in_place[card]`
fails with the card's draw left unfollowed and passes with it.

**A box outside the visible viewport skips its clip walk.** `seenRect`
(geometry.js) cuts a box by every box that clips it and then by the
visible viewport's height. The clips only cut, so a box the viewport's
height doesn't reach is now answered null before the walk. Choosing the
margin's Tab stop asks this of every marker.

## Measurements

Chromium, `uv run leaf-dev bench asks --sizes 256,1024 --input comment
--input resolve --rounds 3 --base 5bb337b`: both arms interleaved in
one run, this branch's head against its merge base with main (#5889),
load average 7.1 at the start and 6.0 at the end. Each cell is the
median of 3 rounds, with the range in brackets. Paint is from the input
to the frame that draws 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 | 8 | 57 [54–88] → 52 [51–87] | 88 [83–90] → 87 [71–90] | 82
[75–85] → 81 [74–85] |
| Send | 256 | 79 [72–82] → 68 [68–70] | 119 [105–121] → 105 [105–107] |
110 [104–115] → 104 [102–106] |
| Send | 1,024 | 171 [162–177] → 136 [130–142] | 271 [269–278] → 214
[206–233] | 258 [258–274] → 209 [198–213] |
| Resolve | 8 | 35 [19–36] → 18 [17–19] | 35 [30–36] → 30 [27–31] | 34
[32–39] → 36 [35–37] |
| Resolve | 256 | 54 [33–62] → 56 [53–57] | 59 [54–62] → 56 [53–57] | 65
[64–71] → 62 [60–63] |
| Resolve | 1,024 | 173 [104–179] → 135 [126–153] | 177 [173–179] → 135
[126–153] | 182 [181–185] → 148 [138–155] |

The 8-section rows measure the runtime's fixed work, which this change
leaves alone; their differences are within the spread of the rounds. The
same run's long-frame attribution at 1,024 sections puts the send's own
task at 94 ms of script, from 126, and the frame after it at 40 ms, from
46. Most of the gain is the range change: a two-round run of the first
commit's two smaller changes alone read the send's paint at 157 ms
against main's 166.

Firefox, the same command with `--base origin/main` and `--browser
firefox`, which reads only main-thread CPU there, on this branch's first
commit, which also wrote only the focused marker's place (withdrawn
since); load average 4.3 at the start and 4.0 at the end:

| Input | Sections | CPU, main → branch (ms) |
| --- | ---: | --- |
| Send | 256 | 136 [130–139] → 124 [113–127] |
| Send | 1,024 | 299 [297–301] → 269 [267–620] |
| Resolve | 256 | 108 [96–129] → 107 [103–128] |
| Resolve | 1,024 | 180 [178–194] → 173 [173–173] |
| Load | 1,024 | 4,108 [4,064–4,115] → 3,703 [3,639–3,842] |

Firefox gains less from the range change than Chromium; I haven't
profiled why.

## What remains at 1,024 sections

The send's own task is 94 ms of script and the frame after it 40 ms.
Timed in the page at 1,024 sections with this branch, against 8
sections, these still grow with the page:

- **The send's publication**, 15 ms against 1 (`publish`,
framework/application.js). Every publication derives the whole effective
state (`derive`) and shares each subtree against the last reading
(framework/snapshot.js, `share`), whose module header names this as its
design: "a fold rebuilds its whole result on every publication".
Removing it takes folds that produce only what a change moved.
- **Recording the reading place on the focus move the send makes**, 11
ms against 0.5, and 3 ms more in the frame (`recordPlaces`, version.js).
`capturePlace` hit-tests the landing edge right after the send's DOM
writes, which forces the page's style and layout there, and anchors the
line it finds by searching the page's text for that line's copies
(`findQuote`).
- **Collecting the annotation inventory**, 9 ms against 0.6
(`collectEntries`, annotation-inventory.js). It reads every thread,
obligation, contribution and change, names every group, and sorts the
groups by rendered order, building each node's ancestor chain afresh for
every comparison. Caching each node's chain for the sort, and each
unchanged group's entry, would take most of it.
- **The margin's rows-only pass**, 8 ms against 1.3
(`layoutMarginRows`), which still visits every row; #5752 describes
seating only the rows a change can move.
- **Naming every marker on each render**, about 7 ms in the send's paint
(`nameMarkers` 4.5, `readSpokenPositions` 2.7). Each marker's name says
where its row stands among the rows and how far down the page ("3 of
1,024, 40 percent down"), so a row that comes or goes renames every
marker, and every render reads every target's place. Writing the place
into the focused marker's name only, which an earlier commit of this
branch did, renamed the button in the task that moves focus to it, which
a screen reader may announce twice, and took the place from browse mode,
so it is withdrawn. The platform can't say the place for us:
`aria-posinset` isn't allowed on a button, and a list item gives a
button no position.
- **Ranges made per gesture**: `caretAt` (editor-view.js), about 4 per
keystroke, and `rangeOf` (passages.js), 54 per send. Each stays live
until a collection, so moving them onto the shared range would keep DOM
writes from slowing as they pile up.
- **Forced style recalculations**: in profiled runs, about 30 ms of the
send's window at 1,024 sections and about the same at 64, where each
restyles 15 to 230 elements at around 15 µs an element. They don't grow
with the page, so they are fixed cost, but they are the largest single
item left in the send's task, and the same at every size.

## Checked

- `pnpm run test:runtime` (952 pass), `pnpm run check:browser`,
pre-commit on every changed file.
- 41 browser tests by name across margin seats, card opening and
closing, Resolve and the send (the selection #5866 ran): all pass, the
full-pass comparison
`test_a_pass_for_a_change_to_the_rows_seats_them_as_a_whole_pass_would`
among them, which reads every pin's cover through `childIndex`.
- 177 marker, walk and Page Map tests by name, with main's marker names
and the tests that read them restored, in `margin/test_page_map.py`,
`margin/test_entries.py`, `margin/test_clusters.py`,
`margin/test_opening_threads.py`, `margin/test_status.py`,
`navigation/test_threads.py`, `navigation/test_key_scopes.py` and
`test_render_aim.py`: all pass but
`test_a_comment_uses_the_viewport_when_its_target_fills_the_vertical_lane`,
which times out the same way with main's runtime.
- `test_message_age_may_shift_metadata_but_leaves_the_thread_in_place`
(both surfaces) passes; its card case fails with the card's thread draw
left unfollowed.
- 21 tests by name for the card's message ages, heading links, the send
corner and held news, which the moved measurements reach.
- `tests/model/test_owner_index.py`, which checks the owners table.
- The stale-keys sweep on all 37 published examples passed on the
branch's first commit, with file-tree's teardown flake (#5866 describes
it).
- Timers around the functions in the first table, and a count of
`createRange` calls by caller while loading, typing, sending, resolving
and scrolling at 1,024 sections, both temporary and not committed.

## Not checked

- Firefox's paint and shown times: the bench reads only CPU there.
- WebKit.
- The broad suite and the main-push browser tier.
- Inputs other than the send and Resolve, beyond the review's keystroke
reading above.

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.

Served state labels each revision by scanning the whole log, so it grows with revisions × events

1 participant