Skip to content

Stop Resolve restyling the whole page and Send restyling every margin row - #5818

Merged
max-sixty merged 2 commits into
mainfrom
grind/resolve-restyle
Nov 11, 2026
Merged

max-sixty merged 2 commits into
mainfrom
grind/resolve-restyle

Conversation

@max-sixty

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

Copy link
Copy Markdown
Owner

Resolving a thread no longer restyles every element of the page, and sending a comment no longer restyles every margin row. On the generated asks page at 1,024 sections in Chromium, Resolve's style work drops from 841 ms to 16 ms in a profiled run, and Send's from 125 ms to 39 ms.

Why

Profiling the margin on the generated asks page found two restyles whose cost grew with the page, separate from #5817's margin work:

  • Resolve forced one style recalc of 185,554 elements (744 ms) at 1,024 sections, read by landingBand in reading-regions.js's pageGeometry.
  • Send restyled every margin row, first under holdSelection's read of the selection (46 ms) and again under marginColumn (6,176 elements).

Causes

Neither restyle came from the code that read the style. Each read forced a recalc that an earlier DOM write had made large; Chromium's invalidation tracking names the writes.

Resolve. Resolve closes the card and returns focus to the thread's passage. paintFocus (focus.js) marks every ancestor of the focused element data-lf-focus-within: the paragraph, its section, the article and main. reading.css's fade beside a table's sticky row names is keyed on that mark:

&[data-lf-more-before]:not([data-lf-focus-within]) > * > tr > :first-child::after

Its last compound names no type, class, id or attribute, so when the mark changes on an element, Chromium restyles every element under it. With main marked, that is the whole page. landingBand's getComputedStyle(root) was the next style read in the frame, so it paid the recalc. The data-lf-floating-editor attribute and the calc(0 * var(--lf-landing-top)) padding from #5580 play no part. With the rule dropped, the same mark change restyled 980 elements on a 40-section page instead of 7,185.

