Skip to content

Commit 3eef284

Browse files
perf(html): join a <style> run's sources and minify it once
A sheet whose source ends at a rule (a `}` or an at-rule's `;`, outside every string, comment and block) is appended to as written, so a run is offered to the CSS minifier once: no per-sheet renders, and no second print on the async path. Only a sheet the input left open is still joined on its answer, which is the one case processAsync has to print again for.
1 parent 19d1921 commit 3eef284

4 files changed

Lines changed: 129 additions & 147 deletions

File tree

‎lib/html/syntax-parser.js‎

Lines changed: 84 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -291,6 +291,7 @@ const CC_LESS_THAN = 0x3c;
291291
const CC_EQUALS = 0x3d;
292292
const CC_GREATER_THAN = 0x3e;
293293
const CC_QUESTION_MARK = 0x3f;
294+
const CC_COMMERCIAL_AT = 0x40;
294295
const CC_LEFT_SQUARE_BRACKET = 0x5b;
295296
const CC_REVERSE_SOLIDUS = 0x5c;
296297
const CC_RIGHT_SQUARE_BRACKET = 0x5d;
@@ -9835,13 +9836,12 @@ const _takeRenderedSheet = (css) => {
98359836
*/
98369837
const _sheetDecides = (css) => {
98379838
const emptying = _removeEmptyElements;
9838-
const joining = _S._mergeStyles;
9839+
// A run is joined on its answer only where its text is left open.
9840+
const joining = _S._mergeStyles && !_sheetEndsAtRule(css);
98399841
if (!emptying && !joining) return undefined;
9840-
const clean = joining && _sheetEndsCleanly(css);
98419842
return (answer) =>
98429843
typeof answer === "string" &&
9843-
((emptying && answer === "") ||
9844-
(joining && _sheetEndsCleanly(answer) !== clean));
9844+
((emptying && answer === "") || (joining && _sheetEndsAtRule(answer)));
98459845
};
98469846

98479847
// An at-rule that only applies at the top of a sheet, so a second sheet carrying
@@ -9930,21 +9930,40 @@ const _mergeableStyleAfter = (path, element) => {
99309930
};
99319931

99329932
/**
9933-
* Whether a sheet ends outside every string, comment and bracket, so a sheet
9934-
* appended to it starts rules of its own rather than finishing this one's last.
9933+
* Whether a sheet ends where a rule does: outside every string, comment and
9934+
* block, after a `}` or an at-rule's `;`. A sheet appended to it then starts
9935+
* rules of its own (§5.4) rather than finishing this one's last.
99359936
* @param {string} css the sheet's text
9936-
* @returns {boolean} true when another sheet may follow it
9937+
* @returns {boolean} true when another sheet may follow it as written
99379938
*/
9938-
const _sheetEndsCleanly = (css) => {
9939-
let depth = 0;
9939+
const _sheetEndsAtRule = (css) => {
9940+
/** @type {number[]} */
9941+
const closers = [];
9942+
// Whether nothing has been read since a rule ended, and whether the
9943+
// statement being read is an at-rule, which a `;` ends.
9944+
let ended = true;
9945+
let atRule = false;
99409946
for (let i = 0; i < css.length; i++) {
99419947
const c = css.charCodeAt(i);
9942-
if (c === CC_REVERSE_SOLIDUS) {
9943-
i++;
9944-
} else if (c === CC_SOLIDUS && css.charCodeAt(i + 1) === CC_ASTERISK) {
9948+
if (isAsciiWhitespace(c)) continue;
9949+
if (c === CC_SOLIDUS && css.charCodeAt(i + 1) === CC_ASTERISK) {
99459950
const close = css.indexOf("*/", i + 2);
99469951
if (close === -1) return false;
99479952
i = close + 1;
9953+
continue;
9954+
}
9955+
const top = closers.length === 0;
9956+
if (top && ended) {
9957+
// §5.4.1 skips a CDO or CDC where a rule may start.
9958+
if (css.startsWith("<!--", i) || css.startsWith("-->", i)) {
9959+
i += c === CC_LESS_THAN ? 3 : 2;
9960+
continue;
9961+
}
9962+
atRule = c === CC_COMMERCIAL_AT;
9963+
}
9964+
if (top) ended = false;
9965+
if (c === CC_REVERSE_SOLIDUS) {
9966+
i++;
99489967
} else if (c === CC_QUOTATION_MARK || c === CC_APOSTROPHE) {
99499968
// A string ends at its quote, or at a newline as a bad string does.
99509969
for (i++; i < css.length; i++) {
@@ -9953,22 +9972,57 @@ const _sheetEndsCleanly = (css) => {
99539972
else if (d === c || d === CC_LF) break;
99549973
}
99559974
if (i >= css.length) return false;
9956-
} else if (
9957-
c === CC_LEFT_CURLY_BRACKET ||
9958-
c === CC_LEFT_PARENTHESIS ||
9959-
c === CC_LEFT_SQUARE_BRACKET
9960-
) {
9961-
depth++;
9962-
} else if (
9963-
depth !== 0 &&
9964-
(c === CC_RIGHT_CURLY_BRACKET ||
9965-
c === CC_RIGHT_PARENTHESIS ||
9966-
c === CC_RIGHT_SQUARE_BRACKET)
9967-
) {
9968-
depth--;
9975+
} else if (c === CC_LEFT_CURLY_BRACKET) {
9976+
closers.push(CC_RIGHT_CURLY_BRACKET);
9977+
} else if (c === CC_LEFT_PARENTHESIS) {
9978+
closers.push(CC_RIGHT_PARENTHESIS);
9979+
} else if (c === CC_LEFT_SQUARE_BRACKET) {
9980+
closers.push(CC_RIGHT_SQUARE_BRACKET);
9981+
} else if (!top && c === closers[closers.length - 1]) {
9982+
// §5.4.8 ends a block only on its own closer; any other is a token in it.
9983+
closers.pop();
9984+
if (closers.length === 0 && c === CC_RIGHT_CURLY_BRACKET) ended = true;
9985+
} else if (top && atRule && c === CC_SEMICOLON) {
9986+
ended = true;
99699987
}
99709988
}
9971-
return depth === 0;
9989+
return ended;
9990+
};
9991+
9992+
/**
9993+
* The text a `<style>` run prints as one sheet, minified once. Each sheet's
9994+
* source is appended while the text before it ends at a rule; where it is left
9995+
* open, the renderer's answer for it is appended to instead, if that ends at one.
9996+
* @param {HtmlPath} path the accessor
9997+
* @param {HtmlElement} element the run's first `<style>`
9998+
* @param {string} css its own text
9999+
* @param {boolean} absorb whether to mark the rest of the run as printed here
10000+
* @returns {string} the run's text
10001+
*/
10002+
const _styleRunText = (path, element, css, absorb) => {
10003+
let text = css;
10004+
let ends = _sheetEndsAtRule(css);
10005+
for (
10006+
let next = _mergeableStyleAfter(path, element);
10007+
next !== 0;
10008+
next = _mergeableStyleAfter(path, next)
10009+
) {
10010+
const source = _nodeStrings[_onlyTextChild(next)];
10011+
if (_SHEET_LEADING_AT_RULE_REGEXP.test(source)) break;
10012+
if (!ends) {
10013+
// Without an answer yet, `_sheetDecides` has the print run again.
10014+
if (!_rendersNow()) break;
10015+
text = _renderedSheet(text);
10016+
if (!_sheetEndsAtRule(text)) break;
10017+
}
10018+
if (absorb) {
10019+
if (_S._absorbed === null) _S._absorbed = new Set();
10020+
_S._absorbed.add(next);
10021+
}
10022+
text += source;
10023+
ends = _sheetEndsAtRule(source);
10024+
}
10025+
return text;
997210026
};
997310027

997410028
/**
@@ -9984,21 +10038,8 @@ const _sheetPrintsNothing = (path, element) => {
998410038
return false;
998510039
}
998610040
const css = _nodeStrings[first];
9987-
if (css.trim() === "" || _renderedSheet(css) !== "") return false;
9988-
if (!_S._mergeStyles) return true;
9989-
// The run `_mergedStyleRun` builds, read with the same two breaks.
9990-
for (
9991-
let next = _mergeableStyleAfter(path, element);
9992-
next !== 0;
9993-
next = _mergeableStyleAfter(path, next)
9994-
) {
9995-
const text = _nodeStrings[_onlyTextChild(next)];
9996-
if (_SHEET_LEADING_AT_RULE_REGEXP.test(text)) break;
9997-
const printed = text.trim() === "" ? text : _renderedSheet(text);
9998-
if (!_sheetEndsCleanly(printed)) break;
9999-
if (printed !== "") return false;
10000-
}
10001-
return true;
10041+
const text = _S._mergeStyles ? _styleRunText(path, element, css, false) : css;
10042+
return text.trim() !== "" && _renderedSheet(text) === "";
1000210043
};
1000310044

1000410045
/**
@@ -13611,7 +13652,8 @@ module.exports._sheetDecides = _sheetDecides;
1361113652
module.exports._SHEET_LEADING_AT_RULE_REGEXP = _SHEET_LEADING_AT_RULE_REGEXP;
1361213653
module.exports._mergeableStyleAfter = _mergeableStyleAfter;
1361313654
module.exports._onlyTextChild = _onlyTextChild;
13614-
module.exports._sheetEndsCleanly = _sheetEndsCleanly;
13655+
module.exports._sheetEndsAtRule = _sheetEndsAtRule;
13656+
module.exports._styleRunText = _styleRunText;
1361513657
module.exports.baseTag = baseTag;
1361613658
module.exports.buildHeadTags = buildHeadTags;
1361713659
module.exports.collapseWhitespaceRuns = collapseWhitespaceRuns;

‎lib/html/syntax-printer.js‎

Lines changed: 9 additions & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ const {
2525
NodeType,
2626
SVG_TYPE,
2727
_S,
28-
_SHEET_LEADING_AT_RULE_REGEXP,
2928
_asciiLowerCase,
3029
_asciiTrim,
3130
_attributePrints,
@@ -47,7 +46,6 @@ const {
4746
_isRemovableEmptyElement,
4847
_isSvgRoot,
4948
_keepComment,
50-
_mergeableStyleAfter,
5149
_minifyOpenTag,
5250
_nodeStrings,
5351
_onlyTextChild,
@@ -57,9 +55,9 @@ const {
5755
_renderEmbeddedOrNull,
5856
_rendersNow,
5957
_sheetDecides,
60-
_sheetEndsCleanly,
6158
_startsCharacterReference,
6259
_startsOutsideLineBox,
60+
_styleRunText,
6361
_synthesizeOpenTag,
6462
_takeRenderedSheet,
6563
collapseWhitespaceRuns,
@@ -272,28 +270,15 @@ const _renderEmbedded = (source, type, as, decides) => {
272270
* minifies no CSS here itself: `builtinEmbeddedRenderer` is what a caller
273271
* passes for that.
274272
* @param {string} css the element's text
275-
* @param {boolean=} joined whether it is one of several sheets a run prints as one, which is printed again once they are answered
276273
* @returns {string} the rendered text, the marker standing for it, or the text as written
277274
*/
278-
const _minifyStyleBody = (css, joined) => {
275+
const _minifyStyleBody = (css) => {
279276
if (css.trim() === "") return css;
280277
// Through the emptiness check's memo, which may have rendered it already.
281278
if (_rendersNow()) return _takeRenderedSheet(css);
282-
return _renderEmbedded(
283-
css,
284-
CSS_TYPE,
285-
undefined,
286-
joined ? _answerJoins : _sheetDecides(css)
287-
);
279+
return _renderEmbedded(css, CSS_TYPE, undefined, _sheetDecides(css));
288280
};
289281

290-
/**
291-
* The `decides` of a sheet a run joins: the run is rendered as the one sheet it
292-
* prints as, which only a print holding each sheet's answer can offer.
293-
* @returns {boolean} true
294-
*/
295-
const _answerJoins = () => true;
296-
297282
/**
298283
* A `<script>`'s lowercased `type`, or `""` when it has none.
299284
* @param {HtmlPath} path the accessor
@@ -324,79 +309,17 @@ const _minifyInlineJson = (json) =>
324309
json.trim() === "" ? json : _renderEmbedded(json, JSON_TYPE);
325310

326311
/**
327-
* The text the first `<style>` of a run prints: every sheet in it, each offered
328-
* on its own and then joined, the rest of the run marked absorbed so their tags
329-
* and text print as nothing. A sheet joins when what it printed ends cleanly —
330-
* its source where the answer is still a marker — and not after the first when it
331-
* carries `@import` (or `@charset` / `@namespace`), which would stop applying.
312+
* The text the first `<style>` of a run prints: every sheet in it joined and
313+
* offered as one, the rest of the run marked absorbed so their tags and text
314+
* print as nothing. A sheet after the first carrying `@import` (or `@charset` /
315+
* `@namespace`) is not joined, since it would stop applying.
332316
* @param {HtmlPath} path the accessor
333317
* @param {HtmlElement} element the run's first `<style>`
334318
* @param {string} css its own text
335319
* @returns {string} the run's text
336320
*/
337-
const _mergedStyleRun = (path, element, css) => {
338-
if (!_rendersNow()) return _deferredStyleRun(path, element, css);
339-
let out = _minifyStyleBody(css);
340-
// What is joined is what printed, so that is what has to end cleanly — the
341-
// renderer closes what a sheet left open.
342-
if (!_sheetEndsCleanly(out)) return out;
343-
let joined = false;
344-
for (
345-
let next = _mergeableStyleAfter(path, element);
346-
next !== 0;
347-
next = _mergeableStyleAfter(path, next)
348-
) {
349-
const text =
350-
_nodeStrings[/** @type {HtmlNodeRef} */ (_onlyTextChild(next))];
351-
if (_SHEET_LEADING_AT_RULE_REGEXP.test(text)) break;
352-
const printed = _minifyStyleBody(text);
353-
if (!_sheetEndsCleanly(printed)) break;
354-
if (_S._absorbed === null) _S._absorbed = new Set();
355-
_S._absorbed.add(next);
356-
out += printed;
357-
joined = true;
358-
}
359-
// Joined, the sheets are one, and the renderer reads rules across them as a
360-
// second pass over this element would; a blank sheet joined is no longer one.
361-
return joined ? _minifyStyleBody(out) : out;
362-
};
363-
364-
/**
365-
* `_mergedStyleRun` where the answers are still to come: a marker says nothing
366-
* of how its sheet ends, so the run is read off the sources, and a run of more
367-
* than one has `processAsync` print again with the answers in hand.
368-
* @param {HtmlPath} path the accessor
369-
* @param {HtmlElement} element the run's first `<style>`
370-
* @param {string} css its own text
371-
* @returns {string} the run's text
372-
*/
373-
const _deferredStyleRun = (path, element, css) => {
374-
/** @type {string[]} */
375-
const texts = [];
376-
if (_sheetEndsCleanly(css)) {
377-
for (
378-
let next = _mergeableStyleAfter(path, element);
379-
next !== 0;
380-
next = _mergeableStyleAfter(path, next)
381-
) {
382-
const text =
383-
_nodeStrings[/** @type {HtmlNodeRef} */ (_onlyTextChild(next))];
384-
if (
385-
!_sheetEndsCleanly(text) ||
386-
_SHEET_LEADING_AT_RULE_REGEXP.test(text)
387-
) {
388-
break;
389-
}
390-
if (_S._absorbed === null) _S._absorbed = new Set();
391-
_S._absorbed.add(next);
392-
texts.push(text);
393-
}
394-
}
395-
const joined = texts.length !== 0;
396-
let out = _minifyStyleBody(css, joined);
397-
for (const text of texts) out += _minifyStyleBody(text, joined);
398-
return out;
399-
};
321+
const _mergedStyleRun = (path, element, css) =>
322+
_minifyStyleBody(_styleRunText(path, element, css, true));
400323

401324
/**
402325
* Whether a script body ends where another can be appended after it. A body

‎lib/util/SourceProcessor.js‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,8 @@ const deferredKey = (offer) => {
170170
};
171171

172172
// How many times `processAsync` prints again: an answer can make a print offer
173-
// something new (a joined run, once each sheet in it is answered), not forever.
173+
// something new (a `<style>` run joined after a sheet the input left open, once
174+
// that sheet is answered), not forever.
174175
const MAX_REPRINTS = 3;
175176

176177
/**

0 commit comments

Comments
 (0)