Small speedups for `nitro dev` and `nitro build`

Aperta
#4,973 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub

I maintainer di solito rispondono entro 2 giorni

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
4/5
Tempo stimato
3-5 giorni
Idoneità per principianti
42/100
Tipo di issue
Funzionalità
Chiarezza
Abbastanza chiara
Stato di attività
Attiva
Stack tecnologico
node.js, typescript, vite

Direzione di ricerca

The issue groups four CLI speedups and links comparison branches for each; start by reading the linked changes and the related #4939. Check which changes the maintainers want before choosing one, then compare its measurements and scope with the described baseline. Done means the selected optimization is implemented and its reported CLI timing or module loading improvement is confirmed.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

cli dev performance

I measured where the Nitro CLI spends its time on a generated app with 300 routes. Each change below is small, so I put them all in one issue instead of opening a PR for each. Tell me which ones you want, and I'll open PRs only for those.

The biggest win, reload after an edit, is in env-runner. It's in unjs/env-runner#104.

1. Turn on Node's compile cache

Each time you run nitro, Node compiles Nitro and its dependencies from scratch. For nitro build that's about 2 MB of JS. module.enableCompileCache() tells Node to save the compiled code to disk and use it again on the next run. Vite's CLI already does this.

main with cache
nitro dev, first response 533 ms 494 ms
nitro build 495 ms 453 ms
nitro preview, first response 143 ms 134 ms

Node writes the cache when the process exits. A dev server doesn't exit, so the branch also writes it 10 seconds after start.

https://github.com/nitrojs/nitro/compare/main...onmax:nitro:perf/cli-compile-cache

2. Skip Nitro's own imports in the externals check

In dev, the nitro:externals plugin looks at each package import and decides if it stays an import or goes into the bundle. App code imports nitro and nitro/h3 in almost every file, and those always go into the bundle. In the 300-route app the plugin ran 830 times, and only 23 of those runs kept an import.

Adding /^nitro(?:\/|$)/ to noExternal lets Rolldown skip these imports without calling the plugin. The plugin now runs 23 times, and the dev bundle step goes from 155 ms to 138 ms. The Vite builder already has this pattern.

https://github.com/nitrojs/nitro/compare/main...onmax:nitro:perf/dev-externals-self-imports

3. Let Rolldown skip unwasm's renderChunk

unwasm's renderChunk only changes chunks that import a .wasm file. Rolldown still calls it for every chunk, and it copies the code of each chunk to JS for the call. The 300-route app has 519 chunks, so that's 519 calls that change nothing.

With filter: { code: "unwasm:external:" }, Rolldown skips those chunks. The Rolldown step goes from 262 ms to 252 ms. The same filter on nitro:externals takes it to 242 ms.

https://github.com/unjs/unwasm/compare/main...onmax:unwasm:perf/render-chunk-filter
https://github.com/nitrojs/nitro/compare/main...onmax:nitro:perf/build-render-chunk-filter

4. Don't load the dev server in nitro build

nitro build imports nitro/builder, and that also loads the dev server and env-runner. If the build command imports the build modules directly, it loads 26 fewer modules. That saves about 12 ms on the 300-route app. It's small, so skip it if you don't like the change. Related to #4939.

https://github.com/nitrojs/nitro/compare/main...onmax:nitro:perf/build-lazy-dev-modules

Two more ideas I measured but didn't build
  • import("rolldown") waits until the config has loaded, and it takes 15 to 35 ms. It only needs rootDir, so the CLI could start it earlier.
  • copyPublicAssets calls fsp.cp for each file. For 150 files that takes 15 to 35 ms. mkdir plus copyFile takes 4 to 8 ms. But copyFile follows symlinks, and cp copies them as symlinks. So this change would need a check for symlinks.
Lingua principale
TypeScript
Stelle
11.5k
Fork
968
Merge medio
4g 9h
PR unite (30g)
64

Preparare l'ambiente

Apri in Codespaces

Avvia il container di sviluppo del progetto nel browser, con il tuo account GitHub.

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di nitrojs/nitro

Tutte le issue di nitrojs/nitro

Issue simili

Altre issue su TypeScript

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.