Skip to content

Commit 55d1af4

Browse files
fix(css): release the printed-text store's text when it is dropped (#21958)
* fix(css): release the printed-text store's text when it is dropped Moving the epoch on stopped the store answering for a node but left its text referenced, so what `dropStore` documents as dropped stayed reachable until the column's slot happened to be reused. The range written since the epoch last moved is now cleared with it, which keeps the drop O(1) per node and caps occupancy at 46 KB rather than 328 KB on a 30 MB stylesheet. * fix(css): cover the take paths and shorten the store comments `take` and `takeRetractable` move the epoch too, so each gets a case asserting the store holds nothing afterwards; both fail without the release. The constructor's store comment goes to the two lines `lib/` allows, and `_moveEpoch`'s description drops the counterfactual.
1 parent b069013 commit 55d1af4

3 files changed

Lines changed: 102 additions & 6 deletions

File tree

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+
Release the printed-text store's text when the CSS printer drops it.

‎lib/util/SourceProcessor.js‎

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -261,14 +261,17 @@ class PrintContext {
261261
/** @type {NodePrinter<TPath, TNode, TPrintOptions>} */
262262
this._printer = printer;
263263
// Printed text by node handle, in two columns rather than one `Map`: the
264-
// handles are dense integers, and a stylesheet prints ~300k nodes through
265-
// a store that never holds more than a few dozen of them at once.
264+
// handles are dense integers, and `_moveEpoch` keeps the live set small.
266265
/** @type {string[]} each finished node's printed text */
267266
this._storeText = [];
268267
/** @type {Int32Array} the epoch each slot was written in */
269268
this._storeGen = new Int32Array(0);
270269
/** @type {number} current epoch; a slot from an older one reads as absent */
271270
this._gen = 1;
271+
/** @type {number} lowest slot written since the epoch last moved */
272+
this._storeLow = 0x7fffffff;
273+
/** @type {number} highest slot written since the epoch last moved */
274+
this._storeHigh = -1;
272275
// Output accumulates into `_tail`, a plain string append, so printing that
273276
// never takes anything back costs exactly what appending to one string
274277
// costs. A piece is cut off into `_chunks` only for text that may still be
@@ -424,8 +427,25 @@ class PrintContext {
424427
* node's text.
425428
*/
426429
dropStore() {
427-
// Bumping the epoch invalidates every slot at once, so this stays O(1) —
428-
// it runs per node, and clearing a column by length would not.
430+
this._moveEpoch();
431+
}
432+
433+
/**
434+
* Move the epoch on and release the text every slot written under the old one
435+
* holds. Only the range actually written is walked, so the drop stays O(1)
436+
* per node.
437+
* @returns {void}
438+
*/
439+
_moveEpoch() {
440+
const high = this._storeHigh;
441+
if (high !== -1) {
442+
// `""` rather than a hole: the column is packed, and a hole in it would
443+
// cost every read after this one.
444+
const text = this._storeText;
445+
for (let i = this._storeLow; i <= high; i++) text[i] = "";
446+
this._storeLow = 0x7fffffff;
447+
this._storeHigh = -1;
448+
}
429449
this._gen++;
430450
}
431451

@@ -469,6 +489,8 @@ class PrintContext {
469489
if (n >= this._storeText.length) this._growStore(n + 1);
470490
this._storeText[n] = text;
471491
this._storeGen[n] = this._gen;
492+
if (n < this._storeLow) this._storeLow = n;
493+
if (n > this._storeHigh) this._storeHigh = n;
472494
}
473495

474496
/**
@@ -598,7 +620,7 @@ class PrintContext {
598620
this.anchor(srcOffset, srcLine, srcCol);
599621
const mapping = this._mappings.length > before ? before : -1;
600622
const at = this.emitRetractable(text === undefined ? this.get(node) : text);
601-
this._gen++;
623+
this._moveEpoch();
602624
if (mapping !== -1) {
603625
if (this._retractableMappings === null) {
604626
this._retractableMappings = new Map();
@@ -627,7 +649,7 @@ class PrintContext {
627649
this._emit(
628650
text === undefined ? this.get(/** @type {TNode} */ (node)) : text
629651
);
630-
this._gen++;
652+
this._moveEpoch();
631653
}
632654

633655
/**

‎test/SourceProcessor.unittest.js‎

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ const {
88
NodeType: HtmlNodeType,
99
SourceProcessor: HtmlSourceProcessor
1010
} = require("../lib/html/syntax");
11+
const { PrintContext } = require("../lib/util/SourceProcessor");
1112

1213
const CSS = "a {\n\tcolor : #ff0000 ;\n}\n";
1314
const HTML = "<div class='a' >\n <p>x</p>\n</div>\n";
@@ -21,6 +22,74 @@ const LANGUAGES = [
2122
];
2223

2324
describe("SourceProcessor", () => {
25+
// Dropping the store has to let go of the text, not just stop answering for
26+
// it: a stylesheet prints ~300k nodes, which is the output over again.
27+
describe("printed-text store", () => {
28+
/**
29+
* @param {InstanceType<typeof PrintContext>} context print context
30+
* @returns {number} characters the store still holds
31+
*/
32+
const held = (context) => {
33+
let chars = 0;
34+
for (let i = 0; i < context._storeText.length; i++) {
35+
chars += context._storeText[i].length;
36+
}
37+
return chars;
38+
};
39+
40+
it("lets go of the text when the epoch moves", () => {
41+
const context = new PrintContext({ mode: "minify" }, () =>
42+
"x".repeat(64)
43+
);
44+
for (let node = 0; node < 500; node++) context.printNode(node, null);
45+
expect(held(context)).toBe(500 * 64);
46+
context.dropStore();
47+
expect(held(context)).toBe(0);
48+
});
49+
50+
it("holds only what was printed since the last drop", () => {
51+
const context = new PrintContext({ mode: "minify" }, () => "y".repeat(8));
52+
for (let node = 0; node < 300; node++) {
53+
context.printNode(node, null);
54+
if (node % 10 === 9) context.dropStore();
55+
}
56+
expect(held(context)).toBe(0);
57+
context.printNode(300, null);
58+
expect(held(context)).toBe(8);
59+
});
60+
61+
it("lets go of the text when a node is taken", () => {
62+
const context = new PrintContext({ mode: "minify" }, () =>
63+
"t".repeat(16)
64+
);
65+
context.printNode(1, null);
66+
expect(held(context)).toBe(16);
67+
context.take(1);
68+
expect(held(context)).toBe(0);
69+
});
70+
71+
it("lets go of the text when a retractable node is taken", () => {
72+
const context = new PrintContext({ mode: "minify" }, () =>
73+
"r".repeat(16)
74+
);
75+
context.printNode(1, null);
76+
expect(held(context)).toBe(16);
77+
context.takeRetractable(1);
78+
expect(held(context)).toBe(0);
79+
});
80+
81+
it("answers for a node printed since the drop, and no earlier one", () => {
82+
const context = new PrintContext({ mode: "minify" }, () => "z");
83+
context.printNode(1, null);
84+
expect(context.get(1)).toBe("z");
85+
context.dropStore();
86+
expect(context.get(1)).toBeUndefined();
87+
context.printNode(2, null);
88+
expect(context.get(2)).toBe("z");
89+
expect(context.get(1)).toBeUndefined();
90+
});
91+
});
92+
2493
// `mode` is the only thing that names the output, and it resolves in the
2594
// shared processor — so both languages bound to it must agree.
2695
describe("mode", () => {

0 commit comments

Comments
 (0)