Skip to content

Open a closed tab without walking every panel's document - #5996

Merged
max-sixty merged 4 commits into
mainfrom
grind/tab-reveal-walk
Nov 15, 2026
Merged

max-sixty merged 4 commits into
mainfrom
grind/tab-reveal-walk

Conversation

@max-sixty

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

Copy link
Copy Markdown
Owner

Opening a closed tab that holds a large document held the main thread for 2 s in Chromium and 8–10 s in Firefox on #4781's page (30 tabs, each an lf-text-document of about 4 MB). It now takes 0.7 s in Chromium and 2 s in Firefox, by click or by key.

Why the keyboard scopes walked every element

LeafElement.updated() told the keyboard register that the whole element had been redrawn (paintKeysWithin(this)). The register then found the scopes inside it by walking every element under it (claimPaintedRoots in keyboard/scopes.js). A tab set draws only its strip, with the page's panels standing beside the strip, so each tab switch walked every panel's document, about 2.9 million elements on #4781's page. The walk was 1.2 s of the 2 s reveal in Chromium.

What changed

  • A draw paints its render root (browser/runtime/leaf-element.js). That root is the element itself, its shadow tree, or the part a widget draws into, such as a tab set's strip. Painting the strip still marks the tab set and every scoped element around it, because the frame climbs from a painted root to its ancestors. A strip redraw no longer reads the panels' keys again; nothing in the panels changed. The same holds for lf-parts, lf-deck, lf-trace, lf-file-editor and lf-file-tree, which also draw into a part beside the page's content.
  • One query finds the widgets under a root (declaredElements(root, holds) in browser/runtime/registry.js). widget-descriptors.js had its own copy for the descriptors it captures, and elementsDeclaring collected querySelectorAll("*") and filtered each element in script. Both now use the one query, which the browser matches natively and which names only the tags the question is about. Naming every declared tag, as the descriptors need, took 0.42 s over Export: an exported page parses every data source before its first draw #4781's 30 panels in Chromium's profile; naming the few that declare x-awaits took 0.05 s. elementsDeclaring returns the same list in the same order.
  • A text document's runs are visible under a fold (packages/default/styles/text-document.css). A fold is a closed tab's hidden="until-found" panel or a shut <details>. Firefox 159 reports a content-visibility: auto box under a fold as drawn, and keeps it drawn when the fold opens until it reports the boxes out of view over the next frames. Chromium reports nothing under a fold. So in Firefox, opening a closed tab made the page read and colour every line of its document: the reading place walked every row (1.85 s), syntax colour tokenized every run (0.55 s), and took each run's colour off again one report at a time (1.5 s). With the runs visible under the fold, the fold still skips them, find-in-page still reaches their text, and Firefox decides afresh which runs are drawn when the fold opens. notes/browser-support.md records the gap and its recheck.

Evidence

Page: #4781's repro, 30 tabs in a side list, each an lf-text-document bound to a markdown source of about 4 MB (95,581 rows in the tab opened). Each round loads the page in a fresh context, waits until it is ready and the main thread has been quiet for 1.5 s, then opens tab 17, which nobody has opened. The reading is the main thread's busy time from the input until it is quiet again, summed from gaps over 20 ms in a chain of zero timeouts. Same Mac, same session, base 7ec8751318 and head measured back to back, load average 9–13.

Input Engine Base Head
Click Playwright Chromium 158.0.8127.18 (build 1254) 1942–2028 ms, median 2004 701–709 ms, median 704
Click, after an earlier switch Chromium not measured 689–710 ms, median 706
ArrowDown from the tab before Chromium 2032–2036 ms 633–756 ms, median 644
Click Playwright Firefox 159.0 (build 1572) 8.2–10.6 s, median 9.2 1.8–2.1 s, median 2.0

The rows were measured with the strip and the native match as they stand in this PR's head. By step, from earlier runs in the same session: with the render-root paint alone, Chromium took 0.78 s (3 rounds); with it and the native match, Firefox took 5.5–8.0 s (median 6.6). The fold rule then took Firefox to 2.0 s. The Firefox costs the fold rule removed are from a Gecko profile of that middle arm. Time to ready was unchanged: Chromium 2.9–3.5 s in both arms, and Firefox 13.9–15.6 s on base against 11.7–14.5 s on head.

For scale, the same DOM with no Leaf (one listing of 95,000 rows in runs of 128, the same counters and content-visibility, panels swapped by hidden) opens in 69 ms in Chromium and 1.1–1.3 s in Firefox. Firefox's 1.1 s is its own restyle and layout of the opened listing.

Keyboard behaviour in an opened tab is unchanged: leaf-dev probe --base on a side tab set holding an lf-options, a <details>, a button and a text document gives the same result in both arms. Tab moves from the selected tab through the panel's controls in the same order, each control names the same aria-keyshortcuts, 2 picks the second option, and ArrowDown opens the next tab with Tab going on into its document.

Tests

  • a draw reads again the keys of what it drew and around it, not of the page's content beside it (tests/runtime/leaf-element.test.mjs): a widget drawing into a strip beside an authored panel redraws, and the strip's and the widget's scopes read their rows again while the panel's does not. It fails on base, where the panel's scope is read again.
  • test_opening_a_closed_tab_draws_only_the_lines_in_view (tests/browser/widgets/test_code_and_documents.py, Chromium and Firefox): from the press that opens a closed tab, it counts in each frame how many runs of the tab's document draw their lines, and asserts no frame draws more than the last. With the fold rule's content-visibility: visible deleted, Firefox drew all 32 runs in the first frame and then 1. It is the browser-support entry's recheck: once Firefox passes without the rule, the rule can go.

Ran: pnpm run test:runtime, pnpm run check:browser, the reviewer's selection of 84 tests (key scopes, command routes, obligations, tabs, text documents, previews and the rest of its list), and in tests/browser the 49 tests that read aria-keyshortcuts or a text document's runs, navigation/test_tabs.py and widgets/test_tabs.py, the tally, deck-keyboard, file-tree, diff-fold and trace-arrow tests, the closed-tab, find and long-document tests of widgets/test_code_and_documents.py in both engines, and the markdown tests of test_render_options.py, test_render_widgets.py and test_code_and_documents.py. All pass except test_a_text_document_update_keeps_a_selection_it_carries_into_another_run[browser], which fails the same way on base: its drawn line numbers don't match after an update.

