Skip to content

⚡️ perf(css): merge rules across bang comments - #2535

Merged
gaborbernat merged 1 commit into
tox-dev:mainfrom
gaborbernat:perf/css-comment-barriers
Nov 20, 2026
Merged

gaborbernat merged 1 commit into
tox-dev:mainfrom
gaborbernat:perf/css-comment-barriers

Conversation

@gaborbernat

Copy link
Copy Markdown
Member

A /*! ... */ comment between two rules kept them from merging, so on the minify-css-merges row "200 rules with comment barriers" (.aN{color:red} joined by /*!keep*/) turbohtml wrote the input back unchanged. A comment takes no part in the cascade, so rules now merge as if it were absent. The comment is still written once and verbatim, after the merged rule: a{color:red}/*!k*/b{color:red} → a,b{color:red}/*!k*/. A run of same-body rules that spans comments still folds in one pass. Without that, each rule rebuilt the whole selector list and the row cost 4.4x more instructions.

Output bytes, "200 rules with comment barriers" Before After
turbohtml 4881 2891
rcssmin 3090 3090
esbuild 1110 1110
lightningcss 1100 1100

The remaining gap is the 199 copies of /*!keep*/ (1791 bytes). lightningcss drops bang comments, and esbuild keeps one copy of each distinct legal comment. Both lose license text that turbohtml documents it keeps, so neither transform is done here. No other minify-css, minify-css-merges or minify-css-conflicts case changes size, animate.css, pico and bootstrap included.

Callgrind, release+LTO, Ir per call counted inside the extension, (n=50 − n=10)/40: the worst row is +0.42% (minify-css "320 rules, unitless exponent"), and the barrier row is −7.7%. Whole-process Ir moved by ±20% between runs of the same build. That came from glibc realloc copying or growing in place depending on heap layout, so it was excluded.

A /*! ... */ comment between two rules kept them apart, so a stylesheet
that separates identical rules with license comments never merged them.
A comment takes no part in the cascade: the rules now merge as if it
were absent, and the comment is written after the merged rule, still
once and verbatim. A rule run that spans comments still folds in one
pass, so the merge stays linear.
@codspeed

codspeed Bot commented Nov 20, 2026

Copy link
Copy Markdown

Merging this PR will not alter performance

✅ 580 untouched benchmarks
⏩ 91 skipped benchmarks1


Comparing gaborbernat:perf/css-comment-barriers (b27fac1) with main (bc7df81)

Open in CodSpeed

Footnotes

  1. 91 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. ↩

@gaborbernat
gaborbernat merged commit 7763495 into tox-dev:main Nov 20, 2026
58 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant