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 ae896ab
Browse filesBrowse the repository at this point in the historyBrowse files
refactor(css): lazy-load syntax parser for embedded CSS (#22706)
* refactor(css): lazy-load syntax parser for embedded CSS
* refactor(css): read CSS transforms without the CSS facade
Import pickTransforms from webpack/lib/css/syntax-options in the HTML and
XML minifiers, keeping css.syntax to the parser, printer and SourceProcessor.
Load the HTML parser in builtinEmbeddedRenderer only on first render, so
cssMinify with svg no longer loads it for a stylesheet holding no SVG.
* refactor(css): keep CSS transform options in the parser
Move pickTransforms back into syntax-parser.js and drop syntax-options.js.
builtinEmbeddedRenderer builds its stylesheet options on first CSS render,
and the HTML and XML minifiers pass transforms as a getter, so a document
without CSS still never loads the CSS parser.
* refactor(html): import builtinEmbeddedRenderer dependencies at top level
Read the embedded-language names from util/dataURL and the CSS and HTML
SourceProcessor from their facades, so no require runs inside a function
and both parsers still load only when a body needs one. Test what loads.
* refactor(html): spell block-contents in place rather than exporting it
It names a CSS production, not anything about data: URLs, and cssMinify
and the defaults already spell it as a literal.
* perf(css): compile the joinable-selector regexes without the i flag
Spell each keyword in both ASCII cases instead: with i, V8 case-folds the
whole \u00A0-\uFFFF identifier class, which made the first join test of
a worker cost about 8 ms for top-level rules and 16 ms for nested ones.
* chore: add changeset for faster minifier startup
* test(html): check htmlMinify and xmlMinify skip the CSS parser without CSS
* perf: share the CLI's compile cache with minimizer workers
enableCompileCache() only covers the CLI's own process, so every minimizer
worker compiled the parsers and printers from source on each build. Export
the directory as NODE_COMPILE_CACHE, which worker threads and child
processes read at startup, unless the environment already names one or
disables the cache.
* test: run the compile cache case on Node only
Bun and Deno expose module.enableCompileCache without Node's cache behind
it, so the spawned CLI reported nothing and runtimes (bun) failed.
* docs: add startup cost rules to AGENTS.md
Load on first use, keep caller-facing constants in light modules, let a
disabled option cost nothing, keep laziness in the facades, avoid i-flag
regexes over non-ASCII ranges, hand workers the parent's setup, and prove
it by counting what a fresh process loads.
---------
Co-authored-by: sheo13666q <[email protected]>
Users measure webpack by build time and peak heap. Much of `lib/` sits on per-module hot paths (sometimes per module × runtime, or per chunk × module), so constant factors compound: weigh time and memory in every change, bug fixes and refactors included. Less allocation, smaller `Map`/`Set` footprints and fewer closures retained on hot paths are wins. For any per-`Compilation` state, ask whether it can be released after seal/emit so large structures aren't retained longer than needed (see #15521). Sanity-check perf changes with `FILTER="<case-name>" yarn benchmark` before CI's benchmarks flag them.
308
308
309
+
### Startup cost
310
+
311
+
Every build, CLI run and minimizer worker starts cold, and most of that start is V8 compiling the code it loads (each parser or printer costs 10–30 ms and 1.4–3 MB to load). So pay only for what this input and these options use:
312
+
313
+
-**Load on first use.** A module some inputs, languages or options never need is reached through a facade getter (`syntax.js`) or `memoize(() => require(…))` at the point of use, never a top-level `require`; HTML without CSS never loads the CSS parser, and CSS without an SVG `data:` URL never loads the HTML parser.
314
+
-**Keep what callers need without parsing in a light module** (`util/dataURL`, `ModuleSourceTypeConstants`): constants, option pickers and language lists, never read off a parser, printer or plugin.
315
+
-**An option that turns work off turns its cost off too**: build its tables, regexes and closures on first use, not at load or construction.
316
+
-**Keep laziness in one place** — the facade or memoize helper — not ad-hoc `require()` inside function bodies, so the move to ES modules stays mechanical.
317
+
-**No `i` flag on a regex with a non-ASCII range**: V8 case-folds the whole range at compile time (8–16 ms measured); spell keywords in both ASCII cases instead.
318
+
-**Workers and child processes inherit nothing**: hand them what the parent set up, as `bin/webpack.js` does with the compile cache through `NODE_COMPILE_CACHE`.
319
+
-**Prove it by counting**: run the entry point in a fresh process and assert which `lib/` files `require.cache` holds, as `HtmlSyntax.unittest.js` "what it loads" does; gate Node-only APIs on `!process.versions.bun && !process.versions.deno`, not on the function existing.
320
+
309
321
### Keep instance shapes stable
310
322
311
323
Initialize **every** instance field in the constructor — `undefined`/`null` for those first assigned later. A first assignment outside the constructor forces a V8 hidden-class (Shape) transition, splitting instances across shapes: inline caches go polymorphic/megamorphic (a hot read can cost ~2× at two shapes, more when megamorphic), and code optimized for the first shape deopts with `wrong map`. Never `delete` a field (dictionary mode); set it to `undefined`. One trailing field gains little, but the rule is uniform so reviewers needn't judge case by case — it's why `Dependency` sets all its `_loc*` slots up front. Deliberate symbol-keyed sparse slots are the documented exception.
0 commit comments