Skip to content

Commit

Permalink
Ensure CSSTree does not hold onto last parsed string
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
gorhill committed Nov 9, 2023
1 parent f7511cc commit 1dba557
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 0 deletions.
12 changes: 12 additions & 0 deletions src/js/static-filtering-parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -908,6 +908,10 @@ export class AstFilterParser {
this.scriptletArgListParser = new ArgListParser(',');
}

finish() {
this.selectorCompiler.finish();
}

parse(raw) {
this.raw = raw;
this.rawEnd = raw.length;
Expand Down Expand Up @@ -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;

Expand Down
1 change: 1 addition & 0 deletions src/js/storage.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 1dba557

Please sign in to comment.