What remains of the reveal

From profiles of head, Leaf's share of an opened tab is now work that grows with the opened document or with the page:

  • paintReachNow (reach.js) asks tabStops whether the opened listing holds a Tab stop, which walks all 95,000 rows in script: about 220 ms in Chromium's profile and 300 ms in Firefox's. Read from the code, paintReachNow asks it again on every reach paint while the document shows.
  • Known cost: the tab strip scans every panel on each switch. To keep a side list's mark room for a panel that asks something, the strip asks every panel's content whether it holds an x-awaits widget (elementsDeclaring, now a native match of only those tags), on both of a switch's renders, so the cost grows with what the panels hold: about 130 ms of each switch in Chromium on Export: an exported page parses every data source before its first draw #4781's page (706 ms against 575 ms in a trial that kept each panel's answer). This PR adds no package export for it. The option Max can take later: a package export, authorsObligation(region), that answers from the widget descriptors the runtime captures before upgrade (readApplication().document.descriptors lists each x-awaits widget with its declared ancestors' ids), which costs a lookup per panel and needs no invalidation, since a revision replaces the descriptors and the strip redraws through its signals. It was built and tested on this branch (9c1d7f4) and removed to keep the external interface small.
  • The reading place (capturePlace → pageBlocks) walks the opened document's rows: about 115 ms in Chromium and 170 ms in Firefox.
  • A closed tab keeps the colour of the runs last shown until it opens again, as on base in Chromium. Each tab a user opens adds its runs' ranges to the shared colour highlights, which a later run's uncolouring rebuilds. Read from the code, that rebuild grows with the tabs opened in a session; not measured.

Calls Max may reverse

  • The fold rule covers shut <details> as well as hidden="until-found", since Firefox shows the same gap under both.
  • The fold now also stops a shut <details> from counting line numbers, which the line-number rule did only under hidden="until-found".

Not verified

  • WebKit and Safari: not run.
  • Release Firefox, as opposed to Playwright's build.

Part of #5194

Opening a closed tab on #4781's page (30 tabs, each a text document of
about 4 MB) held the main thread for 2 s in Chromium and 8-10 s in
Firefox. It now takes 0.7 s and 2 s, by click or by key.

LeafElement.updated() told the keyboard register that the whole element
had been redrawn, and the register found the scopes inside it by walking
every element under it. A tab set draws only its strip, beside the page's
panels, so each switch walked every panel's document: about 2.9 million
elements and 1.2 s in Chromium. A draw now paints its render root (the
element, its shadow tree, or the part it draws into), and the frame's
climb from that root still marks the element and every scope around it.

elementsDeclaring, which the strip calls for every panel on each render,
asks the browser for the declaring tags rather than filtering every
element in script: about 220 ms to 100 ms of a switch in Chromium.

Firefox 159 reports a content-visibility: auto box under a fold (a
hidden="until-found" panel, a shut details) as drawn, and keeps it drawn
as the fold opens until it reports the boxes out of view over the next
frames. So opening a tab made the page read and colour every line of its
document. A text document's runs are now visible under a fold, which the
fold still skips and find-in-page still searches, so Firefox decides
afresh which runs are drawn as the fold opens: 6.6 s to 2.0 s.
notes/browser-support.md records the gap and its recheck.

Measured on this Mac, base 7ec8751 and head back to back, main-thread
busy time from the click until quiet, 4 rounds each:
Chromium 158 1942-2028 ms -> 701-709 ms; Firefox 159 8.2-10.6 s ->
1.8-2.1 s. Keyboard behaviour in an opened tab (Tab order through its
controls, their aria-keyshortcuts, a digit picking an option) is the same
in both arms of leaf-dev probe --base.

The new runtime test holds that a draw re-reads the keys of what it drew
and around it, not of the page's content beside its render root; it
fails on base.

Part of #5194
…a test

