Skip to content

perf(css): cut allocations and repeated work in the CSS minifier - #22830

Merged
alexander-akait merged 12 commits into
mainfrom
perf/css-next
Oct 21, 2026
Merged

alexander-akait merged 12 commits into
mainfrom
perf/css-next

Conversation

@alexander-akait

@alexander-akait alexander-akait commented Oct 20, 2026 •

Copy link
Copy Markdown
Member

Summary

Speeds up CSS minification. The output is unchanged: it is byte-identical to main on all 6630 outputs checked with tooling/hash-css-outputs.js.

  • Keep the store of rule bodies allocated between top-level rules, instead of letting length = 0 free it each time.
  • Memoize _orderMatters, which takes two property names, capped at 32k entries.
  • Reuse a box longhand's printed declaration from the memo. The memo also records whether its value is one component, which is all the box merge reads.
  • Key the block memo by the block text, with one table per enclosing at-rule, so no joined key string is built per lookup.
  • Skip work that does nothing for most selectors:
    • walking the parts to unwrap :is() when none holds an is(;
    • lowercasing the whole text of a functional pseudo;
    • matching @supports parentheses when the text has no ((;
    • building a Set to order a selector list that is already in order.
  • When shadowing an earlier rule, store a selector's single earlier rule directly rather than in an array.
  • Add a TODO to make shadowing (dropping what a later rule with the same selector writes again) an opt-in option. It costs about 10% of minify CPU for 248 of 2,456,527 gzip bytes.

Measured with cssMinify over the yarn benchmark:css-tools corpus. Each fixture ran in a fresh process, best of 2 rounds, summed over all fixtures. Geomean is per fixture.

CPU ms Wall ms Geomean vs main
main 3801 2813 —
this PR 3570 2699 CPU −6.8%, wall −6.3%

Allocation per corpus pass went from 1252 MB to 1221 MB.

What kind of change does this PR introduce?

perf

Did you add tests for your changes?

Yes, in test/unitCases/css/syntax.unittest.js:

  • a test for the order memo starting over once it fills;
  • tests that a box longhand printed from the memo still merges when its value is one component, is still refused when it is more, and is not kept when it prints nothing.

Does this PR introduce a breaking change?

No

If relevant, what needs to be documented once your changes are merged or what have you already documented?

n/a. The pending changeset 300-faster-minifier-startup.md ("Speed up startup and CSS minification…") already covers this.

Use of AI

Written with Claude Code (Claude). It profiled the minifier and wrote the changes and tests. It checked each change for byte-identical output across the corpus and measured each one's CPU and wall time against its parent commit.


Generated by Claude Code

Summary by CodeRabbit

  • Bug Fixes

    • Improved CSS minification consistency when processing large stylesheets.
    • Fixed shorthand merging so margin values combine correctly when eligible, while preserving separate values when required.
    • Improved handling of repeated selectors and nested rule contexts.
    • Prevented incorrect processing of certain selector expressions.
  • Tests

    • Added regression coverage for large-input memoization, repeated minification, and margin shorthand output.

@changeset-bot

changeset-bot Bot commented Oct 20, 2026

Copy link
Copy Markdown

⚠️ No Changeset found

Latest commit: bb0c011

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

@github-actions github-actions Bot added the area: css CSS support (lib/css) label Oct 20, 2026
@github-actions

Copy link
Copy Markdown
Contributor

This PR is packaged and the instant preview of its latest commit is available.

Install it locally:

  • npm
npm i -D webpack@https://pkg.pr.new/webpack@22830
  • yarn
yarn add -D webpack@https://pkg.pr.new/webpack@22830
  • pnpm
pnpm add -D webpack@https://pkg.pr.new/webpack@22830

@github-actions

Copy link
Copy Markdown
Contributor

Types Coverage

Coverage after merging perf/css-next into main will be
99.28%
Coverage Report
FileStmtsBranchesFuncsLinesUncovered Lines
bin
   webpack.js98.86%100%100%98.86%113
declarations
   WebpackOptions.ts99.94%100%100%99.94%4240
examples
   build-common.js100%100%100%100%
   buildAll.js100%100%100%100%
   examples.js100%100%100%100%
   template-common.js98.21%100%100%98.21%72
examples/custom-javascript-parser
   test.filter.js100%100%100%100%
examples/custom-javascript-parser/internals
   acorn-parse.js100%100%100%100%
   meriyah-parse.js100%100%100%100%
   oxc-parse.js100%100%100%100%
examples/markdown
   webpack.config.mjs100%100%100%100%
examples/module-federation
   test.filter.js100%100%100%100%
examples/reexport-components
   test.filter.js100%100%100%100%
examples/ssr
   test.filter.js100%100%100%100%
examples/typescript
   test.filter.js100%100%100%100%
examples/typescript-non-erasable
   test.filter.js50%100%100%50%5
examples/virtual-modules
   test.filter.js100%100%100%100%
examples/wasm-bindgen-esm
   test.filter.js100%100%100%100%
examples/wasm-complex
   test.filter.js100%100%100%100%
examples/wasm-emscripten
   test.filter.js100%100%100%100%
examples/wasm-simple
   test.filter.js100%100%100%100%
examples/wasm-simple-source-phase
   test.filter.js100%100%100%100%
lib
   Chunk.js100%100%100%100%
   Compilation.js98.71%100%100%98.71%1783, 2096, 2103, 2111, 2133, 2136, 3124, 3683, 3726, 4315, 4319, 4446, 4467, 4592, 4629, 4636, 4689–4690, 4694, 4699, 4715–4716, 4730–4731, 4736–4737, 5265, 5291, 6141, 6158, 6176, 6192, 6207, 6232, 6235, 6603, 6606–6607, 6612, 6627, 6710, 6734, 855–856
   Compiler.js98.81%100%100%98.81%1116, 1153, 1156, 1336–1337, 1345, 848, 934, 975
   Dependency.js100%100%100%100%
   DependencyTemplate.js100%100%100%100%
   EntryPlugin.js100%100%100%100%
   Entrypoint.js100%100%100%100%
   ExternalsPlugin.js100%100%100%100%
   FileSystemInfo.js100%100%100%100%
   InitFragment.js100%100%100%100%
   LibraryTemplatePlugin.js100%100%100%100%
   ModuleFilenameHelpers.js100%100%100%100%
   ModuleNotFoundError.js100%100%100%100%
   MultiCompiler.js98.91%100%100%98.91%489, 561, 748, 813
   NormalModule.js100%100%100%100%
   NullFactory.js100%100%100%100%
   ProgressPlugin.js100%100%100%100%
   RawModule.js100%100%100%100%
   RequestShortener.js100%100%100%100%
   SingleEntryPlugin.js100%100%100%100%
   WebpackError.js100%100%100%100%
   index.js100%100%100%100%
   webpack.js98.59%100%100%98.59%306, 308
lib/asset
   AssetBytesGenerator.js100%100%100%100%
   AssetBytesParser.js91.67%100%100%91.67%27
   AssetGenerator.js100%100%100%100%
   AssetModule.js100%100%100%100%
   AssetModulesPlugin.js98.45%100%100%98.45%334, 360, 363, 493, 58
   AssetParser.js96.15%100%100%96.15%47
   AssetSourceGenerator.js100%100%100%100%
   AssetSourceParser.js91.67%100%100%91.67%27
   RawDataUrlModule.js100%100%100%100%
   WebManifestGenerator.js100%100%100%100%
   WebManifestParser.js100%100%100%100%
lib/async-modules
   AsyncModuleHelpers.js100%100%100%100%
   AwaitDependenciesInitFragment.js100%100%100%100%
   InferAsyncModulesPlugin.js100%100%100%100%
   isGeneratorLowered.js100%100%100%100%
lib/bun
   BunTargetPlugin.js100%100%100%100%
lib/cache
   AddBuildDependenciesPlugin.js100%100%100%100%
   AddManagedPathsPlugin.js100%100%100%100%
   Cache.js98.25%100%100%98.25%104
   CacheFacade.js100%100%100%100%
   IdleFileCachePlugin.js97.92%100%100%97.92%75, 87, 95
   MemoryCachePlugin.js92%100%100%92%33, 42
   MemoryWithGcCachePlugin.js93.42%100%100%93.42%106, 120–121, 130, 88
   PackFileCacheStrategy.js96.54%100%100%96.54%1306, 1406, 1410, 1472, 1718, 1802, 1825, 1855, 684, 703, 713–715, 717, 733–734, 739, 742, 744, 749, 754, 779, 785, 819, 825, 831, 836, 847, 856, 861–862, 864, 881, 887–888, 890
   ResolverCachePlugin.js100%100%100%100%
   getLazyHashedEtag.js100%100%100%100%
   mergeEtags.js100%100%100%100%
lib/config
   OptionsApply.js100%100%100%100%
   PlatformPlugin.js100%100%100%100%
   WebpackOptionsApply.js100%100%100%100%
   WebpackOptionsDefaulter.js100%100%100%100%
   browserslistTargetHandler.js100%100%100%100%
   cli.js98.89%100%100%98.89%114, 550, 582, 632
   defaults.js99.59%100%100%99.59%1633–1635, 1643, 288, 548
   defineConfig.js100%100%100%100%
   getClaimedAssetTypes.js100%100%100%100%
   normalization.js99.14%100%100%99.14%285–286, 356, 371
   target.js100%100%100%100%
   validateSchema.js94.67%100%100%94.67%100, 87, 89, 98
lib/container
   ContainerEntryDependency.js100%100%100%100%
   ContainerEntryModule.js100%100%100%100%
   ContainerEntryModuleFactory.js100%100%100%100%
   ContainerExposedDependency.js100%100%100%100%
   ContainerPlugin.js100%100%100%100%
   ContainerReferencePlugin.js100%100%100%100%
   FallbackDependency.js100%100%100%100%
   FallbackItemDependency.js100%100%100%100%
   FallbackModule.js100%100%100%100%
   FallbackModuleFactory.js100%100%100%100%
   HoistContainerReferencesPlugin.js100%100%100%100%
   ModuleFederationPlugin.js100%100%100%100%
   RemoteModule.js100%100%100%100%
   RemoteRuntimeModule.js100%100%100%100%
   RemoteToExternalDependency.js100%100%100%100%
   declaredRemotes.js100%100%100%100%
   moduleFederationHooks.js100%100%100%100%
   options.js100%100%100%100%
lib/context
   ContextExclusionPlugin.js100%100%100%100%
   ContextModule.js99.88%100%100%99.88%1495
   ContextModuleFactory.js95.70%100%100%95.70%230, 257, 276, 280–281, 289, 466, 487, 492, 534, 543, 545, 549, 552, 558–559
   ContextReplacementPlugin.js100%100%100%100%
lib/css
   CssGenerator.js99.29%100%100%99.29%1094, 129, 305, 847
   CssInjectStyleRuntimeModule.js100%100%100%100%
   CssLoadingRuntimeModule.js100%100%100%100%
   CssModule.js100%100%100%100%
   CssModulesPlugin.js98.72%100%100%98.72%217, 418, 432, 445, 458, 481, 494, 507, 520
   CssParser.js99.76%100%100%99.76%1090, 2425, 3072, 3075, 3081, 3514, 943
   CssServerStylesRuntimeModule.js100%100%100%100%
   conventions.js100%100%100%100%
   cssMinify.js100%100%100%100%
   data.js99.84%100%100%99.84%229, 5605
   syntax-parser.js99.69%100%100%99.69%11233–11234, 11786–11787, 11824, 12034, 12083, 14454, 14515–14518, 14623–14624, 14722–14723, 15481, 15777, 15779, 16310, 16702, 16744, 19022, 2631, 3338, 3340, 3345, 3364, 4113, 4119
   syntax-printer.js99.33%100%100%99.33%2614–2615, 2617–2618, 2620–2621, 2626, 2896–2899, 2992, 356, 3693, 4118, 4254, 4972, 5283, 6088, 730
   syntax.js100%100%100%100%
   topologicalSort.js100%100%100%100%
lib/debug
   ProfilingPlugin.js100%100%100%100%
lib/define
   DefinePlugin.js99.42%100%100%99.42%1488, 936, 938, 943, 967
   DotenvPlugin.js97.61%100%100%97.61%276, 395, 443, 456–457
   EnvironmentPlugin.js100%100%100%100%
lib/deno
   DenoTargetPlugin.js100%100%100%100%
lib/dependencies
   ConstDependency.js100%100%100%100%
   ContextElementDependency.js100%100%100%100%
   NullDependency.js100%100%100%100%
lib/dependencies/amd
   AMDDefineDependency.js100%100%100%100%
   AMDDefineDependencyParserPlugin.js100%100%100%100%
   AMDPlugin.js99.31%100%100%99.31%54
   AMDRequireArrayDependency.js100%100%100%100%
   AMDRequireContextDependency.js100%100%100%100%
   AMDRequireDependenciesBlock.js100%100%100%100%
   AMDRequireDependenciesBlockParserPlugin.js100%100%100%100%
   AMDRequireDependency.js100%100%100%100%
   AMDRequireItemDependency.js100%100%100%100%
   AMDRuntimeModules.js95.45%100%100%95.45%65
   LocalModule.js100%100%100%100%
   LocalModuleDependency.js100%100%100%100%
   LocalModulesHelpers.js100%100%100%100%
   RequireJsStuffPlugin.js100%100%100%100%
   UnsupportedDependency.js100%100%100%100%
lib/dependencies/commonjs
   CommonJsDependencyHelpers.js98.63%100%100%98.63%43
   CommonJsExportRequireDependency.js99.21%100%100%99.21%414, 416,

@github-actions

Copy link
Copy Markdown
Contributor

Generated code size

Comparing base (c28d144) to head (bb0c011). Merging this pull request changes the size of 1 asset(s) whose tests it leaves untouched.

What moved Tests Assets Gzip Raw Test edit
Changed, test untouched 1 1 🟢 ↓ -1 B — —
Changed, test edited 0 0 — — —
New 0 0 — — —
Deleted 0 0 — — —
Unchanged — 9083 — — —

Read Changed, test untouched first: the test is byte-identical on both sides, so webpack generated the difference. Changed, test edited moved partly because the test did — Test edit is how many bytes the test gained, and a bundle that grew by less is not a regression. New and Deleted are whole files, not deltas — a new asset was created, not grown, so it reads gray (⚪ added) — and adding tests cannot bury a real change. Gzip decides, because that is what users download; raw is the tiebreak, and brotli and zstd are per asset below.

1 asset(s) changed size, test untouched
Asset Before After Change Gzip (9) Brotli (11) Zstd (19)
🟢 ↓ externals/node-require bundle0.js 502 B 502 B 0 B (—) -1 B (-0.30%) +1 B (+0.36%) -3 B (-0.88%)

No runtime gained or lost a runtime module.

Built test/configCases with the defaults a user gets: 2270 test(s), 9084 asset(s), 101 emitted nothing.

@coderabbitai

coderabbitai Bot commented Oct 20, 2026 •

Copy link
Copy Markdown
Contributor

Review in Change Stack →

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration
  • Configuration used: Repository: webpack/webpack/.coderabbit.yaml
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: b8fd73dd-5dc1-4da5-a723-2e9aa6557c6f
📥 Commits

Reviewing files that changed from the base of the PR and between c28d144 and bb0c011.

📒 Files selected for processing: 2
  • lib/css/syntax-parser.js
  • test/unitCases/css/syntax.unittest.js

Included review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 9 remain after this review.

📝 Walkthrough

The CSS syntax parser now scopes block memoization by parsing context, preserves declaration component metadata for shorthand merging, and caches property-order decisions. It also changes rule-shadow span storage and selector processing. Regression tests cover memo reset and margin longhand merging.

Changes: CSS parser changes

Layer / File(s) Summary
Memoization and parser state lifecycle
lib/css/syntax-parser.js, test/unitCases/css/syntax.unittest.js
Block memo tables are scoped by context. Declaration memo entries retain component metadata, property-order decisions are cached, and parser state cleanup and shorthand merging are updated. Tests cover memo reset and box-longhand merging.
Rule-shadow span tracking
lib/css/syntax-parser.js
Rule scopes store a single prior span directly and promote it to an array when needed. Shadow analysis reads rule text from span offsets.
Selector processing
lib/css/syntax-parser.js
Selector processing adds early checks before detailed scans. Selector-list canonicalization skips sorting when the list is already strictly increasing.

Suggested labels: area: css
Suggested reviewers: xiaoxiaojx

Priority: ⬇️ Low

Change: Refactor

Merge Risk: ⚪ Minimal · up to bb0c0

This change speeds up CSS minification without intending to change output. No actionable merge-blocking risk was found in the supplied context.

Pre-merge checks: Passed 4
Check name Status Explanation
Description Check Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check Passed The title uses the required Conventional Commit form with the allowed type perf and optional scope css. It accurately describes the CSS minifier performance changes.
Linked Issues check Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check Passed Check skipped because no linked issues were found for this pull request.
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Warning

Some tools did not complete. Review the errors below.

🔧 ESLint

If the error stems from missing dependencies, add them to the package.json file. For unrecoverable errors (e.g., due to private dependencies), disable the tool in the CodeRabbit configuration.

lib/css/syntax-parser.js

ESLint failed to execute (timeout).

test/unitCases/css/syntax.unittest.js

ESLint skipped: the matched ESLint configuration already failed (timeout).

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

✨ Finishing Touches: 💡 1
🛠️ Fix failing CI checks: 💡
  • Commit to this branch
  • Create a new PR

@codspeed

codspeed Bot commented Oct 20, 2026

Copy link
Copy Markdown

Merging this PR will degrade performance by 4.54%

⚠️ 3 benchmarks spent significant time in system calls

System calls cannot be consistently instrumented, so they are not included in the measure, which understates the real cost. Please switch to the Walltime instrument to accurately measure system calls.

Measurement and system calls

ℹ️ 10 benchmarks have no flamegraph

The flamegraph could not be generated because too much time was spent in runtime internals, making the visualization not useful for performance analysis.

⚠️ Different runtime environments detected

Some benchmarks with significant performance changes were compared across different runtime environments,
which may affect the accuracy of the results.

Open the report in CodSpeed to investigate

⚡ 1 improved benchmark
❌ 1 regressed benchmark
✅ 213 untouched benchmarks
⏩ 497 skipped benchmarks1
🗄️ 124 archived benchmarks run2

Warning

Please fix the performance issues or acknowledge them on CodSpeed.

Performance Changes

Mode Benchmark BASE HEAD Efficiency
❌ Memory benchmark "html-many-chunks", scenario '{"name":"mode-development","mode":"development"}' 1 MB 1.5 MB -31.86%
⚡ Memory benchmark "wasm-modules-async", scenario '{"name":"mode-development","mode":"development"}' 552.8 KB 413.4 KB +33.72%

Tip

Investigate this regression by commenting @codspeedbot fix this regression on this PR, or directly use the CodSpeed MCP with your agent.


Comparing perf/css-next (bb0c011) with main (c28d144)

Open in CodSpeed

Footnotes

  1. 497 benchmarks were skipped, so the baseline results were used instead. If they were deleted from the codebase, click here and archive them to remove them from the performance reports. ↩

  2. 124 benchmarks were run, but are now archived. If they were deleted in another branch, consider rebasing to remove them from the report. Instead if they were added back, click here to restore them. ↩

@alexander-akait
alexander-akait merged commit 240a0b2 into main Oct 21, 2026
124 of 125 checks passed
@alexander-akait
alexander-akait deleted the perf/css-next branch October 21, 2026 07:40
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area: css CSS support (lib/css)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant