Repository navigation
Claim a code block's Tab stop as it is first seen, so opening a tab reads none of its document's lines - #6041
Conversation
…eads none of its document's lines A text document's listing is a code block, whose copy control claims its Tab stop (code-copy.js). In a closed tab the block was deferred until drawn, and until then reach.js treated the listing as any overflowing box: each reach paint as the tab opened asked every line whether it takes a Tab stop. On #4781's page that was five walks of 95,581 lines per opening. The block now claims its stop as it is first seen, drawn or not, and reach skips a claimed box. A closed tab's blocks carry the stop and the lent mark while hidden, where Tab cannot reach them. A block the browser skips off screen (`content-visibility: auto`) now takes the stop it takes once drawn: Tab stops on it before it is drawn, where it skipped it, and the copy control is prepared as focus lands, so Enter is named at once.
leaf-agent
left a comment
There was a problem hiding this comment.
The claim-on-first-sight fix removes the walk for code blocks. The walk itself is still there for every other scroller. paintReachNow in reach.js runs overflows(el) && !holdsOwnStop(el) on each drawn, unclaimed candidate on every reach paint. holdsOwnStop is tabStops(el), which filters renderedElements(el), so it reads the box's whole subtree. A wide table or an authored lf-scroll box holding thousands of rows has no copy control to claim its stop. Opening the tab that holds one, or any later paint that reaches it, would still read every row. I traced this from the code and didn't measure it.
That is the cost root AGENTS.md Responsiveness asks to run only for the change that needs it. The fix would be in reach.js: remember each candidate's answer and ask again only when its subtree changes (the arrivals and mutations reach already watches), rather than on every paint. This one-line change doesn't make that harder, so it can land first. Please name the remaining reach.js cost in the description beside Part of #5194, so the next part of #5194 starts from it.
…le in code-copy.js's header The claim now stands once, at the top of `prepareDocumentBlocks`, for drawn and skipped blocks alike, and the second call after the control is wired is gone. The rule moves to the module header beside the copy control's own place in the Tab sequence: a document code block is a Tab stop whether or not the browser has drawn it, and Tab onto one the browser skipped prepares Copy as focus lands. Why it matters: a text document's listing is such a block, and while the claim waited for a closed tab's listing to be drawn, each reach paint as the tab opened asked every one of its lines whether it takes a Tab stop, five walks of 95,581 lines on #4781's page. The new test now runs in Chromium and Firefox, as its neighbour does. Main fails it in both, asking 10,005 times.
Conflict in tests/browser/widgets/test_code_and_documents.py only: this branch and #6041 each added a test after test_opening_a_closed_tab_draws_only_the_lines_in_view (the closed tab's colour test and the Tab-stop test). Both are kept, each with its own engine parametrization.
Opening a closed tab on #4781's page (30 tabs, each an
lf-text-documentof 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:
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.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 inaria-keyshortcutsin 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, base0144d467a9(main) against this branch, swapping onlycode-copy.js.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 --baseon a side tab set holding anlf-optionsask, 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 fortabindex, 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 oftest_render_anchors.py,test_render_code_reading.pyandmargin/test_scrollers.py, the lent and claimed stop tests oftest_render_aim.py, Tab stops incontrols/test_focus.py, shadow trees and live samples, the command reference's stops, a box that starts scrolling late,navigation/test_tabs.pyand the text-document tests oftest_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
content-visibility: autocontent 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
What #5194 still has here
The walk this removes for code blocks is still there for every other scroller.
paintReachNowinreach.jsasksholdsOwnStop(tabStops) of each drawn, unclaimed candidate on every reach paint, and that reads the box's whole subtree, so a wide table or an authoredlf-scrollbox holding thousands of rows is read in full on each paint: a 5,000-row table in anlf-scrollbox inside a closed tab had its rows and cells asked fortabindex120,000 times as the tab opened (8 walks of 15,000 elements). The fix belongs inreach.js: keep each candidate's answer and ask again only when its subtree changes. This change doesn't make that harder.Part of #5194