Skip to content

Commit ae896ab

Browse 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]>
1 parent afca4d8 commit ae896ab

11 files changed

Lines changed: 236 additions & 45 deletions
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"webpack": patch
3+
---
4+
5+
Speed up minifier startup, sharing the compile cache with minimizer workers.

‎AGENTS.md‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -306,6 +306,18 @@ Runtime-emitting code (chunk loading, resource hints, library and externals pres
306306

307307
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.
308308

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+
309321
### Keep instance shapes stable
310322

311323
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.

‎bin/webpack.js‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,12 @@ const { enableCompileCache } = require("module");
88

99
if (enableCompileCache) {
1010
try {
11-
enableCompileCache();
11+
const { directory } = enableCompileCache();
12+
// Minimizer workers and other child processes start without the cache;
13+
// the variable Node reads at startup hands them the same directory.
14+
if (directory && process.env.NODE_COMPILE_CACHE === undefined) {
15+
process.env.NODE_COMPILE_CACHE = directory;
16+
}
1217
} catch (_error) {
1318
// Nothing
1419
}

‎lib/css/syntax-parser.js‎

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15501,31 +15501,41 @@ const _STRING = String.raw`"(?:[^"\\]|\\[\s\S])*"|'(?:[^'\\]|\\[\s\S])*'`;
1550115501
const _ATTRIBUTE = String.raw`\[(?:${_IDENT}\|)?${_IDENT}(?:[~|^$*]?=(?:${_IDENT}|${_STRING})(?: [iI])?)?\]`;
1550215502
// CSS whitespace: U+00A0 is an identifier's character, not a separator.
1550315503
const _SPACE = "[ \\t\\n\\f\\r]";
15504-
const _LEGACY_ELEMENT_NAMES = [...LEGACY_PSEUDO_ELEMENTS].join("|");
15504+
/**
15505+
* A pattern matching the word in any ASCII case, where an `i` flag would make V8
15506+
* case-fold the whole `\u00A0-\uFFFF` class too, costing milliseconds to compile.
15507+
* @param {string} word a lowercase word
15508+
* @returns {string} the pattern
15509+
*/
15510+
const _anyCase = (word) =>
15511+
word.replace(/[a-z]/g, (letter) => `[${letter}${letter.toUpperCase()}]`);
15512+
const _LEGACY_ELEMENT_NAMES = [...LEGACY_PSEUDO_ELEMENTS]
15513+
.map(_anyCase)
15514+
.join("|");
1550515515
// A pseudo-class with no selector inside it: argument-less, or the `An+B` of an
1550615516
// `:nth-*()`, whose grammar is closed — the one argument a shape may carry.
1550715517
const _PSEUDO_CLASS = String.raw`:(?!(?:${_LEGACY_ELEMENT_NAMES})(?![-\w]))(?:(?:${[
1550815518
...NTH_PSEUDO_FUNCTIONS
15509-
].join(
15510-
"|"
15511-
)})\(${_SPACE}*(?:odd|even|[-+]?(?:\d*n(?:${_SPACE}*[-+]${_SPACE}*\d+)?|\d+))${_SPACE}*\)|[-\w]+(?![-\w(]))`;
15519+
]
15520+
.map(_anyCase)
15521+
.join(
15522+
"|"
15523+
)})\(${_SPACE}*(?:${_anyCase("odd")}|${_anyCase("even")}|[-+]?(?:\d*[nN](?:${_SPACE}*[-+]${_SPACE}*\d+)?|\d+))${_SPACE}*\)|[-\w]+(?![-\w(]))`;
1551215524
// A pseudo-element ends the selector it is in: anything after it is no shape
1551315525
// an engine parses.
1551415526
const _PSEUDO_ELEMENT = String.raw`(?:::[-\w]+|:(?:${_LEGACY_ELEMENT_NAMES}))(?![-\w(])`;
1551515527
const _COMPOUND = `(?:\\*|${_IDENT}|[.#]${_IDENT}|${_ATTRIBUTE})(?:[.#]${_IDENT}|${_ATTRIBUTE}|${_PSEUDO_CLASS})*`;
1551615528
const _COMBINATOR = `(?:${_SPACE}*[>+~]${_SPACE}*|${_SPACE}+)`;
1551715529
const _JOINABLE_SELECTOR_RE = new RegExp(
15518-
`^(?:${_COMPOUND}${_COMBINATOR})*${_COMPOUND}(?:${_PSEUDO_ELEMENT})?$`,
15519-
"i"
15530+
`^(?:${_COMPOUND}${_COMBINATOR})*${_COMPOUND}(?:${_PSEUDO_ELEMENT})?$`
1552015531
);
1552115532

1552215533
// The same, for a rule nested in another: `&` joins there because an engine that
1552315534
// cannot read it cannot read the block holding it either, so both selectors are
1552415535
// dropped together rather than one taking the other down.
1552515536
const _NESTED_COMPOUND = `(?:&(?:${_COMPOUND})?|${_COMPOUND})`;
1552615537
const _JOINABLE_NESTED_RE = new RegExp(
15527-
`^(?:${_NESTED_COMPOUND}${_COMBINATOR})*${_NESTED_COMPOUND}(?:${_PSEUDO_ELEMENT})?$`,
15528-
"i"
15538+
`^(?:${_NESTED_COMPOUND}${_COMBINATOR})*${_NESTED_COMPOUND}(?:${_PSEUDO_ELEMENT})?$`
1552915539
);
1553015540

1553115541
// A keyframe selector is a percentage or `to` — `from` is already printed `0%`.
@@ -16500,7 +16510,7 @@ const _nothingBetweenShadows = (items, texts, from, to, declared, subjects) => {
1650016510
// selectors and pseudos are gone: what either holds may look like a combinator.
1650116511
const _SUBJECT_STRIP_RE = new RegExp(
1650216512
`${_ATTRIBUTE}|${_PSEUDO_CLASS}|${_PSEUDO_ELEMENT}`,
16503-
"gi"
16513+
"g"
1650416514
);
1650516515
const _SUBJECT_TYPE_RE = /(?:^|[\s>+~])([-\w\u00A0-\uFFFF]+)[^\s>+~]*$/;
1650616516

‎lib/html/builtinEmbeddedRenderer.js‎

Lines changed: 31 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,12 @@
55

66
"use strict";
77

8+
const { SourceProcessor: CssSourceProcessor } = require("../css/syntax");
89
const { CSS_TYPE } = require("../module/ModuleSourceTypeConstants");
10+
const { JSON_TYPE, SVG_TYPE } = require("../util/dataURL");
11+
const { SourceProcessor: HtmlSourceProcessor } = require("./syntax");
912

10-
/** @import { CssProcessOptions } from "../css/syntax-parser" */
13+
/** @import { CssPrintOptions, CssProcessOptions } from "../css/syntax-parser" */
1114
/** @import { EmbeddedSourceRenderer } from "./syntax-printer" */
1215

1316
/**
@@ -21,15 +24,6 @@ const { CSS_TYPE } = require("../module/ModuleSourceTypeConstants");
2124
* @property {boolean=} svg minify an SVG `data:` URL as the `.svg` assets are, wherever that is shorter (default false)
2225
*/
2326

24-
// Both are loaded on first render rather than at module load: a document that
25-
// embeds no CSS and no JSON pays for neither.
26-
/** @type {typeof import("../css/syntax") | undefined} */
27-
let _cssSyntax;
28-
/** @type {typeof import("./syntax-parser") | undefined} */
29-
let _htmlParser;
30-
/** @type {typeof import("./syntax") | undefined} */
31-
let _htmlSyntax;
32-
3327
/**
3428
* Strip the whitespace between a JSON body's tokens, every literal copied byte
3529
* for byte. Re-serializing would round numbers, drop a duplicate key and rewrite
@@ -78,21 +72,14 @@ const stripJsonWhitespace = (json) => {
7872
* @returns {EmbeddedSourceRenderer} the renderer
7973
*/
8074
const builtinEmbeddedRenderer = (options = {}) => {
81-
const { BLOCK_CONTENTS, JSON_TYPE, SVG_TYPE } =
82-
_htmlParser || (_htmlParser = require("./syntax-parser"));
83-
const { svg, ...cssOptions } = options;
84-
const sheet = {
85-
mode: /** @type {"minify"} */ ("minify"),
86-
...cssOptions,
87-
// A stylesheet's own `url(data:image/svg+xml,…)`, which `renderSvg` answers.
88-
renderEmbeddedSource: svg
89-
? /** @type {EmbeddedSourceRenderer} */ (
90-
(source, info) =>
91-
info.type === SVG_TYPE ? renderSvg(source) : undefined
92-
)
93-
: undefined
94-
};
95-
const block = { ...sheet, as: BLOCK_CONTENTS };
75+
const { svg } = options;
76+
// Built on first CSS render, so reading `options` (a lazy `transforms`
77+
// included) waits until the CSS minifier loads.
78+
/** @typedef {CssPrintOptions & { mode: "minify", as?: CssProcessOptions["as"] }} SheetOptions */
79+
/** @type {SheetOptions | undefined} */
80+
let sheet;
81+
/** @type {SheetOptions | undefined} */
82+
let block;
9683
// A `style=""` repeats across a document far more often than it varies.
9784
/** @type {Map<string, string>} */
9885
const blocks = new Map();
@@ -104,18 +91,31 @@ const builtinEmbeddedRenderer = (options = {}) => {
10491
return svg && info.as === undefined ? renderSvg(source) : undefined;
10592
}
10693
if (info.type !== CSS_TYPE) return undefined;
107-
const isBlock = info.as === BLOCK_CONTENTS;
94+
const isBlock = info.as === "block-contents";
10895
if (isBlock) {
10996
const memoized = blocks.get(source);
11097
if (memoized !== undefined) return memoized;
11198
}
112-
const { SourceProcessor } =
113-
_cssSyntax || (_cssSyntax = require("../css/syntax"));
11499
let code;
115100
try {
116-
code = new SourceProcessor().process(
101+
if (sheet === undefined) {
102+
const { svg: _svg, ...cssOptions } = options;
103+
sheet = {
104+
mode: /** @type {"minify"} */ ("minify"),
105+
...cssOptions,
106+
// A stylesheet's own `url(data:image/svg+xml,…)`, which `renderSvg` answers.
107+
renderEmbeddedSource: svg
108+
? /** @type {EmbeddedSourceRenderer} */ (
109+
(source, info) =>
110+
info.type === SVG_TYPE ? renderSvg(source) : undefined
111+
)
112+
: undefined
113+
};
114+
block = { ...sheet, as: "block-contents" };
115+
}
116+
code = new CssSourceProcessor().process(
117117
source,
118-
isBlock ? block : sheet
118+
/** @type {SheetOptions} */ (isBlock ? block : sheet)
119119
).code;
120120
} catch (_err) {
121121
// Text the minifier cannot read is left as written, by declining.
@@ -130,12 +130,10 @@ const builtinEmbeddedRenderer = (options = {}) => {
130130
* @returns {string | undefined} it minified, or undefined where that is no shorter or it is not XML
131131
*/
132132
const renderSvg = (source) => {
133-
const { SourceProcessor } =
134-
_htmlSyntax || (_htmlSyntax = require("./syntax"));
135133
let code;
136134
try {
137135
// `'` around an attribute, since the URL usually sits in `"` quotes.
138-
code = new SourceProcessor().process(source, {
136+
code = new HtmlSourceProcessor().process(source, {
139137
xml: true,
140138
mode: "minify",
141139
xmlQuote: "'",

‎lib/html/htmlMinify.js‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,10 @@ const htmlMinify = async (
9595
rewriteCustomProperties: css.rewriteCustomProperties,
9696
unusedSymbols: css.unusedSymbols,
9797
pseudoClasses: css.pseudoClasses,
98-
transforms: webpack.css.syntax.parser.pickTransforms(css),
98+
// Picked on first CSS render, which loads the CSS parser anyway.
99+
get transforms() {
100+
return webpack.css.syntax.parser.pickTransforms(css);
101+
},
99102
svg
100103
});
101104
// One set of options, handed to the document pass and to each conditional

‎lib/html/xmlMinify.js‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,10 @@ const xmlMinify = async (input, sourceMap, minimizerOptions = {}) => {
3737
rewriteCustomProperties: css.rewriteCustomProperties,
3838
unusedSymbols: css.unusedSymbols,
3939
pseudoClasses: css.pseudoClasses,
40-
transforms: webpack.css.syntax.parser.pickTransforms(css)
40+
// Picked on first CSS render, which loads the CSS parser anyway.
41+
get transforms() {
42+
return webpack.css.syntax.parser.pickTransforms(css);
43+
}
4144
});
4245
return {
4346
code: new webpack.html.syntax.SourceProcessor().process(code, {

‎lib/util/dataURL.js‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -327,6 +327,8 @@ const askEmbeddedRenderer = async (render, hole, reported) => {
327327

328328
module.exports = {
329329
EMBEDDED_LANGUAGES,
330+
JSON_TYPE,
331+
SVG_TYPE,
330332
URIRegEx,
331333
askEmbeddedRenderer,
332334
buildDataURI,
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
"use strict";
2+
3+
const { execFileSync } = require("child_process");
4+
const fs = require("fs");
5+
const os = require("os");
6+
const path = require("path");
7+
8+
const BIN = path.resolve(__dirname, "../../bin/webpack.js");
9+
10+
// Node's compile cache exists from Node.js 22.8; Bun and Deno expose the
11+
// function without the cache behind it.
12+
const { enableCompileCache } = /** @type {{ enableCompileCache?: () => void }} */ (
13+
require("module")
14+
);
15+
const hasCompileCache =
16+
typeof enableCompileCache === "function" &&
17+
!process.versions.bun &&
18+
!process.versions.deno;
19+
20+
/**
21+
* Runs `bin/webpack.js` on a configuration that reports, and then exits, what a
22+
* child process it starts reads as its compile cache directory.
23+
* @param {NodeJS.ProcessEnv} extra variables to set on top of a clean environment
24+
* @returns {{ variable: string | null, childDirectory: string | null }} what it reported
25+
*/
26+
const runBin = (extra) => {
27+
const dir = fs.mkdtempSync(path.join(os.tmpdir(), "wp-bin-cache-"));
28+
const config = path.join(dir, "webpack.config.js");
29+
fs.writeFileSync(
30+
config,
31+
`const { execFileSync } = require("child_process");
32+
const childDirectory = execFileSync(process.execPath, [
33+
"-e",
34+
"process.stdout.write(JSON.stringify(require('module').getCompileCacheDir() || null))"
35+
], { encoding: "utf8" });
36+
process.stdout.write("REPORT" + JSON.stringify({
37+
variable: process.env.NODE_COMPILE_CACHE || null,
38+
childDirectory: JSON.parse(childDirectory)
39+
}) + "\\n");
40+
process.exit(0);
41+
`
42+
);
43+
/** @type {NodeJS.ProcessEnv} */
44+
const env = {};
45+
for (const [name, value] of Object.entries(process.env)) {
46+
if (!name.startsWith("NODE_COMPILE_CACHE") && name !== "NODE_DISABLE_COMPILE_CACHE") {
47+
env[name] = value;
48+
}
49+
}
50+
try {
51+
const out = execFileSync(process.execPath, [BIN, "--config", config], {
52+
cwd: dir,
53+
encoding: "utf8",
54+
env: { ...env, ...extra }
55+
});
56+
return JSON.parse(out.slice(out.indexOf("REPORT") + 6).split("\n")[0]);
57+
} finally {
58+
fs.rmSync(dir, { recursive: true, force: true });
59+
}
60+
};
61+
62+
(hasCompileCache ? describe : describe.skip)("bin/webpack.js compile cache", () => {
63+
it("hands the compile cache to the child processes it starts", () => {
64+
const { variable, childDirectory } = runBin({});
65+
expect(typeof variable).toBe("string");
66+
expect(typeof childDirectory).toBe("string");
67+
expect(/** @type {string} */ (childDirectory).startsWith(/** @type {string} */ (variable))).toBe(true);
68+
});
69+
70+
it("keeps a directory the environment already names", () => {
71+
const own = fs.mkdtempSync(path.join(os.tmpdir(), "wp-own-cache-"));
72+
try {
73+
const { variable, childDirectory } = runBin({ NODE_COMPILE_CACHE: own });
74+
expect(variable).toBe(own);
75+
expect(/** @type {string} */ (childDirectory).startsWith(own)).toBe(true);
76+
} finally {
77+
fs.rmSync(own, { recursive: true, force: true });
78+
}
79+
});
80+
81+
it("hands nothing on where the cache is disabled", () => {
82+
expect(runBin({ NODE_DISABLE_COMPILE_CACHE: "1" })).toEqual({
83+
variable: null,
84+
childDirectory: null
85+
});
86+
});
87+
});

0 commit comments

Comments
 (0)