Ensure CSSTree does not hold onto last parsed string

When done compiling, force CSSTree to parse an empty string, so
as to ensure it doesn't keep a reference to that string.

Typically, the string passed to CSSTree is a small slice of a
larger string which is a whole filter list. This means that
holding a reference to the sliced string causes the JS engine
to hold in memory to the whole filter list last parsed.
This commit is contained in:
Raymond Hill 2023-11-08 22:33:01 -05:00
parent f7511cc13f
commit 1dba557c9a
No known key found for this signature in database
GPG Key ID: 25E1490B761470C2
2 changed files with 13 additions and 0 deletions

View File

@ -908,6 +908,10 @@ export class AstFilterParser {
this.scriptletArgListParser = new ArgListParser(',');
}
finish() {
this.selectorCompiler.finish();
}
parse(raw) {
this.raw = raw;
this.rawEnd = raw.length;
@ -3229,6 +3233,14 @@ class ExtSelectorCompiler {
this.error = undefined;
}
// CSSTree library holds onto last string parsed, and this is problematic
// when the string is a slice of a huge parent string (typically a whole
// filter lists), it causes the huge parent string to stay in memory.
// Asking CSSTree to parse an empty string resolves this issue.
finish() {
cssTree.parse('');
}
compile(raw, out, compileOptions = {}) {
this.asProcedural = compileOptions.asProcedural === true;

View File

@ -1102,6 +1102,7 @@ import {
}
compiler.finish(writer);
parser.finish();
// https://github.com/uBlockOrigin/uBlock-issues/issues/1365
// Embed version into compiled list itself: it is encoded in as the