Skip to content

Commit 725e693

Browse files
committed
Refactoring work in static network filtering engine
The original motivation is to further speed up launch time for either non-selfie-based and selfie-based initialization of the static network filtering engine (SNFE). As a result of the refactoring: Filters are no longer instance-based, they are sequence-of- integer-based. This eliminates the need to create instances of filters at launch, and consequently eliminates all the calls to class constructors, the resulting churning of memory, and so forth. All the properties defining filter instances are now as much as possible 32-bit integer-based, and these are allocated in a single module-scoped typed array -- this eliminates the need to allocate memory for every filter being instantiated. Not all filter properties can be represented as a 32-bit integer, and in this case a filter class can allocate slots into another module-scoped array of references. As a result, this eliminates a lot of memory allocations when the SNFE is populated with filters, and this makes the saving and loading of selfie more straightforward, as the operation is reduced to saving/loading two arrays, one of 32-bit integers, and the other, much smaller, an array JSON-able values. All filter classes now only contain static methods, and all of these methods are called with an index to the specific filter data in the module-scoped array of 32-bit integers. The filter sequences (used to avoid the use of JS arrays) are also allocated in the single module-scoped array of 32-bit integers -- they used to be stored in their own dedicated array. Additionally, some filters are now loaded more in a deferred way, so as reduce uBO's time-to-readiness -- the outcome of this still needs to be evaluated, time-to-readiness is especially a concern in Firefox for Android or less powerful computers.
1 parent 64f427d commit 725e693

File tree

6 files changed

+1466
-1752
lines changed

6 files changed

+1466
-1752
lines changed

