Skip to content

Walk an opened tab's document once for Tab stops, not on every reach paint - #6036

Closed
max-sixty wants to merge 3 commits into
mainfrom
grind/tab-open-walks
Closed

max-sixty wants to merge 3 commits into
mainfrom
grind/tab-open-walks

Conversation

@max-sixty

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

Copy link
Copy Markdown
Owner

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) walked every row of the opened document on each of the five reach paints the opening ran, to ask whether its listing holds a Tab stop: 38 to 98 ms a paint in Chromium. reach.js now reads what a box holds once and again only when that changes, so the opening walks the listing once. Opening the tab takes a median of 475 ms of main-thread time in Chromium (567 ms on main) and 1.45 s in Firefox (1.57 s on main).

What changed

paintReachNow (reach.js) asks every box that overflows whether it holds a control that takes a Tab stop, and lends the box a stop when none does. It asked tabStops, which walked the box's whole tree, on every paint. A paint follows a resize or a widget's layout notice, which adds or removes no control.

reach.js now keeps, for each box, the elements whose markup puts them in the tab order, as tabStopCandidates (focus.js) finds them. Each paint asks only whether one of them takes a stop now (takesTabStop: tab index, enabled, not inert, drawn), since a style change can move that without touching the tree. What the box holds is read again after any of these:

  • a change to the nodes in the box's tree, or in a shadow tree the walk crossed, or to an attribute the walk reads (href, contenteditable, tabindex, controls, slot, name, derived from the selector the walk matches as TAB_STOP_MARKUP). One MutationObserver hears them, and reach.js reads its pending records before each answer, so a stop the same pass lends a nested box still counts for the box around it;
  • the definition of a custom element the walk met undefined, which may build a shadow tree as it upgrades, as a component library that registers after the page's first paint does;
  • a layout notice from a widget inside the box, which may have built a shadow tree no watch reaches.

The walk keeps its own stack and steps through siblings, as browser/AGENTS.md ("One answer for each question") asks of a walk. It replaces the recursive walk #6031 left in focus.js, and also returns the shadow trees it crossed and the custom elements it met undefined, which the observer needs. Over the opened listing it takes about 17 ms in Chromium, as #6031's walk does.

An earlier commit on this branch also stopped the page's reading place from reading the opened document's rows. The document's own place still read all of them, so the cost moved to the first scroll rest in the document: Chromium's first wheel step went from 97–109 ms to 212–239 ms in review. A place in a text document reads the whole text to make its quote unique, and reading only around the place is not a small change, so reading-place.js and bounds.js are as main has them.

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 and waits until it is ready and quiet for 1.5 s. Busy time is the main thread's, summed from gaps over 20 ms (16 ms for the wheel) in a chain of zero timeouts. Arms are interleaved round by round in one session on this Mac, base c54c277a60 (main; focus.js and reach.js are unchanged since) against this branch, swapping only focus.js and reach.js.

Input Engine main This change
Click on tab 17, which nobody had opened Playwright Chromium 158 (headless shell 1254) 549–583 ms, median 567 465–510 ms, median 475
Click on tab 17 Playwright Firefox 159 (build 1572) 1.52–1.68 s, median 1.57 1.42–1.47 s, median 1.45
Each of 4 wheel steps of 300 px over the opened listing Chromium longest step 38–40 ms, others 0 longest step 40–41 ms, others 0–28
Each of 4 wheel steps Firefox longest step 38–124 ms longest step 21–96 ms

The 1-minute load average was 4 to 8 throughout. Main here includes #6031, whose array walk took Chromium about 17 ms over the listing, run five times as the tab opened. The wheel rows show the first scroll rests in the opened document cost what they cost on main: the reading place is main's on both arms.