That residual has a second cause, on pages whose main carries a column Layout (the asks page has none, so the bench doesn't show it). Every restyle of main restyled the boxes under it, even a restyle that changed nothing: 246 elements at 10 sections, and 6 with the room set to a constant. The column's room, --lf-free-start and --lf-free-end in layouts.css, is a length less a percentage registered as <length-percentage>, and Chromium 158 counts that value as changed each time it is recomputed. A plain page reproduces it: a no-op restyle of a parent declaring --free: calc(10px - 50%) restyles all 200 children; with a plain length, a plain percentage or syntax: "*", it restyles the parent alone. Focus marking main restyles it, so on a column page Resolve still restyled the whole column.

Send. The frame-edge rules in reading.css are bare compounds:

:nth-child(1 of :not(.lf-ui, [data-lf-gen]))
:nth-last-child(1 of :not(.lf-ui, [data-lf-gen]))

Chromium matches them against every element, whether or not the frame's container query holds. Once it has evaluated :nth-child() for a parent's children, it restyles all of them whenever a child is inserted or removed. Send removes the comment's row from the margin toolbar and inserts the thread's row, so every row in the toolbar was restyled, and Resolve's removal of the row did the same. holdSelection's read of rangeCount was the first read after the insertion.

Fix

All three are CSS changes at the rule that owns the invalidation:

  • reading.css: the row-name fade and its data-lf-more-before background rule end in :where(th, td):first-child, so a mark on an ancestor restyles only first cells. :where keeps the rules' specificity.
  • layouts.css: the column computes its room in two new non-inheriting <length-percentage> properties, --lf-room-start and --lf-room-end, so every unit in it (the shell's cqi, and a page's --col, --col-pad or --aside in ch, em or rem) resolves on the column, as on main. It passes the result to its children in --lf-free-*, now registered syntax: "*", so children inherit a fixed expression of lengths and a percentage that Chromium compares by value, and each reader resolves the percentage against its own containing block, as before. A non-inheriting value that Chromium counts as changed restyles only the column. notes/browser-support.md records the engine gap and how to recheck it. The comment on the reset rule after it (fix: Stop the column's room lengths inheriting past the blocks that read them #5609) loses its performance reason, which was the same gap, and keeps its job: without the reset, a flow inside a plain wrapper breaks out of the wrapper ([168, 888] becomes [24, 1416]).
  • reading.css: the frame-edge rules check :where(:not(.lf-ui, [data-lf-gen])) before :nth-child(). Chromium stops at the first failing part of the compound, so it never evaluates the position for chrome, and the toolbar is no longer marked. :where keeps the specificity: a plain :not() raised it and beat a margin-bottom: -15px on the pr-walkthrough diagram, which leaf-dev stills caught.

landingBand and holdSelection are unchanged. With the invalidations fixed, landingBand's read restyles 25 elements at 1,024 sections, and holdSelection's read no longer appears among the forced recalcs.

Before and after

uv run leaf-dev bench asks --sizes 256,1024 --input resolve --input comment --rounds 2 --base origin/main at the head with the review fix (96f89a5), interleaved, lowest of two rounds, load average 4.0 at the start and 3.1 at the end:

reading 256 main 256 this 1,024 main 1,024 this
resolve, main-thread CPU ms 321.9 155.0 1,162.9 480.7
resolve, input to painted ms 243.9 134.2 900.9 244.2
resolve, input to server's reading shown ms 305.7 134.2 1,131.8 461.9
comment send, main-thread CPU ms 153.0 143.6 478.7 440.0
comment send, input to server's reading shown ms 158.7 154.5 497.3 453.8

The first commit's three-round run read the same within a few percent (resolve painted 897.5 → 244.0 ms at 1,024 sections).

At 1,024 sections, the forced style and layout in the long frames after Resolve fell from 644 ms to 6 ms. Send's gain is the row restyle alone (about 40 ms); the rest of its time is script the profile attributes elsewhere, and both inputs still take over 100 ms to show the server's reading at 1,024 sections.

A profiled run of head at 1,024 sections, against the profile that found the problem:

before after
Resolve, style ms (forced) 841 (797) 16 (14)
Resolve, largest forced recalc 185,554 elements, landingBand 222 elements, card close
Send, style ms (forced) 125 (78) 39 (35)
Send, recalc under marginColumn 6,176 elements 38 elements
holdSelection self time 45 ms not in the top functions

The column-Layout cause doesn't show in the bench, whose page has no Layout. The new resolve test reads it: with --lf-free-* registered <length-percentage> again, a page of 24 sections restyles 120 more elements than one of 4.

Tests

Two tests in tests/browser/test_chrome_contracts.py, beside #5580's floating-editor test, count the elements Chromium restyles for one input on pages of 4 and 24 sections of questions in a column page's article, and fail if the larger page restyles 10 or more elements more. They run with reduced motion, so the card doesn't ease in over a host-dependent number of frames, and without the shift watch, whose cameras inserted into the chrome are restyles of their own. With all fixes the counts are equal at both sizes.

test with the fixes (4 → 24) fails without
test_sending_a_comment_restyles_only_the_rows_it_changes 2,147 → 2,147 the frame-edge guard: 2,150 → 2,170
test_resolving_a_thread_restyles_what_focus_marks 1,178 → 1,178 the row-name types: 1,894 → 4,294; --lf-free-* registered <length-percentage>: 1,196 → 1,316; the frame-edge guard: 1,181 → 1,201

A third test, test_a_column_stated_in_font_units_gives_every_breakout_the_same_room in tests/browser/test_render_pages.py, holds the room's units to the column. On a column page and a sidebar page with :root { --col: 50ch; --col-pad: 2em; } at 1600px, three blocks that break out, in the page's font, a 10px monospace font and 24px, must reach the same edges, and the page must not scroll sideways. It passes in Chromium, Firefox and WebKit, and failed in all six cases on this branch's first commit, which registered the room * without resolving it on the column: the column page's blocks stood at [20, 1580] and [48, 1552] beside [34, 1566], and the sidebar page's monospace block ran to 1738px.

What I checked

  • The two restyle tests, and Enter and leave a floating comment box without restyling the whole page #5580's floating-editor restyle test, after the review fix as well.
  • 92 browser tests selected by -k "resolv or landing or floating_editor or floating_comment or restyle": 90 pass. test_a_resolve_s_notice_stands_for_that_resolve_alone fails here with and without this change (main's CSS restored, by node id), so it is inherited.
  • 56 tests in test_render_pages.py that read the column's room (breakouts, notes, sidebar, tracks and the new font-unit test, -k "breakout or room or note or sidebar or available or wide or column or track or font_units"), on Chromium, Firefox and WebKit, and the two sticky row-name tests: all 58 pass.
  • On the first commit, leaf-dev stills for breakout-captions-scrolled, table-scrolled-sideways, release-notes, pr-walkthrough, breakout-table-caption, sidebar-contents, gallery-wide-passage and proxy-note-paragraph: 0 px changed. table-scrolled-sideways lists one DOM difference, the comment box's resting label; leaf-dev probe shows head without this change lacks it too, so it is capture timing.
  • pnpm run test:runtime (913 pass), and pre-commit on the changed files.

An independent review re-ran leaf-dev stills on this head: 30 states in Chromium and WebKit, and 26 in Firefox, leaving out four that never settle in Firefox on main either. No state shows a layout change. One Firefox state, github-discoveries, shows glyph-edge raster differences (216 px, at most 26 of 255 levels) with every box and computed style identical.

Not checked: Firefox and WebKit restyle counts (both fixes are Chromium invalidation behavior; the layout tests above hold the values in all three); the broad suite, which CI runs.

Follow-up, not in this PR

Each focus move still restyles about 6 elements for every block before the focused one, on a page whose blocks are main's children: 92, 272 and 992 elements at 10, 40 and 160 blocks. Thirty :has() rules in 12 files name a focus mark (data-lf-focus, data-lf-focus-visible or data-lf-focus-within) in their argument, so Chromium starts a :has() invalidation walk on every focus move, and broad :has() subjects, such as :has(> [data-lf-track]) in layouts.css among others, make that walk visit the preceding blocks. Fixing it means keeping focus marks out of :has() arguments or narrowing those subjects, a change across many rules.

This was written by Claude Code on behalf of max-sixty

https://claude.ai/code/session_01GQAnFBpRfi8Gh5cgPofNzs

… row

Resolving a thread from its card restyled every element of the page in Chromium
(744 ms, 185,554 elements at 1,024 sections of the generated asks page), and Send
restyled every margin row (46 ms at 1,024). In each case a style rule's
invalidation reached far past what the input changed.

- Resolve returns focus to the thread's passage, and focus marks every ancestor
  `data-lf-focus-within`, `main` included. reading.css's sticky row-name fade,
  `…:not([data-lf-focus-within]) > * > tr > :first-child::after`, ends in a
  compound that names no type, so Chromium restyled everything under each element
  the mark landed on. `landingBand` reading the root's computed style was only the
  first read after the marks changed. The rule, and its `data-lf-more-before`
  sibling, now name `:where(th, td)`.
- On a page with a column Layout, any restyle of `main` restyled every box under
  it, because Chromium 158 counts the column's room (`--lf-free-start` and kin, a
  length less a percentage registered `<length-percentage>`) as changed each time
  it is recomputed. The room is registered `*`, and `--lf-shell-width` carries the
  shell's width into it as a length; notes/browser-support.md records the gap.
- The frame-edge rules, `:nth-child(1 of :not(.lf-ui, [data-lf-gen]))` and its
  `:nth-last-child` twin, were matched against every element, so Chromium marked
  the margin toolbar as having positional children and restyled all rows whenever
  one was inserted or removed. A weightless `:where(:not(.lf-ui, [data-lf-gen]))`
  before the position check keeps it from evaluating `:nth-child` on chrome.

test_sending_a_comment_restyles_only_the_rows_it_changes and
test_resolving_a_thread_restyles_what_focus_marks count the elements Chromium
restyles for each input at 4 and 24 sections, and fail without their fixes.

Claude-Session: https://claude.ai/code/session_01GQAnFBpRfi8Gh5cgPofNzs
leaf-agent
leaf-agent previously approved these changes Nov 11, 2026
… edges

Registered `*`, the room carried the column's tokens down unresolved, so each
breakout resolved a page's `--col`, `--col-pad` or `--aside` in its own font: at
1600px, `--col-pad: 2em` put blocks at [20, 1580] and [48, 1552] instead of
[34, 1566], and on a sidebar page with `--col: 50ch` a 10px monospace block ran
to 1738px and scrolled the page sideways, in all three engines.

The column now computes its room in two non-inheriting `<length-percentage>`
properties, `--lf-room-start` and `--lf-room-end`, where every unit resolves on
the column, and passes the result to its children in `--lf-free-*`, still
registered `*` so Chromium compares it by value. The notes column and the sidebar
column use `100cqi` again, and the sidebar column no longer sets
`--lf-shell-width`, which the render check reads as a column with room.
test_a_column_stated_in_font_units_gives_every_breakout_the_same_room covers
both Layouts in three engines.

The reset rule after it keeps its job, keeping a flow inside a plain wrapper
from breaking out, and loses its performance reason, which was the same
Chromium gap. The notes entry names the Playwright build, and reading.css counts
attributes among the features that narrow an invalidation.

Claude-Session: https://claude.ai/code/session_01GQAnFBpRfi8Gh5cgPofNzs

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

The description's Fix bullet for layouts.css still describes the first commit's approach, and merging squashes with this body as main's commit message. At the head, 100cqi still appears in the room expressions: the sidebar's --lf-room-end uses it directly, and the column's --lf-shell-width: 100cqi is unchanged. The sidebar column doesn't set --lf-shell-width. The bullet also leaves out the actual mechanism. The column computes its room in the new non-inheriting --lf-room-start and --lf-room-end, registered <length-percentage> so that cqi, em and ch resolve on the column, and only --lf-free-* are registered *. The Tests section likewise doesn't mention test_a_column_stated_in_font_units_gives_every_breakout_the_same_room, which guards that choice. Please rewrite that bullet and the Tests section to match 96f89a5 before this merges.

The code itself looks right. I ran the two new restyle tests, #5580's floating-editor test, the font-units test and test_a_breakout_grows_only_from_a_holder_that_spans_the_column in all three engines, and they pass at the head.

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