Repository navigation
Conversation
afef127 to
1481454
Compare
|
@quantizor this is for React 17 which should still be updating styles in render, right? |
27403f5 to
f2ef743
Compare
|
@quantizor I've updated the implementation so it no longer has a perf regress:
The benchmark is pushed to a different branch, and pits the latest v6 release on npm, with an npm published version of the changes in this branch (currently: When running the workspace version against v6 I get strange numbers, I tried to run a baseline (branch with repro), but for some reason the workspace version always gave worse numbers than the v6 published to npm: |
|
Ok I found a discrepancy in the benchmark code that I fixed in |
5d8c004 to
fd88d40
Compare
|
Thanks @quantizor, I merged in your changes and re-run the benchmarks. I ran each separately, multiple times so I could compare the median times and variance for each. As the benchmark is on react 17 we should see no difference since the logic for
Mount deep
Mount wide
Update dynamic stylesThe workspace version is faster (01.63 ms vs. 01.81 ms) with lower variance (±00.93 ms vs. ±01.06 ms). ConclusionThe workspace version is overall faster and more stable compared to v6 in the benchmarks provided. Which is a bit surprising to me as I don't think it's actually faster. But it's probably fine as the goal in any case was to make sure it didn't regress 🤷 |
|
@quantizor I altered the benchmark to use react 18, and ReactDOM.createRoot from
The workspace version is consistently faster, though the differences are very small, and variance is generally comparable. |
|
@quantizor I've made a repro that demonstrates the perf impact: concurrent-styled-components.mov |
Thank you for making this! I really appreciate your effort and persistence here and will be happy to accept the enhancement 🚀 |
|
|
||
| const isBrowser = typeof document !== 'undefined'; | ||
| // @ts-expect-error - this is fine | ||
| const useInsertionEffect = React['useInsertion' + 'Effect'] |
There was a problem hiding this comment.
Emotion started using this pattern in emotion-js/emotion#2651, in response to community reports that some bundlers got build errors when versions older than React 18 were used: emotion-js/emotion#2649 🤔
The original implementation was focused on racing who can render the most iterations (1 iteration is when all cells have finished animating their background color) in total, even if it's at the cost of fps. I've updated the benchmark to be more balanced, and configurable. It's still better to render the most iterations, but without sacrificing a stable, high, framerate. In this video (before, after) I'm running two chrome (v128) windows in split screen on a M1 Pro at a 3456x2234 resolution, connected to a charger so there's no CPU throttling, running on macOS 14.5: updated.bench.4K.movAs you can see it reaches the 20th iteration twice as fast, while maintaining a stable FPS ranging 35-102 compared to 28-87 when |
🦋 Changeset detectedLatest commit: ad94c91 The changes in this PR will be included in the next version bump. This PR includes changesets to release 1 package
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
|
Quick protip @quantizor, if you want to inspect in more detail what's happening, and what the React timeline looks like when the I'm building the benchmark with |
|
Hey @quantizor, anything I can do to help land this? 😄 |
|
@stipsan so sorry to see this hanging here. We could really use some optimizations of styled components… curious if you forked styled components are patched it for your use case at Santiy. |
|
@skovhus and others who land here: We have open-sourced our fork that fixes a lot of these things. It's a stop gap for us as we're migrating out of SC/css-in-js, so it won't be actively maintained, but might be useful you're on the same journey: https://github.com/sanity-io/styled-components-last-resort |
|
Happy to take a second look at this PR with some additional profiling data per https://x.com/quantizor/status/1966183924985442547?s=46 |
Did you see #4332 (comment)?
There are many others, but the TL;DR is that the current benchmarks on this repo isn't equipped to test them. |
|
I really love everything styled-components stands for. There is no easier way to write styles in react-native. Is it possible to have that PR on some kind o minor version? |
Not related with this PR itself but i think the most performant way to write styles for react-native is using the built in StyleSheet.create() method. Do https://github.com/styled-components/babel-plugin-styled-components already do that? |
|
Based on @quantizor's lack of response in this thread, as well as on #5607 (comment), combined with the recent work creating lots of merge conflicts with the release of 6.3 with RSC support, I'm closing this PR. Instead of declining the PR, or giving any actionable feedback so the PR can be moved forward and land, @quantizor have chosen to simply ghost it. It took an incredible amount of effort and time for me to work on this, and I've respected @quantizor's time and been patiently waiting. |
|
Actually I've replied multiple times in different places. On twitter and I also spent a decent amount of time in #5607 on it. The reason why this hasn't moved forward is I simply have not been able to find proof yet outside of Sanity that it provides a clear benefit. |
|
👋🏼 Using the insertion effect was a huge improvement for us at Linear, as described here: https://www.sanity.io/blog/cut-styled-components-into-pieces-this-is-our-last-resort |
|
Alright, I'll come back to this. Trying to get the new RSC workaround put to bed without a new major. The React 19-specific improvements to drop ServerStyleSheet look interesting, but that's definitely a breaking change so it would need to come after. Honestly my current struggle is just getting enough timely testing and good bug reports. We're getting there. |
|
On another note, it seems like you both are very passionate about this and, perhaps more importantly, have useful production setups to actually test meaningful improvements. I'd be open to inviting either or both of you as co-maintainers if that interests you. At this point, I'm just trying to do my best but am kind of flying blind in various ways. |
You never got back to me on #4332 (comment), I don't know if you've seen it, if you've seen it I don't know why you're unconvinced it has a clear benefit, it would be nice if I could get feedback on that.
On RSC, based on our experience targeting it, the best approach is to use
Look, I know how hard it is to be a solo maintainer so I'm happy to come on as a co-maintainer and help. Creating the sc forks ( I have low availability/bandwidth until March, I'd be happy to come onboard then and start working on contributing back upstream the improvements we've done in our forks 🙌 |
|
Get in touch when you free up, I'd be happy to welcome you and the community would benefit from your involvement. 😃 |
|
Thanks for this PR and the detailed write-up — it directly informed the performance work in the upcoming 6.4 release. We implemented the The implementation is preserved for future evaluation, particularly if Suspense-heavy patterns become more common and the concurrent interruption cost becomes dominant. In the meantime, 6.4 includes substantial performance improvements that should address the original motivation:
Plus first-class RSC support, Would love for you to try the prerelease and share your benchmarks: Release PR: #5687 |
I'm back, I'd love to get involved, reach out on [email protected] and let's have a chat 🙌
Really impressive improvements! I'll find time next week and run some benchmarks and report back 👍 |










At sanity we use
styled-componentsextensively, our@sanity/uidesign system is built upon it, and most of the Sanity Studio codebase is as well. We've been testing the impact of implementinguseInsertionEffectand see a noticeable performance improvement both on component mounting and rerender perf when the CSS is changed and it motivated us to open a PR 🙌The new React docs explains
useInsertionEffectreally well, especially the section at the end:Using Insertion Effects in
StyledComponentThe work in this implementation builds upon @alexandernanberg's work in #3821 and how Emotion implements it.
By using
useInsertionEffectAlwaysWithSyncFallbackfrom@emotion/use-insertion-effect-with-fallbackswe are able to:useInsertionEffecthook is used to write to the CSSOM.useEffectanduseLayoutEffecthooks are called.Using Insertion Effects in
createGlobalStyleI'm using
useInsertionEffectWithLayoutFallbackfrom@emotion/use-insertion-effect-with-fallbacksto ensure that Insertion Effects are used while available, otherwise Layout Effects are used like it is now.I saw that global styles used to opt-in to Insertion Effects in the past, but it was reverted in #4101.
It seems like the React team doesn't see the behaviour as a bug, and there's ways to make global styles mounted in a suspended component (like in a React.lazy) eagerly run teardown and remove global styles:
I forked @mdeschamps codesandbox and modified it to demonstrate how using a
keyprop on the outerSuspenseboundary solves it (using ourspeedy-styled-componentsfork that implements this PR), it also has commented out code that usesstartTransitioninstead of changing the key.Testing
To run the benchmark, and tests, I've bumped react from 17 to 18.One doesn't simply bump test dependencies 😮💨 I changed it back to 17.There's also a forked version of
styled-componentson npm that you can use to test the changes in this PR in userland:"styled-components": "npm:[email protected]".