What remains in Chromium's profile of an opening: the tab strip's scan of every panel for x-awaits widgets (about 145 ms; #5996 describes the package export its fix needs), the page's reading place reading the opened document (about 110 ms), and syntax colour for the runs in view (about 65 ms).

Tab order is unchanged. leaf-dev probe --base with a journey on a side tab set holding an lf-options ask, a <details>, a button, a scroller with no control (lent a stop), a scroller holding a link and a written text document gives the same Tab order, aria-keyshortcuts, digit pick and ArrowDown route in both arms in Chromium and Firefox, and a link added deep in the bare scroller takes its lent stop back at the next layout notice. An independent review compared Tab order against main across scripts, revisions, tabindex/inert/hidden/disabled changes and a 12-file diff in both engines and found it the same, apart from the late custom element this branch now handles.

Tests

In tests/runtime/reach.test.mjs:

  • a box reads what it holds once, until that changes, and counts a stop lent inside it: a layout notice repaints two nested scrollers without reading their lines, a stop lent to the inner box takes back the outer box's in the same pass, and a control added deep inside is read and takes back the inner box's. It fails on main at the first assertion.
  • a box reads again what it holds once a custom element in it is defined: a custom element defined after the box was read, which draws its control in a shadow tree, takes back the box's lent stop. It fails without the definition check.

Ran pnpm run test:runtime, pnpm run check:browser, and in the browser suites the lent and claimed stop tests, Tab stops in shadow trees and live samples, test_workspaces_and_bounds.py, both test_tabs.py, the text-document tests of test_code_and_documents.py, both test_reading_place.py, test_reading_position.py (revisions keeping the reader's place), test_panes.py and the Tab and key tests of test_previews.py: 253 passed. Two failed, and fail the same way on main: test_a_text_document_update_keeps_a_selection_it_carries_into_another_run[browser] and test_each_sampled_focus_ring_is_shown_whole_in_its_surface.

Calls Max may reverse

  • A shadow root attached later to an element that was already defined, with no change to the box's tree and no layout notice from a widget inside it, is read at the box's next change. Before, it was read at the next paint.

Not verified

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

Part of #5194

…age's place

Opening a closed tab on #4781's page still read every row of the opened text
document in script: reach.js asked `tabStops` whether its listing holds a Tab stop
on each of the five reach paints the opening ran, 38 to 98 ms each in Chromium,
and the page's reading place read the document's 95,581 rows (110 ms in Chromium,
170 ms in Firefox) for blocks it then dropped.

- reach.js keeps the controls each box holds (focus.js, `tabStopCandidates`)
  until a MutationObserver on the box's tree, or on a shadow tree the walk
  crossed, reports a change, or a widget inside the box sends its layout notice.
  Each paint asks only whether a kept control takes a stop now (`takesTabStop`),
  and reads the pending records first, so a stop lent to a nested box in the same
  pass still counts for the box around it.
- The walk keeps its own stack and steps through siblings, without a generator
  per level or the `children` collections Chromium keeps for an element's life
  (the change #6031 makes to the old walk).
- A reading of the page box reads only the parts whose scroller is the page,
  since every reading of the page drops the blocks a box of their own scrolls.
- bounds.js finds a bounded block's scroll box when a widget's layout notice
  draws it, as lf-tabs sends when it opens a tab, instead of at its first size
  observation after layout, so the opened document's region names its listing as
  its scroller in the frame where the page's place is recorded.

Main-thread busy time to open tab 17, base 28c1812 against this change,
interleaved in one session: Chromium click 690 to 391 ms (medians of 4), Firefox
click 1.80 to 1.48 s (medians of 4), Chromium ArrowDown 736 to 398 ms (2 each).
…and leave the reading place as main has it

Review of the first commit found three things.

- A custom element defined after reach read a box, as a component library that
  registers after the page's first paint is, built its control in a shadow tree
  the box's observer never heard, and the box kept the Tab stop it had lent, so
  Chromium gave the scroller a stop before the control inside it. The walk now
  also returns the custom elements it met undefined (focus.js,
  `tabStopCandidates`), and each paint reads the box again once one of them is
  defined.
- The observer took every attribute write in a box, Leaf's focus marks
  included. It now hears only the attributes the walk reads
  (`TAB_STOP_MARKUP`, derived from the selector the walk matches, with
  `tabindex`, `slot` and `name`).
- The page's reading place no longer read the opened document's rows, but the
  document's own place still did, so the cost moved to the first scroll rest in
  the document: Chromium's first wheel step went from 97-109 to 212-239 ms. A
  place in a text document reads the whole text to make its quote unique, and
  reading only around the place is no small change, so reading-place.js and
  bounds.js are back as main has them.

The cost test's lines spy now also asserts that a changed box's lines are
read, so a walk that stopped calling `matches` can't pass it, and a new test
defines a custom element after the box was read.
@max-sixty max-sixty changed the title Open a closed tab without reading its document for Tab stops or the page's place Walk an opened tab's document once for Tab stops, not on every reach paint Nov 15, 2026
@max-sixty max-sixty closed this Nov 15, 2026
max-sixty added a commit that referenced this pull request Nov 15, 2026
…rawn tree by one step (#6067)

A reading of the place in a long text document no longer asks each of
the document's lines whether the browser draws it, each document a
reader opens keeps a reading 5 MB smaller, and the runtime's walks of
the tree as the browser draws it share one step down that keeps no child
lists. Over a 96,000-line document, reading the document's place takes
3.0–4.2 ms in Firefox (5.4–7.1 ms on main) and 4.1–4.4 ms in Chromium
(5.5–5.7 ms).

## What changed

**A reading place skips a document's undrawn lines a run at a time**
(`reading-place.js`, `drawnSegments`). The place reads a text document's
listing as one block, one segment a line. Each reading asked every
segment whether the box skipping it held it, 95,945 `contains` calls a
scroll rest over 96,000 lines, though only the runs near the window are
drawn. Once a segment's parent is undrawn, `lastHeld` finds the last
segment the skipping box holds by doubling a step and then halving it,
since a walk meets what a box holds together: 11,214 calls a rest. A
segment a box draws without holding it, as a slot's assigned text is,
ends that search early and goes on through the usual question, so the
segments yielded are the same.

**One step down the drawn tree** (`shadow-tree.js`, `eachRenderedChild`
and `eachRenderedElement`). Four walks each had their own step from a
node to what it draws, and read `children` or `childNodes`, which
Chromium keeps for the node's life once asked (#6031):
`renderedChildElements` (keyboard scopes), `renderedChildren` in
`shadow.js` (margin pins' cover and the controls inside a widget),
`readingChildren` (place previews) and focus.js's keyed match. Each now
calls the one step, which goes sibling to sibling, as does focus.js's
Tab-stop walk; `renderedChildElements` and `shadow.js`'s
`renderedChildren` are deleted. The trees differ only in which shadow
root a host draws in place of its children, so a walk names it with
`rootOf`: an open root by default, a declared one for the page reading,
a closed stage too for the keyboard, and none for the keyed match, which
reads the item's own tree. Element walks step from element to element:
handed every node, the Tab-stop walk made opening a tab with a 5,000-row
table 22–30 ms slower in Chromium.

The step follows a slot as the browser draws it: what is assigned to it,
a slot forwarded into it among them, or its own children where nothing
is; a slot outside a shadow tree is an ordinary element. Three walks
change at the edges:

- The keyboard walk no longer visits a host's light children that no
slot draws, and visits a slot's fallback only when nothing is assigned
to it. Neither is drawn.
- The margin and Tab-stop walks no longer lose the children of a
`<slot>` outside a shadow tree, for which `assignedNodes({flatten:
true})` answers nothing in Chromium and Firefox (checked with `leaf-dev
probe`).
- `readingChildren` lists a slot forwarded into another as that slot's
child, as `readingParent` climbs, where it listed what the forwarded
slot is assigned. This is the one place a place preview's step and the
page reading's `walk` still differ: `walk` passes through a forwarded
slot to what it is assigned, so both reach the same words with the
forwarded slot as one more step in the preview's tree.
- An empty slot's fallback is read alike before and after, by the
preview and by `walk`: Chromium and Firefox give a slot's own children
as its flattened assignment when nothing is assigned to it (checked with
`leaf-dev probe`). happy-dom gives none, which made the runtime tests
read the two as different; `tests/runtime/dom.mjs` now flattens as the
DOM spec does.

**A reading holds one object for each text node** (`passages.js`,
`indexed`). Indexing a reading copied each segment the walk made, to add
where it starts in the reading's text; it now writes that into the
walk's segment, which the walk builds in one literal rather than a
spread.

## Evidence

Pages: `leaf-dev bench`'s `documents` page at size 8 (three tabs, each a
text document bound to a source), with each document 96,000 lines (4.6
MB) as on #4781's page, and the same page with short documents and a
5,000-row table in an `lf-scroll` box in each tab. The script that adds
both to `leaf-dev bench` and writes them as probe sources is not in this
change; the 96,000-line page is `leaf_dev.commands.bench` with
`DOCUMENT_STEPS = 48000`, its markup `side_documents(8)` and its data
`document_sources(8)`. CPU readings are `leaf-dev bench --read cpu
--base`, interleaved arms, lowest of the rounds; base is `4c8696480e`.
The host was shared, with a load average from 21 to over 100.

| Reading | Engine | main | This change |
| --- | --- | --- | --- |
| `capturePlace` of the open document's region, mean per call in five
batches of 20 (probe journey) | Chromium | 5.45–5.65 ms | 4.07–4.35 ms |
| same | Firefox | 5.40–7.05 ms | 3.00–4.15 ms |
| `contains` calls a wheel step, over four steps | both | 95,945 |
11,214 |
| A wheel step in the document, CPU (5 rounds, load 21–36) | Firefox |
111.5 ms | 98.3 ms |
| same (3 rounds) | Chromium | 27.2 ms | 26.0 ms |
| Opening a tab with a 96,000-line document (3 rounds) | Chromium |
324.5 ms | 315.2 ms |
| same | Firefox | 1,638 ms | 1,604 ms |
| JS heap after GC, growth for each document opened with a wheel step in
it (three in turn) | Chromium | 25.3 MB | 20.2 MB |
| Opening a tab with a 5,000-row table in an `lf-scroll` box (5 rounds)
| Chromium | 780.2 ms | 766.9 ms |
| same (3 rounds) | Firefox | 533.0 ms | 531.5 ms |

The wheel step's CPU in Firefox varied from run to run more than the
change: three other runs at loads of 24–90 read −23, −10 and +10 ms. The
`capturePlace` and `contains` rows measure the change directly. The
openings move within their noise.

## Measured and left as they were

Each was measured on main before choosing what to change. Profile shares
are from `leaf-dev bench --profile` in Chromium; the rest are CPU
readings without a profiler.

- **A scroller's Tab-stop walk on each reach paint** (`reach.js`,
`holdsOwnStop`). Opening a tab holding a 5,000-row table in an
`lf-scroll` box asks each of its 15,000 elements for a Tab stop on every
reach paint. Pricing it by answering `false` instead: the opening took
754 ms of CPU in Chromium on main and 727 ms without the walk, and 494
against 491 ms in Firefox. Keeping each box's answer needs an
invalidation mechanism, which #6036 built (an observer over each box's
tree and the shadow trees it crosses, custom element definitions and
layout notices) and was closed; 27 ms of one opening in one engine
doesn't pay for it.
- **A reading place reads a text document whole the first time it
shows** (`reading-place.js`, `shownRuns`; `passages.js`,
`partCollected`). Opening a tab with a 96,000-line document, the page's
place collects every line of the document's part, about half of the
opening's CPU in Chromium's profile (348 of 690 ms over two openings),
though every block of it stands in the document's bounded region and the
page's place drops them. Reading only the region's own words moves the
cost to the first scroll rest in the document, as #6036 found. Its
quote's uniqueness costs once more on that rest, when the document's
reading is indexed and spelled (48 of the first rest's 56 ms in the
profile), and about 1 ms on later rests. Making either cost follow the
window needs `passages.js` to read a datum in pieces smaller than a
part, such as a text document's runs, so a place reads the runs near the
window; that is a redesign of the reading's pieces, left for a change of
its own.
- **Each opened document's reading stays until the document changes**
(`passages.js`, `parts`). Released when its tab closes, the reading
would be read again whenever it is next needed, and, read from the code,
something needs it often: anchor placement re-resolves every quote
placement after any change to the page's words (`anchor-placement.js`,
`hearPageWords`), so a commented document in a closed tab would be read
whole on every change, and page search reads every part for each query.
This change makes each kept reading 5 MB smaller instead. Bounding what
is kept needs placement to leave anchors in hidden documents unresolved
until they show, a call for Max.
- **The page's place lays out every cell of a table in a box that
scrolls on its own.** On the same opening as the first item, the page's
place took 617 of 1,534 profiled ms over two openings, most of it
measuring each of the 15,000 cells in the `lf-scroll` box (`layOut`, 523
ms), and then dropped them all, since words in a box scrolling on its
own name no place (`scrollsAlone`). Other readers of `blocksOnScreen`,
such as the block a page walk starts from, keep them, so skipping them
is a behavior choice too. Not changed.
- In the same profile, lf-tabs' `#activate` spent 180 ms of each opening
inside `setAttribute`, which reads as style invalidation; not
investigated.

## Tests

- `test_a_place_in_a_long_document_names_the_line_at_its_top`
(`tests/browser/navigation/test_reading_place.py`): five lines of a
1,500-line document, each brought to the top of its box with skipped
runs above, are what the document's place quotes. It fails when
`lastHeld` skips to the block's end or 200 lines past a run, and passes
with main's `reading-place.js`.
- `tests/runtime/shadow-climb.test.mjs`: the step down meets what
`renderedParent` climbs, a forwarded slot and a slot's fallback
included, an element walk steps over text a slot is assigned, and a slot
outside a shadow tree draws its own children. Taking the step through
`assignedNodes({flatten: true})` fails the first.
- `a preview reads an empty slot's fallback as the page's reading does`
(`tests/runtime/place-preview.test.mjs`): a heading before a declared
stage whose only slot is empty previews the slot's fallback words, and
`pageText()` reads them too. A step that skips the fallback fails it.
- `tests/runtime/command-controls.test.mjs`: the nested-slot test counts
`assignedNodes`, the call the walk now makes, rather than
`assignedElements`, which it no longer calls (17 reads for 16 levels).

After merging main, `pnpm run test:runtime` (987 passed) and
`test_reading_place.py` and `test_code_and_documents.py` pass (83).
Before it, ran `pnpm run test:runtime` (985 passed), `pnpm run
check:browser`, and in the browser suites
`navigation/test_reading_place.py`,
`projection/test_reading_position.py`,
`widgets/test_code_and_documents.py`, `navigation/test_tabs.py`,
`widgets/test_tabs.py` and `widgets/test_workspaces_and_bounds.py` (184
passed); `controls/test_focus.py`, `navigation/test_key_scopes.py`,
`margin/test_pins.py`, `margin/test_scrollers.py` and the place-preview
tests of `controls/test_previews.py` (157 passed); the focus, key and
line tests of `widgets/test_diffs.py` (16) and the lent, claimed and
reach-stop tests of `test_render_aim.py` (10). An earlier run of the
first group failed
`test_a_page_up_in_a_log_a_stream_feeds_leaves_its_end` in Firefox; run
interleaved on main and this branch under load, it failed 3 of 6 times
on main and 2 of 6 here, and it passed in the final run.

Not verified: WebKit and Safari; release Firefox as opposed to
Playwright's build; memory in Firefox.

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.

1 participant