Skip to content

Read a long document's place a run of lines at a time, and walk the drawn tree by one step - #6067

Merged
max-sixty merged 5 commits into
mainfrom
grind/tree-walk
Nov 15, 2026
Merged

max-sixty merged 5 commits into
mainfrom
grind/tree-walk

Conversation

@max-sixty

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

Copy link
Copy Markdown
Owner

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 Walk an opened tab's document once for Tab stops, not on every reach paint #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 Walk an opened tab's document once for Tab stops, not on every reach paint #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

…rawn tree by one step

A reading place in a text document asked every line whether the box skipping it held
it: 95,945 `contains` calls a scroll rest over 96,000 lines. `drawnSegments` now finds
the last segment a skipping box holds by doubling and then halving a step, 11,214 calls
a rest. Reading the document's place took Firefox 3.0-4.2 ms rather than 5.4-7.1, and
Chromium 4.1-4.4 rather than 5.5-5.7.

Four walks of the drawn tree each had their own step down and read `children` or
`childNodes`, which Chromium keeps for a node's life: `renderedChildElements` (keyboard
scopes), shadow.js's `renderedChildren` (margin cover), `readingChildren` (place
previews) and focus.js's keyed match. They and the Tab-stop walk now take
`eachRenderedChild` or `eachRenderedElement` (shadow-tree.js), which step sibling to
sibling and name the shadow root a host draws with `rootOf`. A slot draws what is
assigned to it, a forwarded slot among it, or its fallback where nothing is; a slot
outside a shadow tree is an ordinary element, whose children the margin and Tab-stop
walks lost. The keyboard walk no longer visits light children no slot draws.

Indexing a reading writes each segment's place in the reading's text into the walk's
own segment rather than a copy: Chromium's heap grows 20.2 MB rather than 25.3 MB for
each 4.6 MB document opened.

Part of #5194

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

readingChildren now reads an empty slot's fallback, though the walk it says it mirrors doesn't. For a slot in a shadow tree with nothing assigned, eachRenderedElement falls through to the slot's own children (drawnFrom returns the slot when assignedNodes() is empty). Before this change, assignedElements({ flatten: true }) returned [] there. walk's enterElement still reads only child.assignedNodes({ flatten: true }), and its comment says "a slot reads what is assigned to it, not its fallback".

So a place preview of a declared stage with an empty fallback slot (readChildren, membersPart, after in place-preview.js) can now show words that the page reading, search and anchors never see. The description lists only the forwarded-slot difference for readingChildren. If previews should follow walk, they should keep skipping fallback. If the page reading should read fallback the way the browser draws it, walk changes too. Either way, the description should mention it.

…why lastHeld may step over drawn-elsewhere segments

`test_a_place_in_a_long_document_names_the_line_at_its_top` brings five lines of a
1,500-line document to the top of its box, with skipped runs above, and asks the
document's place for its quote. It fails when `lastHeld` skips to the block's end or
200 lines past a run, and passes on main's reading-place.js.

The `lastHeld` comment now says why stepping over a segment a skipped box draws without
holding is right: whatever a skipped box draws is undrawn too. `eachRenderedChild`'s
comment says it hands over comments, and the browser/AGENTS.md row names the roots by
`rootOf`, since the keyboard's walk also enters a closed stage.
@max-sixty
max-sixty marked this pull request as ready for review November 15, 2026 20:45
leaf-agent
leaf-agent previously approved these changes Nov 15, 2026
…slot's fallback

A slot in a shadow tree with nothing assigned draws its own children. Chromium and
Firefox (checked with `leaf-dev probe`) give them as its flattened assignment, so the
page reading's `walk`, which reads `assignedNodes({flatten: true})`, reads the fallback,
and so does `readingChildren` through `eachRenderedElement`; main's `readingChildren`
did too, through `assignedElements({flatten: true})`. happy-dom answers nothing there,
which made the two look different in the runtime tests: tests/runtime/dom.mjs now
flattens a slot as the DOM's "find flattened slottables" does.

`a preview reads an empty slot's fallback as the page's reading does`
(place-preview.test.mjs) previews a heading before a declared stage whose only slot is
empty, and asserts the preview and `pageText()` both read its fallback words. A step
that skips the fallback fails it.
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