Repository navigation
perf(Switch): read no computed style at mount - #13480
Conversation
A Switch read computed styles when its touch controller attached: its writing direction under any pointer, and under a coarse pointer also a theme's thumb size and its fade colour. Each read makes the browser compute styles inside the task that mounts the switch, such as a dialog's open. The new cases count those reads at attach. On main a finger mount reads three times and a mouse mount once, so both cases fail. Three more cases guard what the reads were for. The first change moves the thumb toward the inline end in both directions. It takes the theme's circle for the state the switch attached in. The fade layer's colour is painted when its first motion starts, after a value change or a tap, in both states.
The touch controller now reads each style at its first use, not when it attaches. The writing direction is read when a value change or a drag first needs it, then kept until the next attach. A theme's thumb size for the state the switch attached in is read at the first value change before any press. The track's and the thumb's class and data attributes are recorded at attach, a DOM read with no style read. The first change reads the size in those states and puts them back in the same task. The fade colour is no longer painted at attach. The fade layer is clear at rest, and its colour is painted before each of its motions. No API change.
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
|
AI review status for this pull request.
|
PR Analysis Report🔎 Preview ·
|
| Component | Size | LoC | Visual · A11y · RTL |
|---|---|---|---|
| Switch · Storybook | 111.5 kB (+113 B) · 10.5 kB CSS (0) | 1,815 (+31) | ✓ · ✓ · ✓ |
Package size: @astryxdesign/core 7.96 MB packed (+1.1 kB); no new dependencies.
Touched components checked; for the full suites, add ci:run-visual for Visual, ci:run-rtl for RTL.
Generated by PR Enrichment workflow | View full report
There was a problem hiding this comment.
Approved pending CI: CI has not run on this head yet. This is a draft from a fork, so CI runs once it is marked ready and a maintainer adds the ci:run label.
The three attach-time style reads move to their first use, and right-to-left travel, the theme circle and the colour fade stay as they were. The new red-first cases fail on main and pass here, and each guard fails when its read is broken.
- Non-blocking:
TrackStatenow also holds the thumb's state (switchFill.ts:18); a neutral name such asElementStatewould make its JSDoc unnecessary.
Claims and files checked
Review mode: full; reason: no-earlier-review
- Changeset: Switch no longer reads styles when it mounts. Verified: the attach-count cases in
useSwitchTouch.test.tssee 0 reads at the head. - Changeset: it read its direction at mount, and under a finger also the thumb size and the fade colour. Verified: the same cases see 3 and 1 reads on the base.
- Changeset: each value is now read at its first use, at the first press or value change. Verified:
useSwitchTouch.ts:424-430,:438-451,:834-838,:868-878. - Changeset: dragging, right-to-left, theme thumb sizes and the colour fade work as before. Verified: the RTL, theme-circle and fade cases pass, and the Switch, SegmentedControl and gestures suites pass (1,175 tests).
- Description: a mount read styles 3 times under a finger and once under a mouse or a pen. Verified by the red-first run.
- Description: the fade read swaps the track's state, so a finger mount paid two recalculations. Verified:
switchFill.ts:114-120swaps and restores the track. - Description: the direction helper asks callers to read lazily, and the boxes were already read at the first press. Verified in
isRtlElement.tsand the attach comment on the base. - Description: the direction is a getter cached until the next attach. Verified:
useSwitchTouch.ts:301,:424-430. - Description: attach records the track's and thumb's class and data attributes, and the first change reads the size in those states and restores them. Verified:
useSwitchTouch.ts:314-317,:438-451,switchFill.ts:75-111. - Description: the attach-time fade paint is removed, and
switchFill.tsgainsstateOf. Verified:useSwitchTouch.ts:336-337,switchFill.ts:61-69. - Description: no public API change. Verified: the API report lists 0 rows for Core, build and the themes.
- Description: it composes with #11450, whose touch part calls
useSwitchTouchunchanged. Verified: that pull request does not touch these two modules. - Description: the test commit fails the two counts and passes the three guards, and each guard fails under a mutation of its read. Verified on an equivalent tree and with three mutations.
- Description: 17 of 17, and 1,175 tests in 48 files, pass. Verified.
- Description: the thumb's travel is the track's width less its height for any circle. Verified: the
switchTouch.tstravel formula. - Description: the Chromium timings and the preflight run. Not reproduced: they are the author's local runs, and no claim requires them.
.changeset/switch-no-style-read-at-mount.md: clear.packages/core/src/Switch/switchFill.ts: clear; one non-blocking naming suggestion.packages/core/src/Switch/useSwitchTouch.ts: clear.packages/core/src/Switch/useSwitchTouch.test.ts: clear.
| */ | ||
|
|
||
| /** A track state: its class name and its data attributes. */ | ||
| /** A state of the track or the thumb: its class name and data attributes. */ |
There was a problem hiding this comment.
Non-blocking: this JSDoc now explains that TrackState also holds the thumb's state. A neutral name such as ElementState (with sameTrackState and trackStateOf to match) would say that without the comment.
|
/approve-spec e825408 |
cixzhang
left a comment
There was a problem hiding this comment.
Engineering-owner approval at this exact head (e825408), posted by Cindy's agent (agent-made, not a human review) under her standing instruction. CodeBunny approved this head, CI is green at this head, and Specs records are current.
Brings in #13480 (Switch reads no computed style at mount) before the device set runs, so its Switch tap and thumb-drag cells cover both changes.
User impact
A
Switchread computed styles when it mounted. Under any pointer it read its writing direction. Under a coarse pointer it also read a theme's thumb size and its fade colour. A read forces any pending style recalculation, inside the task that mounts the switch. The fade colour read swaps the track's state, so a finger mount in visible content forced two recalculations per switch.In a local Chromium test, a tap that mounts 40 switches reaches its first frame in 77 ms instead of 106 ms. Under a mouse the saving is about 5%, because the next script read then pays the recalculation (Testing).
Problem and solution fit
Each value is now read at its first use. The direction is read when a change or a drag needs it. The thumb size is read at the first change. Each fade motion already paints its colour.
Expected behavior and authority
hooks/isRtlElement.tsasks callers to read the direction lazily.useSwitchTouch.tsalready reads the track's boxes at the first press: "a read at mount forces a layout for every switch on the page".Smallest restoration
useSwitchTouch.ts: the direction is a getter, cached until the next attach. At attach, the track's and thumb's class and data attributes are recorded. The first change reads the theme's thumb size in those states throughreadInState, which restores them in the same task. The attach-time fade paint is removed.switchFill.tsgainsstateOf.useSwitchTouchunchanged.Evidence
useSwitchTouch.test.ts, red-first in their own commit. Each guard fails under a mutation of its read.Scope
Components: Switch
Testing
All runs are test runs.
At 0931b5c, the test commit: the two read counts fail (3 and 1). The three guards pass.
At e825408:
useSwitchTouch.test.tspasses 17 of 17. Switch, SegmentedControl and gestures pass 1,175 tests in 48 files.Mutations at e825408, each failing its guard:
readInState, or never, fails the theme-circle case;swapFadeandshowWellfails the fade-colour case.The theme-circle case checks the thumb's scale. The thumb's travel is the track's width less its height for any circle, so the travel cannot tell the circle.
Chromium 149, 1x CPU, local Storybook builds of the test commit (main's engine) and of the fix. Each row is the median of 10 runs, the builds alternating. Two local probe stories, not part of this change:
Dialogwith N switches. A layout effect opens it in a synchronous second commit.Under a mouse, the switch's one forced recalculation moves to the next script read. In A that is the
Dialog's own reads at open, which this change does not touch.ASTRYX_STRICT_LINT=1 pnpm preflight --browserat e825408, from a clean tree: all 11 steps pass, and it reports "ready to flip".