Repository navigation
Stop every focus move restyling the page in Chromium - #5844
Merged
Merged
Conversation
Opening the comment box from a diff line's `+` on `pr-walkthrough` spent 22 ms restyling the whole page before the click handler ran, and more inside it, because every focus move restyled every element in Chromium. `focus.js` writes `data-lf-focus*` on the focused element and its ancestors, and many `:has()` rules read those marks. One rule anywhere whose `:has()` leads to a subject naming no element makes Chromium restyle everything under each element whose `:has()` such a write may change, and four shipped rules did: base.css's pane end room (`:not(:has(> footer)) > :not(header, footer)`), layouts.css's workspace end room, reading.css's sticky row names (`> * > tr > :first-child`) and disclosure.css's instant keyboard closing (`:has(…)::details-content`). Firefox doesn't do this; a focus-mark toggle costs it under a millisecond on the same page. Each rule now asks without a `:has()` before an untyped subject: - The pane and workspace end room read the body's position, since a pane, like a workspace, holds one body with its footer after it. - The sticky row names name their cells, `:where(th, td):first-child`, as the table's other rules already did. - A diff fold states its fold duration in a non-inherited registered property, set to 0s by a `:has()` on the fold itself while its summary holds keyboard focus, and its `::details-content` reads it. Keying the fold on its own `data-lf-focus-visible-within` instead would have relaid out a file's whole content each time focus entered the comment box inside it (30 ms a click). `test_a_has_leads_only_to_a_subject_naming_an_element` replaces `test_an_untyped_has_leads_only_to_a_typed_subject`. The old guard flagged a subject naming no element only after a `:has()` that was the whole of its compound, and its docstring held that naming an element on the `:has()` side was enough; probes showed `.zz:has(> .yy) > :not(.q)`, `> *`, ` :first-child`, `::details-content`, `::placeholder` and `::file-selector-button` each restyled the page (16–20 ms a toggle), while `> .q`, `> p`, `> :where(p, li)`, `::after`, `::marker` and `::part()` cost 0.3–1.4 ms. The guard now flags any `:has()` before a subject naming no element, and a `:has()` before those shadow pseudo-elements, including inside an `@scope` and inside an `:is()`, `:where()` or `:not()`, where an independent review measured the same cost; it fails on main with these four rules. Measured in Chromium on this Mac under load (load average 10–22): | | main | this branch | |---|---|---| | elements restyled per focus-mark toggle, `pr-walkthrough` | 7,276 (16 ms) | 1,392 (3.5 ms) | | same, `command-hub` | 1,801 (7.7 ms) | 788 (3 ms) | | `+` click to next paint, `pr-walkthrough` with 60 threads, median of 21 | 104 ms | 88 ms | | `+` click to the frame showing the box, same | 46 ms | 29 ms | The computed end room, padding, cell position and fold duration these rules set are identical on main and this branch across 11 examples (workspaces with and without footers, panes, row-header tables, diff folds). Checked: tests/model/test_interact_layer.py, the CSS model tests, build/registry.test.mjs, and by node id the workspace end-room tests, `test_a_scrolling_table_keeps_its_row_names_in_view`, `test_evidence_close_folds_room_but_retires_controls_immediately` (every engine, both kinds) and `test_resolving_a_thread_restyles_what_focus_marks`.
leaf-agent
approved these changes
Nov 11, 2026
This was referenced Nov 12, 2026
max-sixty
added a commit
that referenced
this pull request
Nov 12, 2026
In Chromium, a focus move near the end of a long page restyled every block before the focused one. One Tab between the last two sections' links of a column page of 1,024 sections restyled 7,152 elements (40 ms of style), and 1,131 (27 ms) in a `main` with no Layout. #5844 fixed the shape that restyled the whole page on any focus move; two others remained, and they combine: - **A `:has(~ …)` asked of every child of `main`.** The parts layout named what stands before the parts as `:where(main > *):has(~ [data-lf-views="parts"])`, which Chromium asked of every child of every `main`, flagging each later sibling. On each write to an attribute some `:has()` reads, focus marks among them, Chromium walks back from a flagged element through every earlier sibling and restyles each one a `:has()` names as its subject. `main` now states `--lf-holds-parts`, registered and not inherited, and its children read it with a style query. `.layout-sidebar > aside:has(~ …)`, which Chromium asked of every `aside` in a page's content, is gated to the sidebar's own children. - **The column's room counted as changed on every restyle of the column.** With the walk gone, a column page still restyled everything under `main`: `main` is asked a `:has()` again as focus moves (`:has(> [data-lf-track])` names no element before it), and Chromium counted `--lf-room-start`, a registered `<length-percentage>` mixing a length with a percentage, as changed each time. `notes/browser-support.md` recorded this for an inheriting property; it holds for this non-inheriting one too. The column now resolves the room's lengths in registered `<length>`s (`--lf-room-length`, `--lf-room-note`) and states the room, percentage and all, only in `--lf-free-start` and its kin, registered `*`; `--lf-room-start` and `--lf-room-end` are gone. Readers resolve the same expressions as before. Restyled elements per focus move, from a Chromium 158 trace, median of 6 Tab/Shift+Tab moves per arm, arms interleaved, load average 12–16 on 18 cores; ms is the summed `UpdateLayoutTree` duration: | page | main | this branch | |---|---|---| | column, 64 sections | 1,392 (10.1 ms) | 1,014 (9.1 ms) | | column, 1,024 sections | 7,152 (40.5 ms) | 1,014 (9.7 ms) | | plain `main`, 64 sections | 171 (2.2 ms) | 108 (1.8 ms) | | plain `main`, 1,024 sections | 1,131 (26.6 ms) | 108 (2.0 ms) | What remains on the column page doesn't grow with it: about 880 of the 1,014 are four restyles of the chrome as the margin projects the focused link. Firefox, read from the Gecko profiler's `Styles` markers, restyles the same elements a move on both arms at both sizes: 405 on the column page (about 9 ms, 24 moves per arm) and 26 in the plain `main`. `test_a_has_reads_no_sibling_of_an_element_most_selectors_reach` holds the first shape off the shipped CSS: on main's `layouts.css` it names exactly the parts rule (three copies) and the sidebar's `aside` rule, and nothing else. `test_a_focus_move_near_a_long_pages_end_restyles_what_focus_marks` counts a move's restyle on pages of 8 and 48 sections; it fails on main's `layouts.css` (1,093 against 1,333), with either fix reverted alone, and with `--lf-free-start` registered `<length-percentage>` again (1,225 against 2,185), which is the recheck the note now names. **Where to look.** The parts title rule now selects whatever `main` holds besides the parts' holder, where it used to exclude anything after the holder. `page-layout.md` said the header is the only element written before the parts; it now says the header is the only element in `main` besides the parts, which is what the layout assumes, and a live parts page's `main` holds only those two. The title's computed styles match main's in Chromium and Firefox, desktop and phone. **What I ran.** `tests/model/test_interact_layer.py`; by name, the focus, resolve, send and floating-box restyle tests in `test_chrome_contracts.py`, and the parts, sidebar-track, note, breakout (including `test_a_column_stated_in_font_units_gives_every_breakout_the_same_room`), workspace-part and rail tests in `test_native_document_layout.py`, `test_render_pages.py`, `widgets/test_tabs.py`, `controls/test_rings.py`, `margin/test_rail.py` and `drafts/test_reading_place.py` (220 passed). `leaf-dev stills --base origin/main` on `root-parts`, `pr-walkthrough`, `triage-board`, `sidebar-contents`, `feature-gallery`, `alert-thresholds`, `lost-update`, `shadow-scale`, `gallery-wide-passage`, `gallery-wide-schedule`, `frame-edges` and `margin-gallery`: all unchanged in Chromium. In Firefox, 9 unchanged; `margin-gallery` and `gallery-wide-schedule` differ by 0 px, only in the margin rows' offsets, which differ between two runs of identical code; `sidebar-contents` never settles in Firefox on either arm. At phone width, `root-parts`, `pr-walkthrough` and `feature-gallery` are unchanged. Element boxes and block margins matched the base on ten examples and the corpus. An independent review also found no visible change in WebKit. **Not in this PR.** Part of #5194's other follow-up, the insertion cost in Firefox from the frame-edge trims' `:nth-*-child(1 of …)` and the reading theme's `.eyebrow + :is(h1, h2, h3)`, needs a decision on the look and stays out of this change. Part of #5194.
max-sixty
added a commit
that referenced
this pull request
Nov 12, 2026
No `:has()` in Leaf's sheets now reads where focus is. After #5844, a focus move on `pr-walkthrough` still restyled 1,419 elements in Chromium (5.5 ms), and 7,345 (22 ms) once any rule, an author's included, put a `:has()` before a subject naming no element, because 25 rules asked with `:has()` for a focus mark, as `.choice:has(> input[data-lf-focus-visible])` does. A box that shows a control's focus is now named by the control (`--lf-focus-box`, a selector), and `focus.js` marks that box `data-lf-control-focus` / `data-lf-control-focus-visible`. Rules for focus anywhere in a box read its existing `-within` marks. A move now restyles 18 elements (2.0 ms), with that authored rule or without it. The new `test_no_has_reads_where_focus_is` guards the rule; it fails on main. Where to look: - `focus.js` re-reads the box a control names when focus reaches it, when its ancestry changes, and when one of the control's own attributes changes (a panel view's `data-lf-leaving`), so a repaint after a tree change forces no style pass. Typing in a comment field writes no attribute on it. - The obligation's location ring now stands down on `--lf-obligation-ring: none` declared by the focused control itself (`locationStandsDown`), and a margin card's transcript fade lifts on `data-lf-transcript-focus`, written by `thread-preview.js`. - One behavior changes, which you may want back: the band rules in `surfaces.css` treated a marked box as wearing a control's ring whenever any child held visible focus. They now do so only for a declared box, as their comment says, so a commented or traced paragraph keeps its brackets while a link in it holds focus. Keeping the old reading needs one more mark, on the focused node's parent. The faces focus draws match main on each box from the control to the body, focused and blurred, for 20 controls across 8 examples. Max asked for this follow-up after #5844 and hasn't read it yet; an independent Codex review found a stale-cache case, now fixed and covered by a runtime test. Tests: the CSS guards in `test_interact_layer.py`, `pnpm run test:runtime`, `pnpm run check:browser`, and 24 browser tests by node id on rings, choices, obligations, diff folds, the file editor, the sample gutter and the thread card. > _This was written by Claude Code on behalf of max-sixty_
max-sixty
added a commit
that referenced
this pull request
Nov 12, 2026
…t at a time (#5875) On #4781's page (30 tabs, each a 4 MB text document, one comment), a triple click on a line of the open document held the main thread 450–580 ms in Chromium and 0.9–1.5 s in Firefox, and a page search held it for the whole search: 2.6–2.7 s in Chromium and 4–4.3 s in Firefox for a word the page doesn't hold, and for a common word typed a key at a time, 6–11 s a key in Firefox and minutes in Chromium. Now a triple click holds it 185–270 ms in Chromium and 210–380 ms in Firefox, and each of the search's own tasks reads at most one document: under 200 ms in Chromium and under 380 ms in Firefox, for a word the page doesn't hold or a common word typed. Find returns the same matches in the same order, a walk through them steps as main's does, and a stored anchor resolves or detaches as before. Two costs on Find's path are not the search and stay, both on main too: opening the search field the first time restyles the whole page in Firefox (6 s here), and landing on a match in a closed tab reveals it (1.4 s in Chromium, 1.6–5 s in Firefox). See "Left as it was". ## The triple click A selection's anchor is checked unique in its datum's words (root `AGENTS.md`, "Validate once and share readings"). Profiles in both engines showed most of the gesture was that check reading the 4 MB document again, several times: - **Finding the quote's copies.** One JavaScript pass of an automaton over every character, 85–125 ms over the document in either engine, once for the word the double click selects and once for the line. A reading's letters (its characters less whitespace and edges) are now kept with it (`spell`), taken out by the platform's own replace and marked every 32 letters, 15–30 ms once a reading. A few quotes are found in them by the platform's string search, under 1 ms a quote; a batch of more than 32 still takes one automaton pass, so placing many comments stays linear in the page and the quotes (`eachPlace`, which `findEach` uses too). Each place is held to its quote's spaces where it stands in raw (`copyAt`), as before. - **Telling a common word's 17,000 copies apart.** `holds` read the words around every copy. It now compares a side's letters outward through raw first and refutes at the first letter that differs, recording that span as what it read, so the index still re-places a quote when a change there could complete its context. Only a copy whose letters all match has its words read. - **Checking each copy stands under the datum.** Two binary searches and two climbs of the tree per copy, 0.7 s in Firefox for the word. A copy in an element's own reading stands under it, unless a slot of the element's own tree shows it words from outside. - **Asking again.** The comment button's placement, the command bar's availability, the response target and the remembered selection each asked for the anchor, so the check ran up to eight times a gesture. `findQuote` keeps its answer with the reading it answered for; a change makes a new reading. It still asks the quote's copies first each time, since that is what brings a quote the index has come to keep into it, so a change to a quote first answered for a selection is heard once its draft is placed. What a triple click still pays beyond its cost on the same page with 40 KB documents (89–110 ms in Chromium, 74–99 ms in Firefox): the datum's indexed reading on its first capture (14 ms in Chromium, 30–50 ms in Firefox), its letters (15–30 ms), the list of the word's 17,000 copies, which anchor placement keeps for the draft's quote, and the garbage those leave. In Firefox the three clicks run as two tasks; the second holds 120–250 ms on this branch and 180–230 ms on main. ## Page search `pageMatches` (was `pageFind`) is a generator: it reads a part when the search reaches it, and yields null before each part and for each match the page hides. The target picker takes matches 8 ms at a time, the first slice in the input's own task and each later one in a task of its own, so a search waits on at most one document's reading. Matches arrive in reading order, and the search lands where it did: the first match at or below the top of the window, else the first; after a page change, where it stood. Until it lands, the count slot shows an ellipsis; after, the count grows where it stands. The status is busy (`aria-busy`) while the search runs, so a screen reader says the count once, as it ends, rather than at each slice ("Searching" and "Match 3 of 120 so far" are its name meanwhile). Main searched once, as the query was typed (and again before a step after the page changed), so a step after that took 20–48 ms. Here a step or a choice (Tab, Shift+Tab, Enter, `n`) waits only when its target hasn't arrived: it takes matches at once until the search has landed, where it stood if the page changed under it, and the match the step goes to has arrived, or the search has ended (`settle`). That matters most after a match in a closed tab: bringing it into sight opens the tab, which changes the page, so the next step searches again from where it stood. A step back past the first match is the one step whose match comes last, so while the search runs it stays put and says "Searching" rather than wait for all of it. Once Enter has closed the field, a search still finding stops at the first page change rather than start again, and `n` searches again from there; only the open field searches again where it stood after a change, as a reveal makes. ## Measurements #4781's page, served by `leaf server run --lifetime temporary` with a throwaway state home: main at b3387f1, the merge base, against this branch, each arm a snapshot of its tree. One browser per engine and round, warmed 15 s; each gesture on a fresh load, 5 s after the comment's mark stood and every document drew, with the page's view checks stubbed in both arms. The longest task is the longest gap in a chain of zero timeouts, which only main-thread work holds back. Arms alternate within each round; load average 3–13. The branch arm is this PR before its last change, the busy status, which redraws the count each slice as the first push did. Chromium 156 headless and Firefox 157 through Playwright. Milliseconds, one per round. | Longest task | Chromium, main | Chromium, branch | Firefox, main | Firefox, branch | |---|---|---|---|---| | Triple click, first gesture | 548, 575, 552 | 253, 270, 254 | 1,179, 1,157, 1,112 | 376, 222, 332 | | Triple click, after a click | 448, 455, 446 | 185, 187, 192 | 1,022, 1,474, 901 | 210, 210, 224 | | Find, a word nowhere on the page | 2,654, 2,731, 2,595 | 181, 196, 192 | 4,116, 3,953, 4,318 | 323, 373, 321 | | Find, `heap` typed | not run | 98, 78, 100 | 11,144, 6,387, 6,702 | 364, 219, 321 | Each Find opens and closes the search once before its window, so Firefox's first-open restyle stays out of it, then pastes or types its word. "Nowhere" reads every document; `heap` matches 530,000 times. In Chromium the typed search is still running 12 s later: whether a match shows is read from its styles, and a match in a run of a text document the browser hasn't drawn makes it style that run to answer, 26 ms a run here, in 15,900 runs. The page stays live meanwhile; main's arm isn't run, since `pageFind("widget asks")` had not returned after 14 minutes. On the first push, at load 12–30, a word in a closed tab held 4.1 s in Chromium and 8.7–15 s in Firefox on main; on the branch its search tasks held at most 206 ms and 349 ms, and the reveal on landing 1.4 s and 4.3–5 s. Walking `FINDTOP`'s 30 matches, one a document, Tab goes 1 to 6 in both engines, and `n` after Enter selects the next. Steps right after typing `heap` in Firefox, while the branch's search still runs (main's has ended, since it searched as the key was typed): each key's time from its keydown to the next paint, by Event Timing, three rounds per arm, alternating, load 13–31. | Keydown to next paint | Firefox, main | Firefox, branch | |---|---|---| | Longest key typing `heap` | 11,912, 10,752, 11,040 | 56, 88, 80 | | Tab | 48, 64, 72 | 32, 72, 48 | | Shift+Tab | 48, 48, 40 | 32, 56, 48 | | Shift+Tab from the first match | 5,696, 5,368, 6,400 | 32, 24, 24 | Main's wrap lands on the last of the 529,957 matches, in a closed tab, and reveals it; the branch's stays on the first and says "Searching". Profiles: Chromium through CDP's profiler, Firefox through the Gecko profiler (`MOZ_PROFILER_STARTUP`), for each arm's triple click and search. ## Left as it was - **Firefox's first open of the search field** restyles the whole page as focus enters it: 6.0 s here on main, 58 ms on the 40 KB page, 41 ms the second time. In single runs, disabling the theme's sheet brought it to 0.9 s and deleting every `:has()` and focus rule only to 2.7 s, so I didn't find the rule. #5844 fixed focus restyles in Chromium. - **Revealing a closed tab that holds a large document**, as landing on a match there does: 1.4 s in Chromium, 0.9 s of it `claimPaintedRoots` (`keyboard/scopes.js`) walking every element of the revealed document; 1.6–5 s in Firefox, mostly the tab's activation. A click on the tab pays the same. - **A common word in Chromium** runs for minutes behind the page, as above. Reading only the style of the box that skips a run (`skipper`) would end it in seconds but find words CSS hides inside a run or closed tab the browser hasn't drawn; capping the count, as Firefox's find bar does at 1,000, would too. Either changes Find's results, so it's yours to choose. The text document's line-number counters make each run's forced style 26 ms rather than 4 ms. ## Calls you may reverse - **32 quotes** as the line between string search and one automaton pass. I kept the automaton so many comments on one large document stay linear in the page and the quotes; one mechanism would drop either that or the 1 ms single-quote search. - **A step waits for its match, and a wrap back stays put.** A step past the last match found takes matches at once until the next has arrived or the search has ended, as main's search did up front; Shift+Tab from the first match while the search runs stays and says "Searching". Tab and Enter in the field before the first match has arrived do what they do with no match, as on main for a query with none. - **The count slot's ellipsis** before the search lands, and the count growing under a busy status after. The slot is 7 characters wide, so a word wouldn't fit; a count over "99 of 99" was already cut by its ellipsis on main. ## Tests - `test_a_walk_through_search_matches_in_closed_tabs_reaches_each_in_turn` (both engines): Tab walks four matches in closed tabs and Shift+Tab back one, with the page's clock run fast so each of the search's slices takes one step; then Enter selects the match and `n` the next. It fails on the first push of this PR. - `test_a_step_back_past_the_first_search_match_stays_while_the_search_runs` (both engines): with the page's zero-delay timers held, so the search stands unfinished, Shift+Tab from the first match stays and says "Searching", and Tab goes on to the next. It fails on the second push. - `tests/runtime/passage-boundaries.test.mjs`, "a quote first answered before the index kept it is heard when its words are redrawn": fails with the kept answer asked before the copies. - `tests/runtime/datum-quotes.test.mjs`: capturing a word a datum repeats 2,000 times reads only the words beside the selected copy (fails without the letter check, reading every line), and a datum's quote counts no copy among the words its slot shows from outside it (fails without the slot guard). - `test_a_search_finds_each_match_in_prose_and_every_part_in_reading_order` also holds a search to reading no part ahead of the matches taken (fails when the search reads every part first). - The shared quote cases cover the letters and the letter check (reversing the letter order or dropping the spaces check fails them). Both sides of the 32-quote line run in the existing tests: `findEach`'s cases, and 42 quotes in "a line rewritten in a part changes only the quotes standing on it". Ran: `pnpm run test:runtime` (951 passed), `pnpm run check:browser`, pre-commit on the changed files, and browser selections `-k "search or find or match"` (62 passed) and `-k "quote or capture or selection or select or repeated or twice or datum or comment_on"` (87 passed, 3 failed) over `test_render_anchors`, `test_render_semantic_selection`, `test_render_floating_panel`, `test_render_aim`, `test_reading_pieces`, `test_command_reference`, `test_render_diff_controls`, `test_render_gate`, `test_code_review` and `test_code_and_documents`. The 3 failures fail the same with main's runtime files at the merge base: `test_a_text_document_update_keeps_a_selection_it_carries_into_another_run[browser]`, `test_dense_selection_hints_stay_short_and_reach_an_atomic_visual` and `test_target_hints_name_only_addressable_elements_shown_by_a_disclosure`. Not run: the whole suite (CI), journeys, Safari. #5843 (open) adds the same `COLLAPSE_CHARS` export and builds the same class of gaps, whitespace and edges, in `findText`'s pattern that this builds as `GAPS`. Whichever lands second should take the other's: `findText` can join its words with `GAPS.source`, or `GAPS` can move beside `COLLAPSE` in `kernel-facts.js`. Part of #5194.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
In Chromium, every focus move restyled the whole page: on
pr-walkthrough, one write of a focus mark restyled 7,276 elements (16 ms), and a click on a diff line's+pays that twice, once as the button takes focus and once as the comment box does.focus.jswritesdata-lf-focus*on the focused element and its ancestors, many:has()rules read those marks, and one rule anywhere whose:has()leads to a subject naming no element (or to a shadow pseudo-element such as::details-content) makes Chromium restyle everything under each element such a write may affect. Four shipped rules did; this rewrites them without that shape and generalizes the static guard, which missed them, to the measured rule (including@scopebodies and selectors inside:is()/:where()/:not()). Firefox never paid this.Where to look: the pane and workspace end-room rules now rely on the documented child order (one body, footer after it) via
:last-child; the diff fold's instant keyboard closing now goes through a non-inherited registered--lf-fold-time, because keying it on the fold's owndata-lf-focus-visible-withinrelaid out a whole file each time focus entered the comment box.Result: a focus-mark write restyles 1,392 elements (3.5 ms) on
pr-walkthrough; with 60 threads,+click to next paint went from a median 104 to 88 ms and the comment box usually lands a frame earlier (Chromium, loaded host, 21 interleaved clicks per arm). Most of what remains on a busy page is the margin re-seating every row when the box grows the diff (#5752).Max asked why the box takes ~250 ms to appear on a busy page; he hasn't read this yet. An independent Codex review found two gaps in the guard, both fixed. Tests: the layer model tests,
build/registry.test.mjs, and by node id the workspace end-room, sticky row-name, fold-closing (every engine) and focus-restyle browser tests; the rules' computed values are identical to main on 11 examples.