Repository navigation
Stop Resolve restyling the whole page and Send restyling every margin row - #5818
Conversation
… 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
… 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
left a comment
There was a problem hiding this comment.
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.
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:
landingBandinreading-regions.js'spageGeometry.holdSelection's read of the selection (46 ms) and again undermarginColumn(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 elementdata-lf-focus-within: the paragraph, its section, the article andmain. reading.css's fade beside a table's sticky row names is keyed on that mark: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
mainmarked, that is the whole page.landingBand'sgetComputedStyle(root)was the next style read in the frame, so it paid the recalc. Thedata-lf-floating-editorattribute and thecalc(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
maincarries a column Layout (the asks page has none, so the bench doesn't show it). Every restyle ofmainrestyled 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-startand--lf-free-endin 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 orsyntax: "*", it restyles the parent alone. Focus markingmainrestyles it, so on a column page Resolve still restyled the whole column.Send. The frame-edge rules in reading.css are bare compounds:
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 ofrangeCountwas the first read after the insertion.Fix
All three are CSS changes at the rule that owns the invalidation:
data-lf-more-beforebackground rule end in:where(th, td):first-child, so a mark on an ancestor restyles only first cells.:wherekeeps the rules' specificity.<length-percentage>properties,--lf-room-startand--lf-room-end, so every unit in it (the shell'scqi, and a page's--col,--col-pador--asideinch,emorrem) resolves on the column, as on main. It passes the result to its children in--lf-free-*, now registeredsyntax: "*", 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]).: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.:wherekeeps the specificity: a plain:not()raised it and beat amargin-bottom: -15pxon the pr-walkthrough diagram, whichleaf-dev stillscaught.landingBandandholdSelectionare unchanged. With the invalidations fixed,landingBand's read restyles 25 elements at 1,024 sections, andholdSelection'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/mainat 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: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:
landingBandmarginColumnholdSelectionself timeThe 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_sending_a_comment_restyles_only_the_rows_it_changestest_resolving_a_thread_restyles_what_focus_marks--lf-free-*registered<length-percentage>: 1,196 → 1,316; the frame-edge guard: 1,181 → 1,201A third test,
test_a_column_stated_in_font_units_gives_every_breakout_the_same_roomintests/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
-k "resolv or landing or floating_editor or floating_comment or restyle": 90 pass.test_a_resolve_s_notice_stands_for_that_resolve_alonefails here with and without this change (main's CSS restored, by node id), so it is inherited.test_render_pages.pythat 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.leaf-dev stillsforbreakout-captions-scrolled,table-scrolled-sideways,release-notes,pr-walkthrough,breakout-table-caption,sidebar-contents,gallery-wide-passageandproxy-note-paragraph: 0 px changed.table-scrolled-sidewayslists one DOM difference, the comment box's resting label;leaf-dev probeshows 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 stillson 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-visibleordata-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.https://claude.ai/code/session_01GQAnFBpRfi8Gh5cgPofNzs