Skip to content

Style a long page's rows in a quarter of the time in Chromium by keying every rule - #6223

Merged
max-sixty merged 7 commits into
mainfrom
grind/style
Nov 17, 2026
Merged

max-sixty merged 7 commits into
mainfrom
grind/style

Conversation

@max-sixty

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

Copy link
Copy Markdown
Owner

Chromium files each style rule under one id, class, attribute or type its subject names, and skips a rule at once for an element whose ancestors lack a key an ancestor compound names. It matches every other rule against every element it restyles. A list in the subject, as :is(.a, .b), :where(p, li) or a nested & under two selectors, names no key, and Leaf's sheets held about 120 such rules, so each plain row of a long document was matched in full against 141 rules. Now each of those rules names a key, and a plain row is matched against 13.

Change

  • Each rule whose subject was keyed only through a list names a key. A list becomes separate selectors (.lf-btn, .lf-icon-action). Where a lighter member must keep the list's weight, the key stands first at no weight before the list (:where(.lf-reaction):is(.lf-reaction, .lf-response-face[aria-pressed])); a nested rule under a list names one (&:where(.layout-flow):has(…)); and where an ancestor can name one, it does (:where(svg) :where(:not(foreignObject *)):where([data-lf-aimed], …)). Where nothing else in Leaf's sheets sets a different value for a rule's properties on a lighter member, the list is split plainly and that member weighs its own: leaf-text beside .lf-msg-body, lf-parts, lf-tabs and lf-tab, the coarse-pointer fields, a draft body's edge children and the thread separators. A page's own CSS is unlayered and outranks all of them either way.
  • build/styles.mjs writes the Web Awesome rules it maps onto Leaf's faces and a page's controls the same way: one selector for each class of a face's list (spread), and each compound in a :where() of its own (weightless). webawesome-native.css and page-controls.css are regenerated.
  • browser/AGENTS.md, "A rule costs only the elements it could style", states the rule, and test_a_rule_s_subject_names_a_key finds a subject keyed only through a list, :host() lists included, with no keyed ancestor, naming the file it stands in: 125 on main, none here.
  • notes/browser-support.md, "A style read lays out a size container's contents", records with a recheck what the measurement turned up: once a size query reads body's lf-shell container (@container lf-shell (width < 640px) in layouts.css and tabs.css), a style read inside body in Chromium lays out everything out of date there, a closed tab's panel included. Firefox does it for the container alone.

Chromium: style 76 → 20 ms per 20,000 rows

Chromium 158, main (b2ab51e) beside this branch in one run:

Reading main here no Leaf sheets
20,000 plain rows in a closed tab, a forced read of the last row's style 108 ms (115 after merging main) 50 ms (52) 6.5 ms
the same, traced: style recalc / the layout the read forces 76 / 49 ms 20 / 46 ms
rules matched in full per row (selector stats) 141 13
bench documents --input matches CPU at 64 / 256 620 / 2,266 ms 486 / 1,798 ms

An independent run read the closed-tab read at 101.5 → 49.2 ms and matches at 256 at 2,253 → 1,768 ms, faster in every round. What remains of the read is mostly the forced layout above, which rewriting the two lf-shell size queries might lift in Chromium (untested on a Leaf page).

Firefox: neutral

Firefox already files a list under each member's key: in the independent review, 120 list rules took it 5 ms written as lists and 5 ms split, where Chromium took 38 and 6, and restyling 20,000 shown rows read 44 ms on both arms, 19 and 18 ms in a closed tab. bench documents load at 256 reads within noise: over 8 interleaved rounds its median was 5% lower here and this branch read higher in 4 of them, and a Gecko profile of each arm styles the same 93,000 or so elements.

No look change

  • The computed style of every element of nine bench pages (asks, asktabs, boards, cards, document, documents, options, prose, rows), shadow trees and drawn ::before/::after included, is the same on main and here, in Chromium and Firefox; so is that of 15 further page and condition pairs (dark, phone and touch, Firefox, hover, Tab) in an independent review.
  • uv run leaf-dev stills over all 290 states: 253 unchanged and 4 failing alike on both arms. The other 33 differ in live content or timing: the version block naming this checkout's commit, a message's clock minute or a version saved during the capture, a preview racing the state's scripted focus, and resolved margins and loading states that leaf-dev probe --state reads equal on both arms at rest. After the review's changes, 16 states covering marked drawings, drafts, the margin and Page Map, thread outlets, disclosures, worktrees and traces read 14 unchanged and 2 with those same timing differences, and the nine pages above read no computed difference again.

Tests

uv run pytest tests/model/test_interact_layer.py tests/model/test_distribution.py (191 passed); -k "ring or bracket or mark or shape or stroke or outline or reaction or disclosure or veil" over tests/browser/controls/test_rings.py, test_render_aim.py, test_render_reactions.py, test_render_drawing.py and navigation/test_comment_marks.py (125 passed); pnpm run check:browser and check:tools; pre-commit on the changed files. Not run: the whole suite, journeys, WebKit.

Part of #5194.

…ng every rule

Chromium files each style rule under one id, class, attribute or type its
subject names, and tests every rule filed under none against every element it
restyles, unless a compound before the subject names a key its ancestor
filter can reject on. A list in the subject, as `:is(.a, .b)`, `:where(p, li)`
or a nested `&` under two selectors, names none. Leaf's sheets held about 120
such rules, so a plain row of a long document was matched in full against 141
rules on every restyle; now it is matched against 13.

Each such rule now names its key. A list whose members weigh alike becomes
separate selectors; where they weigh differently, the key stands first at no
weight before the unchanged list (`:where(.lf-reaction):is(.lf-reaction,
.lf-response-face[aria-pressed])`), which keeps the rule's weight; a nested
rule under a list names one (`&:where(.layout-flow):has(...)`).
build/styles.mjs writes the Web Awesome rules it maps onto Leaf's faces the
same way (`spread`, and `weightless` wrapping each compound on its own), and
webawesome-native.css is regenerated. browser/AGENTS.md, "A rule's subject
names a key", states the rule, and test_a_rule_s_subject_names_a_key finds a
subject keyed only through a list with no keyed ancestor (104 on main).
browser/AGENTS.md also records that in Chromium a forced style read lays out
whatever in `body` is out of date, a closed tab's panel included, since `body`
is a size container and Leaf's style queries make Chromium finish style there
during layout.

Chromium 158, 20,000 plain rows in a closed tab, a forced read of the last
row's style: 108 ms on main, 50 ms here (6.5 ms with no Leaf sheets); traced,
style recalc 76 -> 20 ms and the layout the read forces 49 -> 46 ms.
`bench documents --input matches` CPU at 256: 2,266 -> 1,798 ms. Firefox
restyles 20,000 shown rows in 48 ms rather than 60. The computed style of
every element of nine bench pages, shadow trees and pseudo-elements included,
is unchanged in Chromium and Firefox.

Part of #4781.
…in body

The evidence and the recheck live in notes/browser-support.md; browser/AGENTS.md
keeps the rule a runtime author relies on and points there.

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

Four of the splits change a selector's weight, so "the rules match the same elements at the same weight" doesn't hold for them. Each puts a type selector next to a class or an attribute selector as separate selectors, which drops the type member to (0,0,1). That breaks the new browser/AGENTS.md rule, "Where the members weigh differently, name the key at no weight before the list":

  • reading.css: .lf-msg-body, leaf-text. leaf-text drops from (0,1,0).
  • surfaces.css, the coarse-pointer font-size … !important: textarea, leaf-text and select drop from (0,1,1).
  • tabs.css: lf-tabs, lf-tab, .lf-tabstrip. lf-tabs and lf-tab drop from (0,1,0).
  • tabs.css: lf-tabs[list="side"], lf-parts. lf-parts drops from (0,1,1).

I found no other Leaf rule that sets these properties on those elements, which explains why the computed-style comparison came out identical. But Leaf's sheets are unlayered. An author rule such as lf-tabs { scroll-padding-block-end: 0 } or leaf-text { --lf-quote-inset: 4px } now ties Leaf's rule and wins on order where it used to lose. The suggestions keep each weight with the PR's own :where(key):is(list) form.

Comment thread browser/styles/components/reading.css Resolved
Comment thread browser/styles/components/surfaces.css Resolved
Comment thread packages/default/styles/tabs.css Resolved
Comment thread packages/default/styles/tabs.css Resolved
# Conflicts:
#	browser/styles/components/controls.css
#	browser/styles/control-face.css
#	browser/styles/vendor/webawesome-native.css
#	tests/model/test_interact_layer.py
…en the key test

The browser-support note now says what makes a style read lay out a size container's
contents: in Chromium a size query evaluated against the container, such as
`@container lf-shell (width < 640px)`, whether it matches or not, and not a style
query; in Firefox the container alone. Its recheck reproduces on a plain page.

The SVG mark rules give `svg` an ancestor compound of its own, so the five marks stay
one list at no weight and Chromium's ancestor filter passes over them outside a
drawing; the margin entry rules split their ancestor instead of their subject. The
thread separators and a draft's edge trims split plainly, since nothing else in Leaf's
sheets sets their properties on the lighter members.

test_a_rule_s_subject_names_a_key reads each composed file once, naming the file a
rule stands in, counts `:host()` as a key, and finds a list nested in a one-selector
`:where()`, as the generated sheets wrote them: 125 findings on main, none here.
browser/AGENTS.md opens the section with its goal, says the rule is Chromium's, and
leaves the figure to the test's docstring.
…omium's filter rejects it

Its ancestor `:is(.lf-margin-face, .lf-page-map-action)[data-lf-turn="user"]`
left the filter only `[data-lf-turn]`, whose hash on a Leaf page's rows met the bits
the page's own ancestors set, so Chromium matched the rule in full on every row (14
rules a plain row instead of 13, selector stats, Chromium 158). Each class now stands
beside the attribute. The key test can't see a Bloom filter's collisions, so its
docstring names selector stats as the measure.
@max-sixty
max-sixty marked this pull request as ready for review November 17, 2026 01:30
leaf-agent
leaf-agent previously approved these changes Nov 17, 2026
@max-sixty max-sixty closed this Nov 17, 2026
@max-sixty max-sixty reopened this Nov 17, 2026
# Conflicts:
#	packages/playground/defaults.css
…e it after this branch

Main's native-form playground (#6231) added
`:where(.lf-playground-controls-region > aside :is(input…, select, textarea))`, whose
list left Chromium no key. The region and its aside now stand as an ancestor of their
own, which Chromium's filter rejects on every row outside one (13 rules matched in full
per plain row, selector stats). It matches the same elements at the same zero weight.
@max-sixty
max-sixty merged commit a16430a into main Nov 17, 2026
5 checks passed
@max-sixty
max-sixty deleted the grind/style branch November 17, 2026 02:04
max-sixty added a commit that referenced this pull request Nov 17, 2026
#6223 split each rule whose subject was keyed only through a list, so Chromium files it
under one key. The flow rules this branch writes named their subjects as
`:is(section, article, details, [data-lf-flow]):where(:not(…))`, the unkeyed form. Each
now names its kind as a selector of its own (`section:where(:not(…))`, …,
`[data-lf-flow]`), a rule nested under them names its kind again at no weight
(`&:where(section):dir(ltr)`), and lists stay only where they cost nothing: the reset's
`:not()`, ancestors and `:has()` arguments. `test_a_rule_s_subject_names_a_key` passes.

The Layout exception reads the `layout-` prefix (`[class^="layout-"]`,
`[class*=" layout-"]`), as page check and the authoring reading already read a Layout
class, rather than listing the five classes three times over; it keeps a section
carrying `layout-tiles` or `layout-track` a box, as the previous commit's list did.
Weights are unchanged where two rules meet: the reset still weighs a class, and a
nested rule its list's heaviest kind. Probed in Chromium, Firefox and WebKit, every
breakout lands where it did before the merge.

> _This was written by Claude Code on behalf of max-sixty_
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