Repository navigation
Read a long document's place a run of lines at a time, and walk the drawn tree by one step - #6067
Conversation
…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
left a comment
There was a problem hiding this comment.
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.
…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.
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,945containscalls a scroll rest over 96,000 lines, though only the runs near the window are drawn. Once a segment's parent is undrawn,lastHeldfinds 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,eachRenderedChildandeachRenderedElement). Four walks each had their own step from a node to what it draws, and readchildrenorchildNodes, which Chromium keeps for the node's life once asked (#6031):renderedChildElements(keyboard scopes),renderedChildreninshadow.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;renderedChildElementsandshadow.js'srenderedChildrenare deleted. The trees differ only in which shadow root a host draws in place of its children, so a walk names it withrootOf: 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:
<slot>outside a shadow tree, for whichassignedNodes({flatten: true})answers nothing in Chromium and Firefox (checked withleaf-dev probe).readingChildrenlists a slot forwarded into another as that slot's child, asreadingParentclimbs, where it listed what the forwarded slot is assigned. This is the one place a place preview's step and the page reading'swalkstill differ:walkpasses 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.walk: Chromium and Firefox give a slot's own children as its flattened assignment when nothing is assigned to it (checked withleaf-dev probe). happy-dom gives none, which made the runtime tests read the two as different;tests/runtime/dom.mjsnow 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'sdocumentspage 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 anlf-scrollbox in each tab. The script that adds both toleaf-dev benchand writes them as probe sources is not in this change; the 96,000-line page isleaf_dev.commands.benchwithDOCUMENT_STEPS = 48000, its markupside_documents(8)and its datadocument_sources(8). CPU readings areleaf-dev bench --read cpu --base, interleaved arms, lowest of the rounds; base is4c8696480e. The host was shared, with a load average from 21 to over 100.capturePlaceof the open document's region, mean per call in five batches of 20 (probe journey)containscalls a wheel step, over four stepslf-scrollbox (5 rounds)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
capturePlaceandcontainsrows 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 --profilein Chromium; the rest are CPU readings without a profiler.reach.js,holdsOwnStop). Opening a tab holding a 5,000-row table in anlf-scrollbox asks each of its 15,000 elements for a Tab stop on every reach paint. Pricing it by answeringfalseinstead: 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.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 needspassages.jsto 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.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.lf-scrollbox (layOut, 523 ms), and then dropped them all, since words in a box scrolling on its own name no place (scrollsAlone). Other readers ofblocksOnScreen, such as the block a page walk starts from, keep them, so skipping them is a behavior choice too. Not changed.#activatespent 180 ms of each opening insidesetAttribute, 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 whenlastHeldskips to the block's end or 200 lines past a run, and passes with main'sreading-place.js.tests/runtime/shadow-climb.test.mjs: the step down meets whatrenderedParentclimbs, 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 throughassignedNodes({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, andpageText()reads them too. A step that skips the fallback fails it.tests/runtime/command-controls.test.mjs: the nested-slot test countsassignedNodes, the call the walk now makes, rather thanassignedElements, which it no longer calls (17 reads for 16 levels).After merging main,
pnpm run test:runtime(987 passed) andtest_reading_place.pyandtest_code_and_documents.pypass (83). Before it, ranpnpm run test:runtime(985 passed),pnpm run check:browser, and in the browser suitesnavigation/test_reading_place.py,projection/test_reading_position.py,widgets/test_code_and_documents.py,navigation/test_tabs.py,widgets/test_tabs.pyandwidgets/test_workspaces_and_bounds.py(184 passed);controls/test_focus.py,navigation/test_key_scopes.py,margin/test_pins.py,margin/test_scrollers.pyand the place-preview tests ofcontrols/test_previews.py(157 passed); the focus, key and line tests ofwidgets/test_diffs.py(16) and the lent, claimed and reach-stop tests oftest_render_aim.py(10). An earlier run of the first group failedtest_a_page_up_in_a_log_a_stream_feeds_leaves_its_endin 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