Skip to content

Stop a focus move restyling every block before it in Chromium - #5876

Merged
max-sixty merged 2 commits into
mainfrom
grind/selector-invalidation-2
Nov 12, 2026
Merged

max-sixty merged 2 commits into
mainfrom
grind/selector-invalidation-2

Conversation

@max-sixty

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

Copy link
Copy Markdown
Owner

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.

A focus move near the end of a long page restyled every block before the
focused one in Chromium: 7,152 elements a Tab on a column page of 1,024
sections, 1,131 in a `main` with no Layout. Two shapes combined.

The parts layout named what stands before the parts as
`:where(main > *):has(~ [data-lf-views="parts"])`, asked of every child of
every `main`. Chromium flags each later sibling of an element it asks a
`:has(~ …)` of, and on each write to an attribute some `:has()` reads, focus
marks among them, walks back through every earlier sibling and restyles each
one a `:has()` names as its subject. `main` now states `--lf-holds-parts`,
which doesn't inherit, and its children read it with a style query; the
sidebar's `aside:has(~ …)`, asked of every `aside` in a page's content, is
gated to the sidebar's own children.

With that gone, a column page still restyled everything under `main`: `main`
is asked a `:has()` again as focus moves, and Chromium counted the column's
room, a registered `<length-percentage>` mixing a length with a percentage, as
changed on each restyle, though the property doesn't inherit. The column now
resolves the room's lengths in registered `<length>`s (`--lf-room-span`,
`--lf-room-note`) and keeps the percentage in `--lf-room-start` and
`--lf-room-end`, registered `*`; readers resolve the same expressions.

A focus move now restyles 1,014 elements on the column page at 64 and at
1,024 sections (40 ms of style down to 9 ms), and 108 in the plain `main`.
Firefox restyled a constant 405 elements a move before and after.
`test_a_has_reads_no_sibling_of_an_element_most_selectors_reach` holds the
first shape off the shipped CSS, and
`test_a_focus_move_near_a_long_pages_end_restyles_what_focus_marks` fails with
either fix reverted.
`--lf-room-start` and `--lf-room-end` only passed the room to `--lf-free-start`
and `--lf-free-end`, so the column now states it there and both registrations
go. The length the room is made of keeps one registered slot,
`--lf-room-length`: the shell less the gutters on a centred column, the end
room itself beside a sidebar. A focus move still restyles 1,014 elements on the
column page at 64 and at 1,024 sections, where main restyles 1,392 and 7,152.

The parts contract in page-layout.md now says what the layout does with
anything else in `main`: the header is the only element there besides the
parts. The browser-support note gives the room's cost at one size and names the
recheck that still applies, registering `--lf-free-start` `<length-percentage>`,
which fails the focus-move test (1,225 against 2,185 at 8 and 48 sections).
@max-sixty
max-sixty marked this pull request as ready for review November 12, 2026 09:01
@max-sixty
max-sixty merged commit 7845de2 into main Nov 12, 2026
6 checks passed
@max-sixty
max-sixty deleted the grind/selector-invalidation-2 branch November 12, 2026 09:09
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