You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Commit 52c5f6e
Browse filesBrowse the repository at this point in the historyBrowse files
fix: run style interpolations on every render (#5788) (#5809)
* fix: run style interpolations on every render (#5788)
The render cache added in 6.4.0 reused a cached result on a props-equal
re-render, skipping evaluation of interpolations and attr functions. Those
run in the component's own render body, so skipping them dropped any hook
they called (crashing with "Rendered fewer hooks than expected", in
production too) and served a stale class name or style object when an
interpolation read anything outside props and theme.
Interpolations and attr functions now run on every render, on both the web
and native paths. The pure work downstream of the produced CSS (hashing,
compile, injection on web; the CSS-to-style-object transform on native) is
still memoized, so repeat renders that yield the same styles stay cheap. A
props-equal re-render bailout belongs at the component boundary via
React.memo, which only the caller can key correctly.
* perf(native): key the inline style cache by CSS string
cssToStyleObject keyed its cache by generateComponentId(flatCSS), whose
base-52 name conversion and regex ran on every render and dominated the
native re-render cost (~80% of per-child work, measured), while the id was
only ever used as an internal Map key and never as a class name. Keying by
the flat CSS string directly drops generateComponentId from the path.
Recovers most of the re-render cost from running interpolations every render
(#5788) and makes cache-miss, cycling, and sibling paths faster than before,
since the id was on the miss path too. A string key also cannot collide the
way the 32-bit hash could, so two distinct CSS strings can no longer return
each other's cached style.
* refactor(test): share hook-recording helper, clear stale render-cache text
Extract the React use*-recording harness (duplicated across the web hook
tests and the new native one) into src/test/recordHooks.ts, and update the
tests, docs, and agent rules that still described the removed render cache:
retitle the HMR class-name test, rewrite the memoization suite's header and
hook-sequence test to re-render vocabulary, correct the runtime-performance
hot-path wording, and fix an AGENTS rule that read as "hooks must be
unconditional" against the deliberate __SERVER__/IS_RSC gating. Trim the
changeset to user-observable behavior.
Fixed a crash ("Rendered fewer hooks than expected") and a related stale-style bug for components that call a React hook, or read any value outside their props and theme, from inside a style interpolation. This affected `@mui/styled-engine-sc` with MUI X DataGrid, which calls hooks within an interpolation, and was a regression introduced in 6.4.0.
6
+
7
+
Style interpolations now run on every render, so a hook called inside one runs consistently and a value read inside one always reflects its current state.
8
+
9
+
If a component re-renders often with unchanged props and its interpolations are expensive, wrap it in `React.memo` to skip those re-renders. That is the right place to bail out, because only the calling code knows the full set of inputs its styles depend on.
Copy file name to clipboardExpand all lines: docs/runtime-performance.md
+18-16Lines changed: 18 additions & 16 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -14,30 +14,32 @@ microbenchmark against the realistic workload, not a synthetic best case.
14
14
| Template literals | Manual `+` concat is 1.3x faster than `` `${a}${b}` `` in tight loops |
15
15
|`React.createElement`| Raw element objects measure 60-120x faster, but are NOT used on this branch: `StyledComponent` calls React's own `createElement`. Measurement only, not an inventory entry |
16
16
17
-
## Dynamic re-render hot path
17
+
## Dynamic render hot path
18
18
19
-
On a cache hit, meaning props and theme are unchanged and this is the most common re-render:
19
+
Interpolations and attr functions run on every render. They are the component's own render-body user
20
+
code, so a hook called inside an interpolation (React `useContext` from an `@mui/styled-engine-sc`
21
+
adapter, for example) is one of the component's hooks and must run every render like any other hook
22
+
site. Skipping evaluation on a props-equal re-render would drop that hook and serve a stale class name
23
+
whenever an interpolation reads state outside props and theme (#5788).
20
24
21
-
-`shallowEqualContext` costs roughly 0.2us, comparing props by `for..in` against a stored key count.
22
-
- Everything else is skipped: `resolveContext`, flatten, hash and `generateName`.
23
-
`buildPropsForElement` still runs.
24
-
25
-
On a cache miss, meaning props changed:
25
+
The pure work downstream of the produced CSS string is memoized, so a repeat render that yields the
26
+
same CSS is cheap:
26
27
27
28
1.`resolveContext`: object spread plus attrs evaluation.
28
-
2.`flatten()` fast path: an inline function call for string-returning interpolations, roughly 0.05us
29
-
each.
30
-
3.`dynamicNameCache` lookup: a `Map.get` on the CSS string, O(1), which skips `phash` and
31
-
`generateName` on a hit.
29
+
2.Interpolation fast path: an inline function call for string-returning interpolations that bypasses
30
+
`flatten`'s type dispatch and array allocation, roughly 0.05us each.
31
+
3.`dynamicNameCache` lookup: a `Map.get` on the CSS string (prefixed by the stylis plugin hash when
32
+
one is set), O(1), which skips `phash` and `generateName` on a hit.
32
33
4.`phash()` and `generateName`: only on a `dynamicNameCache` miss, the first time this CSS is seen.
33
34
5.`stylis` compile and serialize: only when `hasNameForId` misses, the first injection of this class.
34
35
6.`hasNameForId`: a `Map.has`, negligible.
35
36
36
-
The render cache splits this path in two, and only the miss side runs the style work. Any hook placed
37
-
on one side of that split is called on a miss and skipped on a hit, which makes the component emit a
38
-
different hook sequence depending on whether its props happened to change. React rejects that outright
39
-
(#5788). `memoization.test.tsx` records the hook sequence across both cache states and asserts they
40
-
match, so a hook added inside the branch fails there rather than in a consumer's app.
37
+
A props-equal re-render bailout belongs at the component boundary via `React.memo`, which only the
38
+
caller can key against the full set of inputs the styles depend on. `memoization.test.tsx` records the
39
+
hook sequence across a re-render with unchanged props and asserts it matches the mount, so a change
40
+
that reintroduces skipped evaluation fails there rather than in a consumer's app. The native path is
41
+
the same: `InlineStyle` caches CSS-to-style-object by content, so `generateStyleObject` returns a
42
+
stable reference for equal styles without an outer render cache.
0 commit comments