Skip to content

Enter and leave a floating comment box without restyling the whole page - #5580

Merged
max-sixty merged 2 commits into
mainfrom
grind/comment-keydown-layout
Nov 6, 2026
Merged

max-sixty merged 2 commits into
mainfrom
grind/comment-keydown-layout

Conversation

@max-sixty

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

Copy link
Copy Markdown
Owner

Sending a comment on the generated asks page at 256 sections now paints in a
median 184 ms rather than 435, and entering or leaving a comment box that floats over
the page no longer restyles every element on it.

Cause

While focus is in a leaf-text inside a box standing in a plane, such as the comment
box on selected words or a margin card's reply, floating.js (readFloatingEditor)
sets data-lf-floating-editor on the root, and base.css zeroes the root's scroll
padding with scroll-padding: 0 !important. At rest the root's scroll-padding-top is
var(--lf-landing-top), a registered custom property, so the zero made the root stop
reading that token, and leaving the box made it read the token again.

On Leaf's pages in Chromium 156, that flip restyles every element. On the 256-section
asks page (14,257 elements), with the box open, it cost 174 ms of style at each entry
and each exit. A change between two literals, between two reads of the token, or to
and from an unregistered var() cost 0.1–0.2 ms. The review narrowed the trigger to
the root ceasing to read this token. On a plain 4,000-element page, no flip to or from
a registered token restyled more than 4 elements. On Leaf's asks page at 64 sections, main's flip
restyles 12,103 elements, and only 8 when another root property keeps reading
--lf-landing-top or the token is a constant.

The send keystroke pays it: the trace of Control+Enter in the comment box at 256
sections showed one forced style recalc of 47,805 elements (237 ms traced), read by
letGo → landingPlace → pageReading → readingBounds → shownWindow
(geometry.js), after readFloatingEditor removed the attribute as focus left the
box (Chromium's invalidation tracking names data-lf-floating-editor on HTML). The
same restyle ran when the user entered the box. It is the 255 ms of forced style and
layout under field-keys.js LEAF-TEXT.onkeydown that the first full leaf-dev bench
run of #5576 read on that page.

The flip arrived with #5417, which moved the override into base.css as !important
so that it wins the top edge again. Before that, the unlayered override lost the top
edge and changed only the literal bottom edge, which costs nothing.

Change

The zero is now a product of the same token, calc(0 * var(--lf-landing-top)), so the
root reads --lf-landing-top in both states. The computed padding in a floating editor
is still 0px on all four sides, checked in Chromium, Firefox and WebKit, and Leaf's
landings still read --lf-landing-top directly. The rule's comment in base.css says
what was measured and names the test that guards it.

render_harness.restyle_counts reads the elements each style recalc restyled from
Chromium's trace. The new test and
test_code_copy_reads_sources_before_positioning_many_controls both use it in place of
their own copies.

Evidence

Main against this branch on the asks page at 256 sections, the comment input of
leaf-dev bench run by a scratch script: drag-select the first paragraph, open the
box, type, then Control+Enter. "Painted" is painted.js's message goal from the
keystroke; CPU is the main thread's thread ticks over the keystroke and the 1.5 s after
it, less the idle rate. Eight interleaved sends each, at load 4–5:

painted ms, median (range) send CPU ms, median (range)
main 435 (403–515) 517 (494–593)
this branch 184 (179–281) 270 (262–275)

In these eight sends main paid the restyle every time (CPU 494 ms or more). In the
review's eight interleaved sends, main restyled the whole page in 6 of 8, painted median 421 ms (187–533), and this branch in 0 of 8, median
190 ms (184–309). At 64 sections, the lowest of three rounds read 152 ms painted on
main and 95 on this branch.

Firefox (review): the flip costs at most 1 ms with the box open, on main and on this
branch alike.

On the corpus,
uv run leaf-dev bench-latency --page corpus --transition comment --runs 3 --base HEAD
reads 173 ms painted on both arms; its comment follows bench-latency's open Threads
view, where the send's focus moves to the panel's reply and the root keeps the
attribute.

The send still grows with the page (95 → 184 ms painted from 64 to 256 sections). Most
of the rest is the margin's two layout passes per send, each over every row, and the
new comment's mark adds Chromium's per-frame cost of a text-decorated custom highlight.
Neither is changed here.

Test

test_entering_and_leaving_a_floating_comment_box_restyles_only_the_chrome
(tests/browser/test_chrome_contracts.py) opens the box on a 600-paragraph page, then
Tabs out of its field and clicks back in. It counts the elements each style recalc
restyled in a trace, against a control that restyles the whole page (a custom property
set on the root). With main's base.css both moves restyle the whole page (3,973 and
3,982 elements against the control's 3,807), so the test fails; on this branch it
passes. Unthrottled, 18 leaf-dev flake copies failed 18 of 18 with main's
base.css; on this branch 18 copies passed 18 of 18 at --cpu-throttle 10 and at 20
(review).

It depends on CPU speed in one direction. With main's CSS at --cpu-throttle 10 under
six concurrent copies, 2 of 18 and then 6 of 18 copies passed, so on a slow machine
main's flip isn't always expensive. It never fails falsely on this branch.

Ran: that test (before and after the helper), test_code_copy_reads_sources_before_positioning_many_controls,
test_a_compact_comment_keeps_no_room_for_send_under_a_mouse; the floating-editor,
caret-reveal and landing tests #5417 named, -k "caret or reveal or resume or floating or foot or reply_row or follows or wheel_return or grows_back" over
tests/browser/{drafts,margin,threads,navigation}, test_render_send_placement.py
and test_chrome_contracts.py (85 passed). Two of those fail the same way with main's
base.css: test_a_caret_is_drawn_at_a_lines_end_and_after_a_list_bullet[firefox_browser]
(a wait_for_function timeout) and
test_a_zoomed_floating_draft_and_sent_comment_fit_a_narrow_window (a
ResizeObserver loop window error). Pre-commit on the changed files.

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

max-sixty and others added 2 commits November 5, 2026 16:56
While focus is in a leaf-text inside a box standing in a plane, floating.js
sets data-lf-floating-editor on the root and base.css zeroes the root's
scroll padding. At rest the root's scroll-padding-top reads the registered
--lf-landing-top, and in Chromium 156 a root property that starts or stops
reading a registered custom property restyles every element: 174 ms of
style per entry or exit on the 256-section asks page of leaf-dev scaling.
The send keystroke paid it in a forced read from letGo's landing, which is
the forced style under LEAF-TEXT.onkeydown that #5576's bench run read.

The zero is now calc(0 * var(--lf-landing-top)), so the property reads the
token in both states; the computed padding stays 0px on every side in
Chromium, Firefox and WebKit.

On the asks page, a comment's send paints in 95 ms rather than 152 at 64
sections and 181 rather than 425 at 256 (send CPU 201 -> 119, 512 -> 267;
lowest of three interleaved rounds). The new test counts restyled elements
in a trace against a whole-page control: on main both moves restyle the
whole page (3,973 and 3,982 elements against the control's 3,807).

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…yle counter

Review of #5580 found the comment's engine rule broader than what
reproduces. On a plain 4,000-element page, no root flip to or from a
registered token restyled more than 4 elements; on Leaf's page, main's
plain-zero flip drops from 12,103 restyled elements to 8 when another
root property keeps reading --lf-landing-top or the token is a
constant. So base.css now says what was measured: on Leaf's pages, the
root ceasing or resuming to read that token restyled every element,
and it names the test that guards it. The test's docstring points to
base.css rather than restating the rule.

render_harness.restyle_counts reads the elements each style recalc
restyled from Chromium's trace; the new test and
test_code_copy_reads_sources_before_positioning_many_controls both use
it in place of their own copies.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@max-sixty
max-sixty marked this pull request as ready for review November 6, 2026 01:53
@max-sixty
max-sixty merged commit 227ccc2 into main Nov 6, 2026
6 checks passed
@max-sixty
max-sixty deleted the grind/comment-keydown-layout branch November 6, 2026 02:02
max-sixty added a commit that referenced this pull request Nov 6, 2026
…ails without it (#5589)

While the user writes in a floating comment box, the root drops its
scroll padding (`data-lf-floating-editor`, `browser/base.css`), because
the browser's caret reveal through a box anchored to the page applies
that padding and moves the words beneath a box already in view (#4821).
Nothing in the suite failed with that rule removed: #4833's
`test_a_card_at_the_windows_foot_grows_back_to_how_it_stood` stands its
card in the window plane, which writes no `position-anchor`, so the
reveal stops at the box there. This adds a test that does fail, and
records why the rule has no structural replacement.

- **Test.**
`test_writing_in_a_comment_box_over_the_window_foot_moves_no_words`
(`tests/browser/test_chrome_contracts.py`, Chromium and WebKit) opens a
comment box on words low in a 1512×860 window, so the box stands over
the foot band the panel states, types four letters, and asserts the page
didn't scroll. It also asserts the caret stands inside that band, so a
layout change that moves the box clear of it fails the test rather than
passing it vacuously. With the rule's declaration removed, the words
move 16 px in Chromium 156 and 14–21 px in WebKit 27.2.
- **Why the rule stays.** Chromium stops a scroll-into-view at a fixed
box only when the box has no anchor scroll adjustment (`IsFixedToFrame`,
`scroll_into_view_util.cc`); since crbug.com/469481151 an anchored fixed
box bubbles to the root and takes its padding, pinned by WPT
`css-anchor-position/scroll-to-anchored-fixed-001…006`. WebKit does the
same (`adjustForScrollByAnchor`, `LocalFrameView.cpp`). Firefox 157
reveals nothing through a fixed box. An agent tried, on a plain anchored
contenteditable and in Leaf: the field or the box as its own scroller,
`overflow`, `contain`, `overscroll-behavior`, the top layer (popover),
`position: absolute`, `scroll-margin` on the caret's text, and taking
typing in `beforeinput`. Each either still moved the page in Chromium or
WebKit, or stopped the box following its anchor. The comments in
`base.css` and `floating.js` now name WebKit and say what was rejected.
- **Engine note.** `notes/browser-support.md` records a Chromium bug
found on the way: each reveal of an anchored fixed box made before the
next frame scrolls the full amount again (three `scrollIntoView` calls
scroll 270 px where one scrolls 90; four keys in one frame 240 px where
one scrolls 60). WebKit reveals once. It is the overshoot #5417
accepted, and the note gives the check that shows when Chromium fixes
it.
- The rule's comment now names `leaf-dev bench`, which replaced
`leaf-dev scaling` in #5576.

Checked: the new test passes on both engines at the head and fails on
both with the rule's declaration removed (Chromium 2076 → 2092, WebKit
2075 → 2096, rerun independently); #5580's restyle test passes;
pre-commit on the changed files.

> _This was written by Claude Code on behalf of max-sixty_

---------

Co-authored-by: Claude Opus 5.5 <[email protected]>
max-sixty added a commit that referenced this pull request Nov 11, 2026
… row (#5818)

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:

```css
&[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:

```css
: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 (#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 #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
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