A side list's strip asked every panel's content whether it authors an
obligation on every render, twice a switch: on #4781's page that scanned
every panel's document. The strip now keeps each panel's answer until a
change under the panel, such as a revision patching it, takes it back
(a MutationObserver over the panel's children). In Chromium on that
page, the first switch after load went from 704 to 645 ms and a later
switch from 706 to 575 ms.

declaredElements(root, holds) in registry.js is now the one query for
the widgets under a root. widget-descriptors.js and elementsDeclaring
both use it. It names only the tags the question is about: naming every
declared tag took 0.42 s over the 30 panels, against 0.05 s for the tags
that declare x-awaits.

test_opening_a_closed_tab_draws_only_the_lines_in_view samples, in each
frame from the press, how many runs of a closed tab's document draw
their lines, and asserts none draws more than the last. With the fold
rule's content-visibility: visible deleted, Firefox drew all 32 runs in
the first frame and then 1. The browser-support entry now names the
test as its recheck.

The comment on a text document's `shown` said a fold takes the colour
off as it closes. It doesn't: the listing keeps its box under the fold,
so the fold keeps the colour of the runs last shown until it opens.
text-document.css defines a fold once for both of its rules, so a shut
details now counts no line numbers either, and its comment points at
the note for the figures rather than repeating them.
…iptors

The side list's strip kept each panel's answer to "does this panel author
an obligation" in a cache that a MutationObserver over the panel cleared.
The cache re-derived a fact the runtime already publishes, and it heard
runtime writes too: each text document's own drawing cleared its panel's
answer, so the first switch still scanned every panel.

authorsObligation(region) in obligations/place.js answers it from the
widget descriptors captured before the page's widgets upgrade
(readApplication().document.descriptors). Each descriptor of a widget
that declares x-awaits names its declared ancestors, and an lf-tab
carries an id. The set of owning ids is built once per descriptors map,
so a revision, which replaces the map, redraws the strip through its
signals and nothing needs invalidating. A quoted widget asks nothing,
as the obligation inventory already holds.

No existing package reader answers this: obligationsIn reads the
admitted inventory, which is empty until the first reading, and the
row keeps the mark's room from its first paint. So widget-api.js exports
authorsObligation, and module-reading.md names it beside obligationsIn.
The strip's cache, observer and reset on reconnect are gone.

@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.

authorsObligation(region) answers only for a region that is itself a declared widget with an id, but the shipped reference offers it for any region. The owner set is built from each descriptor's ancestors, and declaredAncestors in widget-descriptors.js keeps only parents with an id whose tag is in runtime.registry. A package module marking obligations "per card or section", as the paragraph above it in module-reading.md invites, passes a plain <section id> and gets false even when the section holds an lf-options with x-awaits. Its row then reads no obligations at all, because lf-tabs gates obligationsIn on this answer. The new test in obligation-place.test.mjs doesn't catch it: it hand-writes div ids into ancestors, which real descriptors never contain. One fix is to scope the contract to widget regions, as the place.js header already does ("the widget region"). The other is to answer for any region by resolving each asking descriptor's element and checking under(element, region), which costs only the asking widgets per call.

Also, the description still says the strip keeps each panel's answer with a MutationObserver, under both What changed and Calls Max may reverse. The head reads the answer from the widget descriptors and adds authorsObligation to widget-api.js, which the description doesn't mention. Since the description becomes main's commit message, it needs updating before this lands.

Leaf's external interface stays as small as it can, so the strip asks
each panel's content again with elementsDeclaring, as on main, rather
than through a new authorsObligation export. The export, its reference
line and its test are gone, and the strip keeps no cache and no
observer.

The scan now matches only the tags that declare x-awaits natively, but
it still runs for every panel on both of a switch's renders, and its
cost grows with what the panels hold: about 130 ms of each switch in
Chromium on #4781's page (706 ms against 575 ms with each panel's answer
kept). The descriptor-based export is the option to take if that cost
matters.
@max-sixty
max-sixty marked this pull request as ready for review November 15, 2026 02:18
@max-sixty
max-sixty merged commit b2dc53e into main Nov 15, 2026
6 checks passed
@max-sixty
max-sixty deleted the grind/tab-reveal-walk branch November 15, 2026 02:33
max-sixty added a commit that referenced this pull request Nov 15, 2026
…whole-page walks crash its renderer (#6031)

A review of #5996 found that opening all 30 tabs of a page of 30 text
documents
of 4 MB, one after another, crashed Chromium's renderer. Each opened tab
also left
about 57 MB more in the renderer for the life of the page. The two have
separate
causes.

## The crash is Chromium's, reached through the harness

Every `page.query_selector` walks all 2.96 million elements of that page
in
Playwright's injected CSS engine. With no tab opened at all, the
renderer died
with `Target crashed` (the browser logged `V8 process OOM
(ExternalEntityTable::AllocateEntry)`) on the 22nd query. A page of 2.96
million
plain rows built by a script, with no Leaf, died the same way: on the
23rd to
32nd Playwright query across runs, or on the 24th to 26th run of a page
script's
`querySelectorAll("*")` loop, installed Chrome 157 included.
With a full GC forced after each walk, 100 script walks passed, and
clicking the
30 tabs by position opened them all without a crash. Playwright's
Firefox 159
survived both journeys.

`notes/browser-support.md` records the gap, how a harness on a page this
size
finds its controls instead, and the recheck; `dev/leaf_dev/browser.py`'s
header
points there.

## The growth is partly Leaf's

Each tab open runs reach.js's `holdsOwnStop` over the opened document's
listing,
and `tabStops` walked every row through `children`. Chromium keeps the
HTMLCollection that `children` returns on its element for the element's
life: 35
MB of Blink's heap for the 98,000 rows of a 4 MB document.
`renderedElements` now
steps from `firstElementChild` through `nextElementSibling` and pushes
each element
into one array, so it allocates no collection, and Tab stops over a
96,000-row
listing take 36 to 39 ms in Firefox (54 to 60 through a generator for
each
element) and 21 to 23 ms in Chromium. browser/AGENTS.md ("One answer for
each
question") now states the sibling-stepping rule for every walk.

Measured in Playwright's Chromium headless shell 158.0.8127.18 on that
page,
clicking tabs by position, with a full GC before each reading:

- `tabStops` over one closed document's listing: Blink's heap grew 34.8
MB
  before, 0.2 MB after (memory-infra dump).
- Renderer private footprint over 30 opened tabs: 1357 to 3012 MB before
(57 MB a
tab), 1361 to 1947 MB after (20 MB a tab). Median main-thread busy time
per tab
  open: 699 ms before, 562 ms after, at similar machine load.

The stops are unchanged: compared with main's walk from every root a
caller hands
`tabStops`, they matched on 57 pages in Chromium (the corpus, the site
and a page
built to hold the walk's edge cases) and 12 in Firefox.

## What remains, about 20 MB a tab

Over six tab opens the memory dump grows V8's heap 15 MB a tab, malloc
11 MB and
Blink's heap 3.4 MB. About 10 MB of V8's is passages.js's reading of the
opened
document, which its piece cache keeps until the document changes
(sampled heap
profile); the rest of V8's and all of Blink's are unattributed. A script
that
walks one closed document's 98,000 rows grows malloc by 8 to 13 MB
however it
walks them, keeping nothing, so the malloc growth follows script
touching the rows;
what holds it is not measured. This change leaves all of it.

Other rendered-tree walks still read `children` or `childNodes`:
`renderedChildElements` in shadow-tree.js (used by keyboard/scopes.js),
`readingChildren` in passages.js, `renderedChildren` in shadow.js, and
`matching` in focus.js. They break the new browser/AGENTS.md row: a walk
that
reads `children` leaves the collection measured above on each element it
reaches,
and Chromium keeps a `childNodes` list on its node too (not measured
here). One
shared walk primitive would carry the rule for all of them. The
grind/tab-open-walks change is reworking reach.js and focus.js walks in
parallel;
whichever lands second merges main.

## Checks

Ran `pnpm run test:runtime`, `pnpm run check:browser`, and the browser
tests over
Tab stops, lent scroll stops, a scroll box in a widget's shadow tree, a
hidden
scroller once shown, and a thread card's place in the Tab order (27
cases). No
test asserts the memory, which is a reading rather than a pass or fail
(tests/AGENTS.md, "Results do not depend on execution speed").
max-sixty added a commit that referenced this pull request Nov 15, 2026
… costs (#6033)

`leaf-dev bench` now generates #4781's page, a side list of tabs each
holding a large text document bound to its own source, and measures the
inputs that found this week's costs on it by hand. The daily `bench` job
runs it, so Tend's review sees these costs grow with the page.

## The page

`documents` holds the shown document, one more in a closed tab for each
eight of `n`, and one more again: 3 documents on the BASE page, so
`reveal` has two closed tabs to open there, then 10, 18 and 34 at the
daily sizes 64, 128 and 256, about #4781's 30 tabs at the largest. Each
source is 2,048 lines of Python (16 of a listing's 128-row runs), the
same at every size. So a cost that grows with the documents a page holds
grows with `n` and shows in the exponent. A cost that grows with the
length of the one document opened stays level and shows only beside
another arm (`--base`).

The largest page holds 69,000 rows where #4781's held 2.9 million. That
is enough for a walk of every panel on each tab switch (#5996) to fit an
exponent in Chromium, and it keeps the daily run short (below). The text
is Python rather than #4781's Markdown, because Python's syntax colour
paints most words, and the colour a closed tab keeps is what `browse`
reads.

The sources are set through the fixture's data companion
(`page.data.json`, which `prepare_page` sets with `leaf data set`), so
each arm builds the page with its own Leaf. `Collection` gains `data`
for it, which `server_paths.build` also sets in process.

## The inputs

| Input | What it does | Fix it watches |
|---|---|---|
| `data` | the agent's `leaf data set` changes the first line of the
shown document's source; `painted` is the frame that draws the new line
| #5992 |
| `reveal` | a click opens the second tab, then the third, neither
opened before | #5996 |
| `search` | the first `/`, which opens page search | #5999 |
| `matches` | type `value`, which every line holds, into page search,
until the count is final | none yet |
| `browse` | put away page search, open every closed tab, then turn the
wheel a run's height down and up the last one's listing, 16 steps |
closed tabs keep their colour ranges (still on main; #6035) |

They run in that order, so `search` and `matches` read the page before
`browse` has opened every tab, whose kept colour every later input would
meet. They need the page's own ids (`#docs #d0[source]`) and find the
other tabs and documents by them. Each finds what it acts on through
`page.evaluate`, never a locator, since a Playwright locator walks every
element of the page for each query. `holds`, which decides whether a
page has what an input needs, now asks the document with `querySelector`
for the same reason. `painted.js` gains three facts: `shows` (an element
is drawn and holds some words), `searching` (page search's field holds
the caret) and `searched` (its count is final). `BenchPage.written`
takes the file a command writes, here the source's file rather than the
log.

## What the marks hold

`held` now covers the requests and bytes any input causes, the size of
the answer to each of its POSTs (`<input> event KB`) included, besides
the CPU of an input the browser answers alone: root `AGENTS.md`,
"Responsiveness", holds an input's work to what it changes. Without it,
#5992's cost, every source sent to the tab after a write to one, fits
exponent 1.08 with no mark, since an agent's write may grow with the
page. An input whose result is the whole page is exempt, through a new
`Input.whole`: `revision` and `stamp`, which carry the new document, and
`matches`. A held count's noise floor is a quarter of its own reading on
the BASE page and at least one, where every count's was a fixed 10 KB or
one request.

Re-judged under main's rules and this branch's, the nine daily reports
of 11-06 to 11-14 differ in one reading: `asks` `comment event KB`, 4.9,
8.1, 11.9 and 19.6 KB across the sizes in the six reports of 11-06 to
11-11, now marked at 1.08. Those reports showed it with no mark; it
reads level from 11-12 on. On this page's runs the only differences are
`matches`, which `whole` lets grow, and `data KB` with #5992 reverted.

## Evidence

All on this Mac (M5 Max, 18 cores, load average 3–21 from other
sessions), `uv run leaf-dev bench documents --sizes 64,128,256 …`.

Current main, Chromium, the daily job's command (`--rounds 2 --input
data --input reveal --input search --input matches --input browse`), 266
s:

```
                            8         64        128        256   exponent
elements               6435.0    20988.0    37620.0    70884.0         - 
load cpu ms             200.0      242.8      299.5      510.5      1.43 
data cpu ms              23.1       21.3       25.5       30.7         - 
reveal cpu ms            46.0       42.0       43.9       47.1         - 
search cpu ms            18.3       17.3       18.4       19.7         - 
matches cpu ms          290.5      718.7     1271.1     2328.5      1.13 
browse cpu ms            15.4       19.5       25.4       28.0      0.81!
data painted ms         133.5       99.9       68.6       99.5         - 
data requests             1.0        1.0        1.0        1.0         - 
data KB                  82.6       82.6       82.6       82.6         - 
reveal painted ms        37.2       33.5       34.8       36.1         - 
reveal requests           0.0        0.0        0.0        0.0         - 
reveal KB                 0.0        0.0        0.0        0.0         - 
```

Current main, Firefox, the first commit's page and input order with
`--browser firefox`:

```
                         8         64        128        256   exponent
elements            4351.0    18904.0    35536.0    68800.0         - 
load cpu ms          344.8      521.9      557.5      917.8      0.85 
data cpu ms           40.6       56.2       43.4       52.0         - 
reveal cpu ms         73.5       78.3       65.0       71.7         - 
browse cpu ms         43.9       51.4       54.6       68.1         - 
search cpu ms         37.5       61.3       40.8       48.5     -0.56 
matches cpu ms        98.3      115.0      201.3      287.1      0.87 
```

What main shows, with a V8 profile of each growing input from
`--profile`, which lists the functions whose time grew past the input's
limit from the smallest page to the largest:

- **`browse`, 0.81 `!` in Chromium**, and 0.96 and 1.04 in two runs of
`browse` alone. A closed tab keeps the colour ranges of the runs it last
showed (896 a tab here), and each run a step colours rebuilds highlights
holding every opened tab's ranges. The profile names `unpaint` and
`follow` (`lf-text-document.js`), 3 to 40–45 ms. This is the cost
#5996's body left unmeasured, which #6035 removes: with its change,
review read `browse` level. Firefox grows too (44 to 68 ms), but under
the noise floor in one of two runs.
- **`load cpu`**, 1.43 here and between 1.02 and 1.71 across seven runs
on main, above 1.5 in two. The profile names an anonymous function at
`addressable-names.js:187`, `headingsIn`'s query of every heading under
an element, growing from 1 to 126 ms (exponent 1.54). Read from the
code, not confirmed: each document's first draw may start a new page
reading, whose heading queries walk the whole page again.
- `matches` grows at 1.13 in Chromium and 0.87 in Firefox, within its
limit as a `whole` input. Chromium's 2.3 s at 34 documents against
Firefox's 0.29 s is #5999's note that a match's visibility read styles
each run it lies in.
- `search`, `data` and `reveal` stay level in both engines. In the first
commit `search` ran after `browse` and grew with the tabs `browse` left
coloured (0.79 `!`); run before it, it reads 17 to 20 ms.

Each fix reverted in the working tree (`git revert --no-commit`), with
`--base HEAD` for main beside it. The #5992 and #5999 rows are from the
first commit's page, which held one document fewer:

- **#5992**, Chromium: `data KB` goes 164, 736, 1,398, 2,722 KB with the
fix reverted, exponent 1.08 `!`; main reads 82.6 KB at every size.
- **#5996**, Chromium, two rounds, this head's page and `reveal`:
`reveal cpu` goes 48, 50, 63, 84 ms with the fix reverted, exponent 1.25
`!`; main reads 48, 43, 51, 49 ms. On the first commit's page, whose
`reveal` reopened the first tab for its second sample, the revert read
1.77 `!`. In Firefox the revert raises `reveal cpu` to 210–290 ms at
every size from 60–90 ms, level rather than growing, since the Firefox
half of #5996 (the fold rule) scaled with the opened document's length,
which this page holds fixed.
- **#5999**, Firefox, two rounds: `search cpu` goes 45, 83, 92, 165 ms
with the fix reverted, exponent 0.82 `!`; main reads 26, 30, 28, 24 ms.

## CI time

The daily job measures `documents` in a step of its own after the
default pages, with only these five inputs, since the `document` page's
text already grows under every other input. The step runs after a failed
one, and the readings to chart are collected once both have run, so one
page's failure keeps the other's chart. Its table goes to
`documents.txt` in the `bench-report` artifact, which Tend's benchmark
review (`.claude/skills/running-tend/SKILL.md`) now names beside the
browser and server tables. The command took 266–284 s here. To scale it
to the runner: the `rows` page with the daily job's arguments took 397 s
here and 7.5 minutes on the runner on 2026-11-14, a ratio of 1.13, while
the runner's load CPU is 2.2 times this Mac's. So the job should grow by
between about 5 and 10.5 minutes, nearer the first, since most of a run
is waiting rather than CPU.

## Not verified

- The runner's time for the new step, which the first daily run's step
timings will show.
- Firefox on CI. The Firefox clock (`ContentClock`) reads macOS's thread
times, so the daily job reads Chromium only, and a Firefox-only cost
such as #5999's shows only in a local run.
- WebKit.

No `dev/tests` smoke test: the bench has none, and each input ran here
in both engines at four sizes.

## Calls Max may reverse

- `held` covering requests and bytes, with `Input.whole` to exempt a
revision and a search.
- The CI step's input list, rather than every input on this page.
- Python content, 2,048 lines a document and `2 + n/8` documents.
- `browse`'s 16 steps, which over two runs fitted 0.96 and 1.04 where
three runs of 8 fitted 0.49 to 0.75.

Part of #5194.
max-sixty added a commit that referenced this pull request Nov 15, 2026
…eads none of its document's lines (#6041)

Opening a closed tab on #4781's page (30 tabs, each an
`lf-text-document` of about 4 MB; the tab opened holds 95,581 rows) no
longer asks any of the opened document's lines whether they take a Tab
stop. Opening the tab takes a median of 447 ms of main-thread time in
Chromium (548 ms on main) and 1.34 s in Firefox (1.53 s on main).

## Why the lines were asked

A text document's listing is a code block, and its copy control claims
the block's Tab stop (`code-copy.js`, `claimReachStop`), which reach.js
then leaves alone. A block in a closed tab was deferred until drawn, and
the claim waited with it. Until the claim came, reach.js treated the
listing as any box that overflows: each reach paint asked whether any
element inside it takes a Tab stop, a walk of every line, and the tab's
opening ran five such paints before the block was drawn and claimed its
stop. On main that is five walks of 95,581 lines, about 17 ms each in
Chromium.

## What changed

A document code block claims its stop in one place, as code-copy.js
first sees it, drawn or not (`prepareDocumentBlocks`), and the module
header states the rule. reach.js never asks a claimed box what it holds,
so opening the tab walks none of the lines.

What a user can see differs in two places:

- A closed tab's code blocks carry `tabindex="0"` and the lent mark
(`data-lf-lent-reach-stop`) while hidden. Tab can't reach anything under
a fold, so the Tab order there is unchanged.
- A code block the browser skips off screen (`content-visibility: auto`)
now takes from the start the stop it takes on main once drawn. On main,
Tab from a button 3,000 px above two such blocks went past both to the
next control, since they had no stop until the browser drew them; now
Tab stops on each. The copy control is prepared as focus lands, and the
block names Enter in `aria-keyshortcuts` in the same gesture (`leaf-dev
probe --base`, Chromium and Firefox). The block's own control stays out
of the Tab sequence, as before.

## Evidence

Page: #4781's repro, built with #5996's `make_page.py` (30 tabs × 4 MB
markdown). Each round loads the page in a fresh browser context, waits
until it is ready and quiet for 1.5 s, then opens tab 17, which nobody
has opened. Busy time is the main thread's from the press until it is
quiet again, summed from gaps over 20 ms in a chain of zero timeouts;
longest is the longest such gap. Arms are interleaved round by round in
one session on this Mac, base `0144d467a9` (main) against this branch,
swapping only `code-copy.js`.

| Input | Engine | main | This change |
| --- | --- | --- | --- |
| Click on tab 17, busy | Playwright Chromium 158 (headless shell 1254)
| 533–580 ms, median 548 | 436–461 ms, median 447 |
| Click on tab 17, longest | Chromium | 536–558 ms, median 549 | 440–465
ms, median 450 |
| Click on tab 17, busy | Playwright Firefox 159 (build 1572) |
1.45–1.61 s, median 1.53 | 1.25–1.42 s, median 1.34 |
| Click on tab 17, longest | Firefox | 1.34–1.43 s, median 1.35 |
1.21–1.34 s, median 1.27 |

Chromium had 5 rounds an arm and Firefox 4, with the 1-minute load
average between 3 and 8. The first frame showing the panel came at a
median of 182 ms against 202 ms in Chromium, and at about 1.07 s in both
arms in Firefox.

Tab order in the opened tab is unchanged: `leaf-dev probe --base` on a
side tab set holding an `lf-options` ask, a `<details>`, a button, two
scrollers and a written text document gives the same Tab route into and
through the document's tab in both arms. The only differences are the
closed tab's listing carrying its stop and lent mark before it opens,
and whether the On Me margin marker had been placed when Tab reached the
page's end, which varies between runs of main too.

## Tests

-
`test_opening_a_closed_tab_reads_none_of_its_document_s_lines_for_tab_stops`
(`tests/browser/widgets/test_code_and_documents.py`, Chromium and
Firefox): opening a closed tab holding a 2,000-line document asks none
of its lines for `tabindex`, and Tab goes from the tab to its panel and
then to the listing. On main the opening asked 10,005 times in both
engines, five walks of 2,001 rows.

Ran `pnpm run test:runtime`, `pnpm run check:browser`, and in the
browser suites the code-copy tests of `test_render_anchors.py`,
`test_render_code_reading.py` and `margin/test_scrollers.py`, the lent
and claimed stop tests of `test_render_aim.py`, Tab stops in
`controls/test_focus.py`, shadow trees and live samples, the command
reference's stops, a box that starts scrolling late,
`navigation/test_tabs.py` and the text-document tests of
`test_code_and_documents.py`: 82 passed.
`test_a_text_document_update_keeps_a_selection_it_carries_into_another_run[browser]`
fails, as it does on main.

## Calls Max may reverse

- An off-screen code block in `content-visibility: auto` content is a
Tab stop before the browser draws it, as described above, where main's
Tab order skipped it until it was drawn.

## Not verified

- WebKit and Safari.
- Release Firefox, as opposed to Playwright's build.

## What #5194 still has here

The walk this removes for code blocks is still there for every other
scroller. `paintReachNow` in `reach.js` asks `holdsOwnStop` (`tabStops`)
of each drawn, unclaimed candidate on every reach paint, and that reads
the box's whole subtree, so a wide table or an authored `lf-scroll` box
holding thousands of rows is read in full on each paint: a 5,000-row
table in an `lf-scroll` box inside a closed tab had its rows and cells
asked for `tabindex` 120,000 times as the tab opened (8 walks of 15,000
elements). The fix belongs in `reach.js`: keep each candidate's answer
and ask again only when its subtree changes. This change doesn't make
that harder.

Part of #5194
max-sixty added a commit that referenced this pull request Nov 15, 2026
…text changes (#6035)

Two costs in `lf-text-document` grew with something other than what the
user did: syntax colour grew with how many tabs the user had opened in
the session, and a rewrite of a document's source grew with the
document's length rather than with the lines that changed.

## Colour: a closed tab gives up its document's colour

Every document on a page paints its syntax colour into the same few
highlights, one per token colour. A document coloured the runs of rows
in view and uncoloured them as they left it, but a closed tab never
uncoloured. Since #5996 a fold (a closed tab's `hidden="until-found"`
panel or a shut `<details>`) makes its runs `content-visibility:
visible`, so they report nothing as it closes, and Chromium reported
nothing under a fold before that. So each tab the user opened left its
runs' ranges in the shared highlights for the rest of the session. Each
uncolouring rebuilds every highlight it touches from the ranges it keeps
(a Firefox delete walks the highlight's list), so it cost every range on
the page, and Chromium's hit tests grew with them too.

A document now drops its colour when a fold closes over its listing. An
`IntersectionObserver` rooted at the listing's `pre` watches its rows.
They stop meeting the `pre` when a fold closes or the document leaves
the page, and page scrolling never moves them out of it. Both engines
report the close after the frame, checked with a fold of each kind.

A reopened tab still shows its colour in its first frame. When the fold
closes, the document observes its listing's size afresh: a size observer
reports only a change from the size it last reported, and the fold opens
at that size, so without the fresh observation nothing reports the
listing's box before the frame. With it, the size observer reports the
box before the frame that shows the reopened tab paints. The document
then reads which runs its view shows off their boxes (`runsInView`) and
colours them in that frame. Reading them rather than keeping the runs
shown before the fold also covers a rewrite made while the tab was
closed: Firefox draws the reopened rows in the first frame, and with the
kept runs, 22 of 22 rows in view stood uncoloured there.

The highlights now hold the ranges of the documents on screen, whatever
the session has opened, so an uncolouring's rebuild costs what the
window shows. The price is paid on reopening: a reopened document
colours its runs in view again before the frame that shows it, about 8
ms in Chromium (72 → 80 ms from the press to the reopening frame, in the
review's measurement).

## Rewrite: a new text changes only the rows of the lines it changes

`reconcileRuns` built a fresh listing of every line, moved every kept
row into the run the new text put it in (`setChildLists`), and
reconciled every run's rows (`setRenderedChildren`), so a one-line
rewrite of 50,000 lines did the work of 50,000. Runs held a fixed 128
rows, so lines added near the top moved that many rows across each later
run's edge, and the runtime heard each move.

`changes` walks the old and new lines (with their endings) and returns
each change as the old rows it replaces and the new rows that replace
them, capped as before (`MAX_EDIT_LENGTH`). `rewrite` applies them last
to first:

- A row a change replaces keeps its node and takes the new words as its
text's least edit (`setRenderedChildren` on the row), so a selection in
the words it keeps stays.
- Rows past the new ones leave. New rows past the old ones stand in the
run of the row before them.
- Runs no longer hold a fixed 128 rows; every run a rewrite didn't touch
keeps its rows (`regroup`). A touched run folds onto the end of the run
before it where the two fit in 128 rows or where it fell under 32, and a
run filled to 256 splits into runs of 128. Without folding, 40 cycles of
adding 200 lines and removing them took a 3,002-line text from 24 runs
to 75 (the reviewer's measurement); with it, 40 cycles took it to 32 and
120 cycles to 40, every run at least 32 rows.
- Folds and splits move rows through `setChildLists`, which holds a
selection in a moved row, and a moved row lands only at a run's end or
in a run the split has just made. Chromium drops the line number of a
row `moveBefore` puts at the head of a run it skips, and numbers the
rows after it from 1: that is what #5916's test hit on main.
`notes/browser-support.md` now records the gap with #5916's reproduction
and its recheck.
- When the line count changes, each later run counts its numbers on from
its new first line, which is counted from the rows before it, not read
back from the page. The number now sits on the run's first row
(`--lf-lines-before`, which the first-row counter rule already read)
rather than on the run, so renumbering restyles one row per run. Held by
the run, it reached every row, and Firefox, which styles the rows of
runs it skips, restyled all of them: renumbering every run of the
50,000-line document took 190–240 ms to the end of the next frame in
Firefox and 13–14 ms in Chromium, against 34–35 ms and 5 ms on the first
rows.
- Only the runs whose rows changed are sized again (`sizeRuns`).
- The line each run starts on (`starts`) is counted where runs are made
or regrouped and kept beside them, so syntax colour reads a run's lines
without counting rows. Counted from the rows before each run instead,
colouring the way to the end of 50,000 lines counted every row before
each run it tokenized: `walk` took 52 ms in Chromium's profile, 45 of
them counting, against 11 ms on base and 9 ms now (the tokenizer's own
330–340 ms is the same in all three).
- A grammar state stands for its run while the run starts on the line it
did and that line comes before the change. A rewrite drops the states
and paint from the first run that holds the change, before it or after
it, or that starts elsewhere; kept by place, a split's second part was
coloured from the state of the run that had stood second, and a string
opened above the split read as code below it.

Past the cap (`refill`), each run reconciles its rows with as many of
the new text's rows at its place, as main did, and new runs of 128 hold
the rows the new text adds. The reconciliation finds a row's words
wherever they stand in its run, so a selection on lines a rewrite
reindented and moved by a line stays on its words. An earlier version of
this branch patched every row with the line at its place, and the
selection moved to another line's words. The reconciliation also gives
each row the new row's attributes, so each run's first new row carries
its number in; patched bare, a rewrite past the cap that kept every
run's first line (every line reindented, none added) left those runs
unnumbered, and their rows counted from 1.

## Evidence

Base `28c1812ddb` and head were measured back to back in one session on
one Mac (load average 22–28, so single readings are noisy), in
Playwright Chromium 158.0.8127.18 (build 1254) and Firefox 159.0 (build
1572). The rewrite and colour paths measured there are unchanged by the
review fixes, which touch only runs a rewrite changes (folding),
rewrites past the cap, and reopening.

### Colour after opening tabs

Page: 30 tabs in a side list, each an `lf-text-document` with
`language="python"` bound to its own source of 20,000 lines of CPython
stdlib modules. Each run serves a fresh copy, opens the last N tabs in
turn by click (700 ms quiet after each), then scrolls the last opened
document's listing 3000 px eight times. "Highlight methods" is the time
spent inside the highlights' `add`, `delete`, `clear` and iterator over
the eight steps, from wrapped methods. "Task" is the median of
Chromium's `TaskDuration` per step.

| | Base, 1 tab | Base, 30 tabs | Head, 1 tab | Head, 30 tabs |
| --- | --- | --- | --- | --- |
| Colour ranges after the opens | 746 | 10,449 | 380 | 380 |
| Highlight methods, Chromium | 2.0 ms | 17.2 ms | 1.5 ms | 1.6 ms |
| Highlight methods, Firefox | 27 ms | 198 ms | 30 ms | 25 ms |
| Task per step, Chromium | 36 ms | 44 ms | 33 ms | 40 ms |

Chromium CPU profiles of the eight steps, from an earlier run in the
same session: on base, `unpaint` took 1.6 ms after one tab and 33.9 ms
after 30, and `elementsFromPoint` 6.4 ms and 25.5 ms; on head after 30
tabs, `unpaint` stayed under 1 ms and `elementsFromPoint` took 6.2 ms.
Firefox's per-step busy time swung by hundreds of milliseconds between
steps on both arms under this load, so it isn't reported.

### Rewrites of a 50,000-line source

Page: one `lf-text-document` with `language="python"` bound to 50,000
lines of Python. Each rewrite is a `leaf data set`, and the reading is
main-thread busy time from the write until 700 ms quiet (gaps over 20 ms
in a chain of zero timeouts). "Plain" leaves the listing at its top with
no selection. "Selected" scrolls it to line 25,000 and selects a word
there. Every rewrite on both arms left the listing's text equal to the
source and the selection on its word.

| Rewrite | Chromium plain | Firefox plain | Chromium selected |
| --- | --- | --- | --- |
| Edit line 100 | 320 → 112 ms | 1,673 → 334 ms | 400 → 162 ms |
| Edit line 49,000 | 259 → 53 ms | 484 → 149 ms | 331 → 132 ms |
| Add a line at 200 | 267 → 51 ms | 1,325 → 317 ms | 385 → 148 ms |
| Remove a line at 300 | 284 → 59 ms | 1,568 → 150 ms | 368 → 154 ms |
| Add 200 lines at 500 | 2,548 → 72 ms | 16,896 → 605 ms | 2,502 → 151
ms |

With a selection, base also lost the reader's place on the 200-line
addition (the row in the middle of the listing changed), where head kept
it. A rewrite past the cap (every line of the 50,000 edited) costs what
it did on main: 1,503 and 1,582 ms in Chromium against 1,501 and 1,576
on base, and 4,248 and 4,169 ms in Firefox against 4,222 and 4,242,
measured alone at load 6–7.

In Chromium's profile of the plain one-line edit, `show` took 8 ms on
head (`changes` 3, splitting the source 3, `rewrite` 1), against 212 ms
on base (`reconcileRuns` 193). What remains of the busy time is outside
the widget's draw; see "What this leaves".

### Colour stays right

Stills of the first document after load, after a 20,000 px scroll, with
the second tab open, after switching back, after a rewrite that adds a
line in view and edits another, and after a page revision are
pixel-identical to base in both engines. While the second tab is open,
the first document holds 0 colour ranges on head, against 962 (Chromium)
and 714 (Firefox) on base. The JS heap after opening 30 tabs in Chromium
read 212 MB on both arms in the back-to-back run, so the closed tabs'
ranges are not a measurable share of memory here.

A tab whose document was scrolled to line 140 and then rewritten while
closed (200 lines added at line 101) reopened with all 22 rows in view
coloured in its first frame in Firefox, as on base; Chromium draws no
rows in the first two frames after the reopen on either arm, and colours
them when it does.

## Tests

-
`test_a_closed_tab_gives_up_its_document_s_colour_and_wears_it_as_it_opens`
(Chromium and Firefox): a closed tab's document holds no colour ranges,
and when it reopens, every row in view holds colour in the frame that
shows it and the three after, both plainly and after a rewrite made
while it was closed. It times out on base in both engines, where the
closed tab keeps its colour. In Firefox it fails without the fresh size
observation, and with the runs kept from before the fold (22 of 22 rows
uncoloured in the reopening frame).
-
`test_a_text_document_rewrite_changes_only_the_rows_of_the_lines_it_changes`
(Chromium): a mutation observer on the listing sees one edited row for
an edited line, one row added to one run for a line added at the head of
a skipped run, one removed row for a removed line, and 100 rows added to
one run for 100 lines. Then a run splits, the text stays the source, and
the drawn numbers count every row at the added line and at the end.
Last, a rewrite past the cap that adds a line at the top and indents
every line keeps a selected line's words selected, and one that only
reindents every line leaves the drawn numbers counting every row. It
fails on base at the added line, where every later run gained and lost a
row; on this branch's first version at the selection past the cap ("line
700" against "line 701"); and on its second at the numbers after the
reindent.
- `test_a_split_run_colours_from_its_own_grammar_state` (Chromium): a
triple-quoted string spans the point where a later rewrite splits its
run, and an edit below the split leaves its rows coloured as a fresh
load colours them. It times out on 84b6ba3, where the second part kept
the state of the run that had stood second.
- `tests/runtime/text-document-lines.test.mjs` now tests `changes`,
which replaces `keptLines`.


`test_a_text_document_update_keeps_a_selection_it_carries_into_another_run[browser]`,
which #5916 tracks failing on main, passes on this branch in both
engines. On base, in the same session, the third run's first row (line
257) draws no number and the rows after it count from 1 (lines 258–280
read 1–23), after a row moved into that run's first place.

Ran: the tests above; 51 browser and journey tests that read a text
document (this branch's selection, by grepping `tests/browser` and
`tests/journeys` for text documents, their row selectors and helpers,
merged with the reviewer's; 74 cases with their engines): all pass but
`test_each_sampled_focus_ring_is_shown_whole_in_its_surface`, which
fails the same way on base; `tests/runtime/text-document-*.test.mjs`;
`pnpm run check:browser`; pre-commit on the changed files. A stress of
120 random edits (edits, insertions, deletions and moves of up to 80
lines) and of 120 add-and-remove cycles on a 3,002-line document left
the text equal to the source and every drawn number right at four places
in each.

## What this leaves

- After a rewrite, syntax colour still tokenizes again from the first
changed line down to the view (`RunColours.walk`, in 8 ms slices). With
the reader at line 25,000, an edit at line 100 spent about 130 ms of
Chromium's main thread there, and the runs in view showed colour from a
fresh grammar state until the walk reached them. Comparing the grammar
state each run ends in with the one kept from before the rewrite could
stop the walk at the changed run.
- Outside the widget, each update still does work that grows with the
document. The keyboard scopes walk the document's rows after it draws
(`claimPaintedRoots`, 17 ms in Chromium on 50,000 lines), and with a
selection in the document, the command hints resolve its passage against
the document's text (`selectionAnchor` → `segmentsIn`, about 75 ms).

## Calls Max may reverse

- Runs hold 32 to 255 rows after a rewrite rather than exactly 128 (a
run no rewrite touched, or the first run, can hold fewer), folded and
split by the thresholds above.
- A run's first line sits on its first row (`--lf-lines-before`), and a
row that stops being first keeps a value the stylesheet doesn't read.
- A reopened tab's colour returns through a second observation of the
listing's size, armed when the fold closes, and a read of the runs'
boxes as it opens.

## Not verified

- WebKit and Safari: not run.
- Release Firefox, as opposed to Playwright's build.

Closes #5916. 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