Repository navigation
🐛 fix(css): keep separators minify_css needs - #2198
Conversation
Minifying a stylesheet a second time changed its output, because the first pass removed or merged whitespace and comments that the next pass reads as token boundaries. The cases fixed here: - A space between `u+` and a following name, number or `?` that starts with a hex digit is kept, so the output does not read as a unicode-range token. The range scan also stops at a `?` that is followed by a hex digit. - A comment or space that separated a range token, a `?` or a `+` from the next piece is kept, in declarations and in custom property values. - A universal selector followed by a comment and then a class, hash, attribute or pseudo-class is dropped, as the comment-free form is. - A folded rgb() or hsl() color followed by a name is separated from it. - A digit ending an identifier no longer gets a space before a following percent sign. - A bang comment inside a function argument that is kept as written is written once, where it stands, instead of again after the rule. - A raw span that ended at a bang comment read its end from the comment's placeholder, which is outside the stylesheet; the span now ends at the last token before it. Fixes tox-dev#1835
Merging this PR will improve performance by 19.4%2 warnings
Performance Changes
Tip Curious why performance improved? Comment Comparing Footnotes
|
The comp vector carries a spaced flag that the value assembler reads. Several shorthand paths declared components without an initializer, so the flag could be read uninitialized, which -Wmaybe-uninitialized reports under -Werror.
# Conflicts: # src/turbohtml/_c/css/minify/css_calc.h # src/turbohtml/_c/css/minify/css_selector.h
`minify_css` and `minify_css_inline` gave a different result on a second pass for some input with stray brackets, hex escapes or CDO/CDC tokens. Four causes, each fixed here: - A declaration whose name opened a block before its colon was kept on the first pass and dropped on the second. CSS Syntax 3 §5.4.5 makes a declaration start with an ident, so such a segment is no declaration, and the first pass now drops it. - An ident written apart from a `(` in stray text was joined to it, and the two formed a function token. The next pass then closed that function. The minifier now writes a space between them, which keeps the tokens apart. - A nested `calc()` or group left open at the end of the input stayed unfolded on the first pass and folded on the second. The end of the input closes such a group (CSS Syntax 3 §5.4.7), so the first pass now folds it. A block left open after a hex escape also gets the closing space that the second pass writes. - A raw at-keyword name that ends in a hex escape took no closing space, so the second pass, reading the escape's space back as part of the name, wrote one more space. The first pass now writes the closing space a spelled name already has. - Stray text that was written after the last delimiter lost the delimiter owed before an at-statement that followed, when an empty `}` or `;` came between them. The first pass joined the two, and the second pass split them again. The delimiter is now kept. The tests cover a table of stray-bracket, hex-escape and CDO/CDC shapes for both functions, a seeded property test over 10000 generated inputs (the generator includes at-keywords, hex escapes, `<!--`, `-->` and `}`), and a fixed-point check on each expected output. ### Behavior changes - A declaration whose name holds a block opened before its colon is dropped, in a stylesheet and in a `style` attribute. - A stray ident followed by `(` in stylesheet text is separated from it by a space. - A nested `calc()` or group left open at the end of the input folds to its value. - A block left open at the end of the input after a hex escape closes after a space. - A raw at-keyword ending in a hex escape before a prelude gets two spaces before the prelude, as a spelled name does. - A stray CDO or CDC followed by an at-statement, with no delimiter between, gets a `;` before the at-statement. ### Performance Callgrind on release LTO builds (`-Dbuildtype=release -Db_lto=true`, `CC=/usr/bin/cc`) of the base and the head, built at the same path length. Run with `PYTHONHASHSEED=0` under `setarch -R` at 20 and 220 repetitions; the table shows the per-repetition difference in instructions. | Benchmark | base (Ir/rep) | head (Ir/rep) | delta | | ---------------------------- | ------------: | ------------: | -----: | | minify-css | 7704434.0 | 7709557.0 | +0.07% | | minify-css-conflicts | 21345660.0 | 21366630.0 | +0.10% | | minify-css-merges | 49154.0 | 49264.0 | +0.22% | | minify-css-number-arithmetic | 742604.0 | 742992.0 | +0.05% | | minify-css-nested-functions | 196440.0 | 196447.0 | 0.00% | | minify-js (control, no CSS) | 11692216.0 | 11692216.0 | 0.00% | The base is `99c346fb1` (upstream #2198), the head `6da3b85ce`, which merges that base with this branch. Both builds are release LTO, built fresh at the same path length. A second run of the base gives exactly 0 on `minify-css`, so the noise floor is zero. Four rows are above the +0.05% noise line, and all are within the 5% ceiling. The changes in this PR that reach the measured paths are the at-rule prelude separator, the stray-text delimiter bookkeeping, and the declaration name check. An earlier round on the merges row removed most of its cost (gating the new check on the escape flag), but the remainder is not confirmed to come from the changed code, since no perturbation build was run. The two optimization rounds on this PR are spent, so the remaining rows are recorded here rather than optimized further. Fixes #1853
Minifying a stylesheet a second time could change it, because the first pass dropped or merged whitespace and comments that the next pass reads as token boundaries. Pipelines that minify more than once got different output on each run. 🧩 Once this merges, the minified stylesheet is stable on repeat, and the separators it needs stay in place. Fixes #1835.
The minifier now keeps a separator wherever the next token would otherwise read differently under the CSS tokenizer. That covers unicode-range pieces, a folded color followed by a number, and a universal selector or a class that a comment separates from the rest of its compound. A bang comment inside a kept function argument is written once, where it stands.
Behavior changes
unicode-rangeas hex digits followed by?wildcards, with no-range after a wildcard (CSS Syntax 3 §4.3.6).