question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

NextJS compile especially slow on M1 Mac silicon

See original GitHub issue

Workarounds to try in your codebase!

It looks like this ticket isn’t going to be addressed anytime soon (nvm, they fixed it pretty quick!) so I’ll just share what we did in our codebase to massively mitigate this. Reduced compile time by 25x.

  1. Move as many props as possible to unconditional sprinkles definitions (i.e. defineProperties() without conditions prop).
  2. Patch VE to remove all the dev string concatenation. This is our patch using yarn. We left in the option to use the strings if an env is defined:
diff --git a/node_modules/@vanilla-extract/css/dist/vanilla-extract-css.cjs.dev.js b/node_modules/@vanilla-extract/css/dist/vanilla-extract-css.cjs.dev.js
index 6e40061..10283a2 100644
--- a/node_modules/@vanilla-extract/css/dist/vanilla-extract-css.cjs.dev.js
+++ b/node_modules/@vanilla-extract/css/dist/vanilla-extract-css.cjs.dev.js
@@ -177,11 +177,13 @@ function generateIdentifier(debugId) {
   var fileScopeHash = hash__default["default"](packageName ? "".concat(packageName).concat(filePath) : filePath);
   var identifier = "".concat(fileScopeHash).concat(refCount);
 
-  if (adapter_dist_vanillaExtractCssAdapter.getIdentOption() === 'debug') {
-    var devPrefix = getDevPrefix(debugId);
+  if (process.env.VANILLA_EXTRACT_DEV_PREFIX) {
+    if (adapter_dist_vanillaExtractCssAdapter.getIdentOption() === 'debug') {
+      var devPrefix = getDevPrefix(debugId);
 
-    if (devPrefix) {
-      identifier = "".concat(devPrefix, "__").concat(identifier);
+      if (devPrefix) {
+        identifier = "".concat(devPrefix, "__").concat(identifier);
+      }
     }
   }

Describe the bug

We have a large NextJS codebase using VE that has progressively moved more and more of our CSS to VE over the past 2 months.

We’ve seen NextJS compile times on M1s get much slower as we adopted VE more (30s to 60s to 120s to 400s). Intel Macs and Windows do not have the issue - compiles are 30s for them.

We looked at 2 compile times, both of which got slower:

  1. Starting up a next server and waiting for it to start serving pages
  2. Going to a page that imports our VE components package and waiting until Next finishes the compile

FYI We have 74 css.ts files and several hundreds of elements with custom styles. And we use the standard withVanillaExtract plugin with no options.

Have you seen anything like this? Any tips to make this better for M1?

Workarounds attempted

In our NextJS page, removing the import to VE components eliminates the slowdown. It’s around 3s after that. But of course we need the imports at some point.

I tried stubbing out all the style() and recipe() and other calls in our code to return empty strings. That brought the compile down to 150s. That is still much slower than the Intel compile.

After the stubbing, I removed the withVanillaExtract plugin and that brought the compile time down to 30s as expected. So I suspect it’s something to do with the webpack stuff.

[See comments for more details on the following workarounds]

Installed an x64 version of node using Rosetta, and that brought compiles down to 50s.

Then I patched the VE webpack plugin to cache the result of processVanillaFile(), and that brought things down to 30s as expected.

Reproduction

Can’t provide since it’s private code. The problem only seems to happen on non-minimal projects!

System Info

System:
    OS: macOS 12.5
    CPU: (10) arm64 Apple M1 Max
    Memory: 78.61 MB / 32.00 GB
    Shell: 5.8.1 - /bin/zsh
  Binaries:
    Node: 16.10.0 - ~/.nvm/versions/node/v16.10.0/bin/node
    Yarn: 3.2.0 - ~/.nvm/versions/node/v16.10.0/bin/yarn
    npm: 7.24.0 - ~/.nvm/versions/node/v16.10.0/bin/npm
  Browsers:
    Chrome: 103.0.5060.134
    Firefox: 102.0.1
    Safari: 15.6
  npmPackages:
    esbuild: 0.14.11 => 0.14.11 
    webpack: 5.73.0 => 5.73.0

Used Package Manager

yarn

Logs

No response

Validations

Issue Analytics

  • State:closed
  • Created a year ago
  • Reactions:12
  • Comments:18 (5 by maintainers)

github_iconTop GitHub Comments

2reactions
foolmoroncommented, Aug 8, 2022

I added our 2 biggest workarounds at the top of the description in case it helps others!

1reaction
jisooyucommented, Oct 30, 2022

@mattcompiles, thanks a lot. After the installation, the speed is noticeably improved.

Read more comments on GitHub >

github_iconTop Results From Across the Web

NextJS compile especially slow on M1 Mac silicon · Issue #771
We've seen NextJS compile times on M1s get much slower as we adopted VE more (30s to 60s to 120s to 400s). Intel...
Read more >
Massively speed up VS Code loading time on Apple Silicon ...
If you are running Visual Studio Code on a Mac with an Apple Silicon processor (also known as an M1, M1 Pro, or...
Read more >
Compiling is slow after moving to Next.js 12.3 : r/nextjs - Reddit
js 12.3. Earlier, I was on 12.1 and the compile times were really fast. Now after I upgraded my project to 12.3, I...
Read more >
Benchmarking web dev tools on the Mac mini M1
The Mac mini M1 is consistently around 50% faster than the Macbook Pro in the Web Tooling Benchmark. These are native speeds, with...
Read more >
Ask HN: How are you dealing with the M1/ARM migration?
To be clear I think the support for Apple Silicon has been solid, but Docker Desktop for Mac regardless of CPU architecture has...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found