src/js/background.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,8 @@ const hiddenSettingsDefault = {
4646
allowGenericProceduralFilters: false,
4747
assetFetchTimeout: 30,
4848
autoCommentFilterTemplate: '{{date}} {{origin}}',
49-
autoUpdateAssetFetchPeriod: 120,
50-
autoUpdateDelayAfterLaunch: 180,
49+
autoUpdateAssetFetchPeriod: 60,
50+
autoUpdateDelayAfterLaunch: 105,
5151
autoUpdatePeriod: 4,
5252
benchmarkDatasetURL: 'unset',
5353
blockingProfiles: '11111/#F00 11010/#C0F 11001/#00F 00001',
@@ -78,7 +78,7 @@ const hiddenSettingsDefault = {
7878
popupPanelLockedSections: 0,
7979
popupPanelHeightMode: 0,
8080
requestJournalProcessPeriod: 1000,
81-
selfieAfter: 3,
81+
selfieAfter: 2,
8282
strictBlockingBypassDuration: 120,
8383
suspendTabsUntilReady: 'unset',
8484
uiPopupConfig: 'unset',
@@ -175,8 +175,8 @@ const µBlock = { // jshint ignore:line
175175

176176
// Read-only
177177
systemSettings: {
178-
compiledMagic: 39, // Increase when compiled format changes
179-
selfieMagic: 39, // Increase when selfie format changes
178+
compiledMagic: 40, // Increase when compiled format changes
179+
selfieMagic: 40, // Increase when selfie format changes
180180
},
181181

182182
// https://github.com/uBlockOrigin/uBlock-issues/issues/759#issuecomment-546654501

src/js/biditrie.js

Lines changed: 86 additions & 118 deletions
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ const toSegmentInfo = (aL, l, r) => ((r - l) << 24) | (aL + l);
125125
const roundToPageSize = v => (v + PAGE_SIZE-1) & ~(PAGE_SIZE-1);
126126

127127

128-
const BidiTrieContainer = class {
128+
class BidiTrieContainer {
129129

130130
constructor(extraHandler) {
131131
const len = PAGE_SIZE * 4;
@@ -177,6 +177,19 @@ const BidiTrieContainer = class {
177177
this.lastStoredLen = this.lastStoredIndex = 0;
178178
}
179179

180+
createTrie() {
181+
// grow buffer if needed
182+
if ( (this.buf32[CHAR0_SLOT] - this.buf32[TRIE1_SLOT]) < CELL_BYTE_LENGTH ) {
183+
this.growBuf(CELL_BYTE_LENGTH, 0);
184+
}
185+
const iroot = this.buf32[TRIE1_SLOT] >>> 2;
186+
this.buf32[TRIE1_SLOT] += CELL_BYTE_LENGTH;
187+
this.buf32[iroot+CELL_OR] = 0;
188+
this.buf32[iroot+CELL_AND] = 0;
189+
this.buf32[iroot+SEGMENT_INFO] = 0;
190+
return iroot;
191+
}
192+
180193
matches(icell, ai) {
181194
const buf32 = this.buf32;
182195
const buf8 = this.buf8;
@@ -284,25 +297,9 @@ const BidiTrieContainer = class {
284297
return 1;
285298
}
286299

287-
createOne(args) {
288-
if ( Array.isArray(args) ) {
289-
return new this.STrieRef(this, args[0], args[1]);
290-
}
291-
// grow buffer if needed
292-
if ( (this.buf32[CHAR0_SLOT] - this.buf32[TRIE1_SLOT]) < CELL_BYTE_LENGTH ) {
293-
this.growBuf(CELL_BYTE_LENGTH, 0);
294-
}
295-
const iroot = this.buf32[TRIE1_SLOT] >>> 2;
296-
this.buf32[TRIE1_SLOT] += CELL_BYTE_LENGTH;
297-
this.buf32[iroot+CELL_OR] = 0;
298-
this.buf32[iroot+CELL_AND] = 0;
299-
this.buf32[iroot+SEGMENT_INFO] = 0;
300-
return new this.STrieRef(this, iroot, 0);
301-
}
302-
303-
compileOne(trieRef) {
304-
return [ trieRef.iroot, trieRef.size ];
305-
}
300+
get $l() { return this.buf32[RESULT_L_SLOT] | 0; }
301+
get $r() { return this.buf32[RESULT_R_SLOT] | 0; }
302+
get $iu() { return this.buf32[RESULT_IU_SLOT] | 0; }
306303

307304
add(iroot, aL0, n, pivot = 0) {
308305
const aR = n;
@@ -561,6 +558,14 @@ const BidiTrieContainer = class {
561558
}
562559
}
563560

561+
getExtra(iboundary) {
562+
return this.buf32[iboundary+BCELL_EXTRA];
563+
}
564+
565+
setExtra(iboundary, v) {
566+
this.buf32[iboundary+BCELL_EXTRA] = v;
567+
}
568+
564569
optimize(shrink = false) {
565570
if ( shrink ) {
566571
this.shrinkBuf();
@@ -693,6 +698,65 @@ const BidiTrieContainer = class {
693698
return -1;
694699
}
695700

701+
dumpTrie(iroot) {
702+
for ( const s of this.trieIterator(iroot) ) {
703+
console.log(s);
704+
}
705+
}
706+
707+
trieIterator(iroot) {
708+
return {
709+
value: undefined,
710+
done: false,
711+
next() {
712+
if ( this.icell === 0 ) {
713+
if ( this.forks.length === 0 ) {
714+
this.value = undefined;
715+
this.done = true;
716+
return this;
717+
}
718+
this.charPtr = this.forks.pop();
719+
this.icell = this.forks.pop();
720+
}
721+
for (;;) {
722+
const idown = this.container.buf32[this.icell+CELL_OR];
723+
if ( idown !== 0 ) {
724+
this.forks.push(idown, this.charPtr);
725+
}
726+
const v = this.container.buf32[this.icell+SEGMENT_INFO];
727+
let i0 = this.container.buf32[CHAR0_SLOT] + (v & 0x00FFFFFF);
728+
const i1 = i0 + (v >>> 24);
729+
while ( i0 < i1 ) {
730+
this.charBuf[this.charPtr] = this.container.buf8[i0];
731+
this.charPtr += 1;
732+
i0 += 1;
733+
}
734+
this.icell = this.container.buf32[this.icell+CELL_AND];
735+
if ( this.icell === 0 ) {
736+
return this.toPattern();
737+
}
738+
if ( this.container.buf32[this.icell+SEGMENT_INFO] === 0 ) {
739+
this.icell = this.container.buf32[this.icell+CELL_AND];
740+
return this.toPattern();
741+
}
742+
}
743+
},
744+
toPattern() {
745+
this.value = this.textDecoder.decode(
746+
new Uint8Array(this.charBuf.buffer, 0, this.charPtr)
747+
);
748+
return this;
749+
},
750+
container: this,
751+
icell: iroot,
752+
charBuf: new Uint8Array(256),
753+
charPtr: 0,
754+
forks: [],
755+
textDecoder: new TextDecoder(),
756+
[Symbol.iterator]() { return this; },
757+
};
758+
}
759+
696760
async enableWASM(wasmModuleFetcher, path) {
697761
if ( typeof WebAssembly !== 'object' ) { return false; }
698762
if ( this.wasmMemory instanceof WebAssembly.Memory ) { return true; }
@@ -816,103 +880,7 @@ const BidiTrieContainer = class {
816880
HAYSTACK_START + HAYSTACK_SIZE
817881
);
818882
}
819-
};
820-
821-
/*******************************************************************************
822-
823-
Class to hold reference to a specific trie
824-
825-
*/
826-
827-
BidiTrieContainer.prototype.STrieRef = class {
828-
constructor(container, iroot, size) {
829-
this.container = container;
830-
this.iroot = iroot;
831-
this.size = size;
832-
}
833-
834-
add(i, n, pivot = 0) {
835-
const iboundary = this.container.add(this.iroot, i, n, pivot);
836-
if ( iboundary !== 0 ) {
837-
this.size += 1;
838-
}
839-
return iboundary;
840-
}
841-
842-
getExtra(iboundary) {
843-
return this.container.buf32[iboundary+BCELL_EXTRA];
844-
}
845-
846-
setExtra(iboundary, v) {
847-
this.container.buf32[iboundary+BCELL_EXTRA] = v;
848-
}
849-
850-
matches(i) {
851-
return this.container.matches(this.iroot, i);
852-
}
853-
854-
dump() {
855-
for ( const s of this ) {
856-
console.log(s);
857-
}
858-
}
859-
860-
get $l() { return this.container.buf32[RESULT_L_SLOT] | 0; }
861-
get $r() { return this.container.buf32[RESULT_R_SLOT] | 0; }
862-
get $iu() { return this.container.buf32[RESULT_IU_SLOT] | 0; }
863-
864-
[Symbol.iterator]() {
865-
return {
866-
value: undefined,
867-
done: false,
868-
next: function() {
869-
if ( this.icell === 0 ) {
870-
if ( this.forks.length === 0 ) {
871-
this.value = undefined;
872-
this.done = true;
873-
return this;
874-
}
875-
this.charPtr = this.forks.pop();
876-
this.icell = this.forks.pop();
877-
}
878-
for (;;) {
879-
const idown = this.container.buf32[this.icell+CELL_OR];
880-
if ( idown !== 0 ) {
881-
this.forks.push(idown, this.charPtr);
882-
}
883-
const v = this.container.buf32[this.icell+SEGMENT_INFO];
884-
let i0 = this.container.buf32[CHAR0_SLOT] + (v & 0x00FFFFFF);
885-
const i1 = i0 + (v >>> 24);
886-
while ( i0 < i1 ) {
887-
this.charBuf[this.charPtr] = this.container.buf8[i0];
888-
this.charPtr += 1;
889-
i0 += 1;
890-
}
891-
this.icell = this.container.buf32[this.icell+CELL_AND];
892-
if ( this.icell === 0 ) {
893-
return this.toPattern();
894-
}
895-
if ( this.container.buf32[this.icell+SEGMENT_INFO] === 0 ) {
896-
this.icell = this.container.buf32[this.icell+CELL_AND];
897-
return this.toPattern();
898-
}
899-
}
900-
},
901-
toPattern: function() {
902-
this.value = this.textDecoder.decode(
903-
new Uint8Array(this.charBuf.buffer, 0, this.charPtr)
904-
);
905-
return this;
906-
},
907-
container: this.container,
908-
icell: this.iroot,
909-
charBuf: new Uint8Array(256),
910-
charPtr: 0,
911-
forks: [],
912-
textDecoder: new TextDecoder()
913-
};
914-
}
915-
};
883+
}
916884

917885
/******************************************************************************/
918886

@@ -954,4 +922,4 @@ const getWasmModule = (( ) => {
954922

955923
/******************************************************************************/
956924

957-
export { BidiTrieContainer };
925+
export default BidiTrieContainer;

0 commit comments

Comments
 (0)