Skip to content

perf(Switch): read no computed style at mount - #13480

Merged
cixzhang merged 2 commits into
meta:mainfrom
vjeux:perf/switch-no-style-read-at-mount
Nov 23, 2026
Merged

cixzhang merged 2 commits into
meta:mainfrom
vjeux:perf/switch-no-style-read-at-mount

Conversation

@vjeux

@vjeux vjeux commented Nov 21, 2026

Copy link
Copy Markdown
Contributor

User impact

A Switch read 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

  • Why it happens: the touch controller read three values when it attached, before anything needed them.
  • Why it harms: a read pays any pending style recalculation in that task, and the fade read invalidates styles again.
  • Why it matters: a page of switches shows later, mostly on a phone.

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.ts asks callers to read the direction lazily.
  • useSwitchTouch.ts already 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 through readInState, which restores them in the same task. The attach-time fade paint is removed.
  • switchFill.ts gains stateOf.
  • No public API change. It composes with perf(core): SegmentedControl, Switch and BottomSheet load the touch animator after first paint #11450, whose lazy touch part calls useSwitchTouch unchanged.

Evidence

  • Reproduction before the change: a mount reads computed styles 3 times under a finger, and once under a mouse or a pen.
  • Result after the change: no style read at mount.
  • Representative unchanged path: the first change travels toward the inline end in both directions, with the theme's attached circle and the right fade colour.
  • Regression test or other mutation-sensitive proof: the "style reads" cases in useSwitchTouch.test.ts, red-first in their own commit. Each guard fails under a mutation of its read.

Scope

Components: Switch

  • One defect is restored.
  • Separable contradictory or unsettled tagalongs were removed or split.
  • No new public API, visual representation, default, or interaction model is hidden under the bug-fix label.
  • Public text and artifacts contain no internal Meta context.

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.ts passes 17 of 17. Switch, SegmentedControl and gestures pass 1,175 tests in 48 files.

  • Mutations at e825408, each failing its guard:

    • the direction forced to left-to-right fails the right-to-left travel case;
    • the thumb size read without readInState, or never, fails the theme-circle case;
    • no fade paint in swapFade and showWell fails 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:

    • A: the press restyles 1,500 rows and mounts a closed Dialog with N switches. A layout effect opens it in a synchronous second commit.
    • B: the press mounts N switches into content that is already visible.
    • A finger is a coarse-pointer touch context with a tap.
    Story Pointer N Press to first frame, main / fix Forced style recalculations in the press task, main / fix
    B finger 10 43.0 / 35.3 ms 21 / 1
    B finger 40 105.8 / 77.4 ms 81 / 1
    A finger 10 82.0 / 74.7 ms 17 / 6
    A finger 40 169.4 / 145.5 ms 47 / 6
    B mouse 10 18.8 / 17.6 ms 2 / 2
    B mouse 40 31.5 / 30.0 ms 2 / 2
    A mouse 10 52.9 / 50.1 ms 7 / 6
    A mouse 40 73.7 / 70.1 ms 7 / 6

    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 --browser at e825408, from a clean tree: all 11 steps pass, and it reports "ready to flip".

    • Vitest passes 20,920 tests in the ui project and 27,234 in the node project.
    • Playwright passes 123 accessibility tests and the 73 tests in the three Switch interaction spec files.

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

vercel Bot commented Nov 21, 2026 •

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated
astryx Ready Ready Preview Nov 21, 2026 9:20pm UTC

Request Review

@meta-cla meta-cla Bot added the CLA Signed This label is managed by the Meta Open Source bot. label Nov 21, 2026
@astracat-bot

astracat-bot Bot commented Nov 21, 2026 •

Copy link
Copy Markdown
Contributor

AI review status for this pull request.

Review status Updated
🟢 Reviewed (for maintainers only) Nov 21, 2026, 9:20 PM UTC

@github-actions

github-actions Bot commented Nov 21, 2026 •

Copy link
Copy Markdown
Contributor

PR Analysis Report

🔎 Preview · e825408

Storybook · Sandbox · built from this PR's head

Modified components

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

@astracat-bot astracat-bot Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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: TrackState now also holds the thumb's state (switchFill.ts:18); a neutral name such as ElementState would 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.ts see 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-120 swaps 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.ts and 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.ts gains stateOf. 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 useSwitchTouch unchanged. 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.ts travel 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.

Automated review (maintainer link)

*/

/** A track state: its class name and its data attributes. */
/** A state of the track or the thumb: its class name and data attributes. */

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@cixzhang cixzhang added ci:run A maintainer lets this outside contributor's pull request run CI (AST-030 FR17) ci:run-a11y Run the accessibility suite on this PR; write access, next push (AST-030 FR12) labels Nov 23, 2026
@cixzhang

Copy link
Copy Markdown
Contributor

/approve-spec e825408

@cixzhang
cixzhang marked this pull request as ready for review November 23, 2026 00:12

@cixzhang cixzhang left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@cixzhang
cixzhang merged commit 38ea127 into meta:main Nov 23, 2026
208 of 211 checks passed
@vjeux
vjeux deleted the perf/switch-no-style-read-at-mount branch November 23, 2026 01:02
cixzhang added a commit that referenced this pull request Nov 23, 2026
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.

This branch was successfully deployed

1 active deployment
Preview — e825408b Deployed Nov 21, 2026 by vercel[bot]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ci:run A maintainer lets this outside contributor's pull request run CI (AST-030 FR17) ci:run-a11y Run the accessibility suite on this PR; write access, next push (AST-030 FR12) CLA Signed This label is managed by the Meta Open Source bot.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants