Skip to content

Commit 3640454

Browse files
committed
Increase URL buffer size to 8192 (from 2048)
Related: easylist/easylist@777d7ba9
1 parent cff88d5 commit 3640454

File tree

9 files changed

+74
-86
lines changed

9 files changed

+74
-86
lines changed

src/devtools.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
<button id="sfe-benchmark" type="button" disabled>SFE: Benchmark<span class="hover"></span></button>
3434
<button id="purge-all-caches" type="button" data-i18n-title="3pPurgeAll"><span data-i18n="3pPurgeAll">_</span><span class="hover"></span></button>
3535
</div>
36-
<div id="console" class="codeMirrorContainer"></div>
36+
<div id="console" class="codeMirrorContainer codeMirrorBreakAll"></div>
3737

3838
<script src="lib/codemirror/lib/codemirror.js"></script>
3939
<script src="lib/codemirror/addon/display/panel.js"></script>

src/js/background.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,7 @@ const µBlock = { // jshint ignore:line
181181
// Read-only
182182
systemSettings: {
183183
compiledMagic: 57, // Increase when compiled format changes
184-
selfieMagic: 58, // Increase when selfie format changes
184+
selfieMagic: 59, // Increase when selfie format changes
185185
},
186186

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

src/js/biditrie.js

Lines changed: 36 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,16 @@
2424
A BidiTrieContainer is mostly a large buffer in which distinct but related
2525
tries are stored. The memory layout of the buffer is as follow:
2626
27-
0-2047: haystack section
28-
2048-2051: number of significant characters in the haystack
29-
2052-2055: offset to start of trie data section (=> trie0)
30-
2056-2059: offset to end of trie data section (=> trie1)
31-
2060-2063: offset to start of character data section (=> char0)
32-
2064-2067: offset to end of character data section (=> char1)
33-
2068: start of trie data section
27+
0-8192: haystack section
28+
8192-8195: number of significant characters in the haystack
29+
8196-8199: offset to start of trie data section (=> trie0)
30+
8200-8203: offset to end of trie data section (=> trie1)
31+
8204-8207: offset to start of character data section (=> char0)
32+
8208-8211: offset to end of character data section (=> char1)
33+
8212-8215: offset to left index result (=> result_l)
34+
8216-8219: offset to right index result (=> result_r)
35+
8220-8223: offset to extra unit result (=> result_iu)
36+
8224: start of trie data section
3437
3538
+--------------+
3639
Normal cell: | And | If "Segment info" matches:
@@ -93,18 +96,19 @@
9396
9497
*/
9598

99+
const VERSION = 2;
96100
const PAGE_SIZE = 65536*2;
97101
const HAYSTACK_START = 0;
98-
const HAYSTACK_SIZE = 2048; // i32 / i8
99-
const HAYSTACK_SIZE_SLOT = HAYSTACK_SIZE >>> 2; // 512 / 2048
100-
const TRIE0_SLOT = HAYSTACK_SIZE_SLOT + 1; // 513 / 2052
101-
const TRIE1_SLOT = HAYSTACK_SIZE_SLOT + 2; // 514 / 2056
102-
const CHAR0_SLOT = HAYSTACK_SIZE_SLOT + 3; // 515 / 2060
103-
const CHAR1_SLOT = HAYSTACK_SIZE_SLOT + 4; // 516 / 2064
104-
const RESULT_L_SLOT = HAYSTACK_SIZE_SLOT + 5; // 517 / 2068
105-
const RESULT_R_SLOT = HAYSTACK_SIZE_SLOT + 6; // 518 / 2072
106-
const RESULT_IU_SLOT = HAYSTACK_SIZE_SLOT + 7; // 519 / 2076
107-
const TRIE0_START = HAYSTACK_SIZE_SLOT + 8 << 2; // 2080
102+
const HAYSTACK_SIZE = 8192; // i32 / i8
103+
const HAYSTACK_SIZE_SLOT = HAYSTACK_SIZE >>> 2; // 2048 / 8192
104+
const TRIE0_SLOT = HAYSTACK_SIZE_SLOT + 1; // 2049 / 8196
105+
const TRIE1_SLOT = HAYSTACK_SIZE_SLOT + 2; // 2050 / 8200
106+
const CHAR0_SLOT = HAYSTACK_SIZE_SLOT + 3; // 2051 / 8204
107+
const CHAR1_SLOT = HAYSTACK_SIZE_SLOT + 4; // 2052 / 8208
108+
const RESULT_L_SLOT = HAYSTACK_SIZE_SLOT + 5; // 2053 / 8212
109+
const RESULT_R_SLOT = HAYSTACK_SIZE_SLOT + 6; // 2054 / 8216
110+
const RESULT_IU_SLOT = HAYSTACK_SIZE_SLOT + 7; // 2055 / 8220
111+
const TRIE0_START = HAYSTACK_SIZE_SLOT + 8 << 2; // 8224
108112

109113
const CELL_BYTE_LENGTH = 12;
110114
const MIN_FREE_CELL_BYTE_LENGTH = CELL_BYTE_LENGTH * 8;
@@ -156,28 +160,21 @@ class BidiTrieContainer {
156160
// Public methods
157161
//--------------------------------------------------------------------------
158162

159-
get haystackLen() {
163+
getHaystackLen() {
160164
return this.buf32[HAYSTACK_SIZE_SLOT];
161165
}
162166

163-
set haystackLen(v) {
167+
setHaystackLen(v) {
168+
if ( v > HAYSTACK_SIZE ) {
169+
v = HAYSTACK_SIZE;
170+
}
164171
this.buf32[HAYSTACK_SIZE_SLOT] = v;
172+
return v;
165173
}
166174

167-
reset(details) {
168-
if (
169-
details instanceof Object &&
170-
typeof details.byteLength === 'number' &&
171-
typeof details.char0 === 'number'
172-
) {
173-
if ( details.byteLength > this.buf8.byteLength ) {
174-
this.reallocateBuf(details.byteLength);
175-
}
176-
this.buf32[CHAR0_SLOT] = details.char0;
177-
}
175+
reset() {
178176
this.buf32[TRIE1_SLOT] = this.buf32[TRIE0_SLOT];
179177
this.buf32[CHAR1_SLOT] = this.buf32[CHAR0_SLOT];
180-
181178
this.lastStored = '';
182179
this.lastStoredLen = this.lastStoredIndex = 0;
183180
}
@@ -571,23 +568,22 @@ class BidiTrieContainer {
571568
this.buf32[iboundary+BCELL_EXTRA] = v;
572569
}
573570

574-
optimize(shrink = false) {
575-
if ( shrink ) {
576-
this.shrinkBuf();
577-
}
578-
return {
579-
byteLength: this.buf8.byteLength,
580-
char0: this.buf32[CHAR0_SLOT],
581-
};
571+
optimize() {
572+
this.shrinkBuf();
582573
}
583574

584575
toSelfie() {
585576
const buf32 = this.buf32.subarray(0, this.buf32[CHAR1_SLOT] + 3 >>> 2);
586-
return { buf32, checksum: i32Checksum(buf32) };
577+
return {
578+
version: VERSION,
579+
buf32,
580+
checksum: i32Checksum(buf32),
581+
};
587582
}
588583

589584
fromSelfie(selfie) {
590585
if ( typeof selfie !== 'object' || selfie === null ) { return false; }
586+
if ( selfie.version !== VERSION ) { return false; }
591587
if ( selfie.buf32 instanceof Uint32Array === false ) { return false; }
592588
if ( selfie.checksum !== i32Checksum(selfie.buf32) ) { return false; }
593589
const byteLength = selfie.buf32.length << 2;

src/js/devtools.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -331,6 +331,7 @@ cmEditor.on('beforeChange', (cm, details) => {
331331
const fields = line.slice(5).split(/\s+/);
332332
const query = {};
333333
for ( const field of fields ) {
334+
if ( field === '' ) { continue; }
334335
if ( /[/.]/.test(field) ) {
335336
if ( query.url === undefined ) {
336337
query.url = field;

src/js/s14e-serializer.js

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1422,8 +1422,14 @@ if ( isInstanceOf(globalThis, 'DedicatedWorkerGlobalScope') ) {
14221422
break;
14231423
}
14241424
case THREAD_DESERIALIZE: {
1425-
const result = deserialize(msg.data);
1426-
globalThis.postMessage({ id: msg.id, size: msg.size, result });
1425+
let result;
1426+
try {
1427+
result = deserialize(msg.data);
1428+
} catch(ex) {
1429+
console.error(ex);
1430+
} finally {
1431+
globalThis.postMessage({ id: msg.id, size: msg.size, result });
1432+
}
14271433
break;
14281434
}
14291435
default:

src/js/static-net-filtering.js

Lines changed: 7 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -550,14 +550,6 @@ const bidiTrieMatchExtra = (l, r, ix) => {
550550

551551
const bidiTrie = new BidiTrieContainer(bidiTrieMatchExtra);
552552

553-
const bidiTriePrime = ( ) => {
554-
bidiTrie.reset(keyvalStore.getItem('SNFE.bidiTrie'));
555-
};
556-
557-
const bidiTrieOptimize = (shrink = false) => {
558-
keyvalStore.setItem('SNFE.bidiTrie', bidiTrie.optimize(shrink));
559-
};
560-
561553
/*******************************************************************************
562554
563555
Each filter class will register itself in the map.
@@ -800,7 +792,7 @@ class FilterPatternPlain {
800792
if (
801793
bidiTrie.startsWith(
802794
left,
803-
bidiTrie.haystackLen,
795+
bidiTrie.getHaystackLen(),
804796
filterData[idata+1],
805797
n
806798
) === 0
@@ -877,7 +869,7 @@ class FilterPatternPlain1 extends FilterPatternPlain {
877869
if (
878870
bidiTrie.startsWith(
879871
left,
880-
bidiTrie.haystackLen,
872+
bidiTrie.getHaystackLen(),
881873
filterData[idata+1],
882874
n
883875
) === 0
@@ -900,7 +892,7 @@ class FilterPatternPlainX extends FilterPatternPlain {
900892
if (
901893
bidiTrie.startsWith(
902894
left,
903-
bidiTrie.haystackLen,
895+
bidiTrie.getHaystackLen(),
904896
filterData[idata+1],
905897
n
906898
) === 0
@@ -1035,7 +1027,7 @@ class FilterAnchorHnLeft {
10351027
lastBeg = len !== 0 ? haystackCodes.indexOf(0x3A) : -1;
10361028
if ( lastBeg !== -1 ) {
10371029
if (
1038-
lastBeg >= bidiTrie.haystackLen ||
1030+
lastBeg >= bidiTrie.getHaystackLen() ||
10391031
haystackCodes[lastBeg+1] !== 0x2F ||
10401032
haystackCodes[lastBeg+2] !== 0x2F
10411033
) {
@@ -3199,14 +3191,9 @@ const urlTokenizer = new (class {
31993191

32003192
_tokenize(encodeInto) {
32013193
const tokens = this._tokens;
3202-
let url = this._urlOut;
3203-
let l = url.length;
3194+
const url = this._urlOut;
3195+
const l = encodeInto.setHaystackLen(url.length);
32043196
if ( l === 0 ) { return 0; }
3205-
if ( l > 2048 ) {
3206-
url = url.slice(0, 2048);
3207-
l = 2048;
3208-
}
3209-
encodeInto.haystackLen = l;
32103197
let j = 0;
32113198
let hasq = -1;
32123199
mainLoop: {
@@ -4197,7 +4184,6 @@ StaticNetFilteringEngine.prototype.prime = function() {
41974184
destHNTrieContainer.reset(
41984185
keyvalStore.getItem('SNFE.destHNTrieContainer.trieDetails')
41994186
);
4200-
bidiTriePrime();
42014187
};
42024188

42034189
/******************************************************************************/
@@ -4793,15 +4779,14 @@ StaticNetFilteringEngine.prototype.optimize = function(throttle = 0) {
47934779
'SNFE.destHNTrieContainer.trieDetails',
47944780
destHNTrieContainer.optimize()
47954781
);
4796-
bidiTrieOptimize();
47974782
filterDataShrink();
47984783
};
47994784

48004785
/******************************************************************************/
48014786

48024787
StaticNetFilteringEngine.prototype.toSelfie = function() {
48034788
this.optimize(0);
4804-
bidiTrieOptimize(true);
4789+
bidiTrie.optimize();
48054790
keyvalStore.setItem('SNFE.origHNTrieContainer.trieDetails',
48064791
origHNTrieContainer.optimize()
48074792
);

src/js/wasm/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ Assuming:
1313
### `wat2wasm` tool
1414

1515
The `wat2wasm` tool can be downloaded from an official WebAssembly project:
16-
<https://github.com/WebAssembly/wabt/releases>.
16+
<https://github.com/WebAssembly/wabt/releases/tag/1.0.29>
1717

1818
### `wat2wasm` tool online
1919

src/js/wasm/biditrie.wasm

9 Bytes
Binary file not shown.

src/js/wasm/biditrie.wat

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -32,16 +32,16 @@
3232
;;
3333
;; Memory layout, byte offset:
3434
;; const HAYSTACK_START = 0;
35-
;; const HAYSTACK_SIZE = 2048; // i32 / i8
36-
;; const HAYSTACK_SIZE_SLOT = HAYSTACK_SIZE >>> 2; // 512 / 2048
37-
;; const TRIE0_SLOT = HAYSTACK_SIZE_SLOT + 1; // 513 / 2052
38-
;; const TRIE1_SLOT = HAYSTACK_SIZE_SLOT + 2; // 514 / 2056
39-
;; const CHAR0_SLOT = HAYSTACK_SIZE_SLOT + 3; // 515 / 2060
40-
;; const CHAR1_SLOT = HAYSTACK_SIZE_SLOT + 4; // 516 / 2064
41-
;; const RESULT_L_SLOT = HAYSTACK_SIZE_SLOT + 5; // 517 / 2068
42-
;; const RESULT_R_SLOT = HAYSTACK_SIZE_SLOT + 6; // 518 / 2072
43-
;; const RESULT_IU_SLOT = HAYSTACK_SIZE_SLOT + 7; // 519 / 2076
44-
;; const TRIE0_START = HAYSTACK_SIZE_SLOT + 8 << 2; // 2080
35+
;; const HAYSTACK_SIZE = 8192; // i32 / i8
36+
;; const HAYSTACK_SIZE_SLOT = HAYSTACK_SIZE >>> 2; // 2048 / 8192
37+
;; const TRIE0_SLOT = HAYSTACK_SIZE_SLOT + 1; // 2049 / 8196
38+
;; const TRIE1_SLOT = HAYSTACK_SIZE_SLOT + 2; // 2050 / 8200
39+
;; const CHAR0_SLOT = HAYSTACK_SIZE_SLOT + 3; // 2051 / 8204
40+
;; const CHAR1_SLOT = HAYSTACK_SIZE_SLOT + 4; // 2052 / 8208
41+
;; const RESULT_L_SLOT = HAYSTACK_SIZE_SLOT + 5; // 2053 / 8212
42+
;; const RESULT_R_SLOT = HAYSTACK_SIZE_SLOT + 6; // 2054 / 8216
43+
;; const RESULT_IU_SLOT = HAYSTACK_SIZE_SLOT + 7; // 2055 / 8220
44+
;; const TRIE0_START = HAYSTACK_SIZE_SLOT + 8 << 2; // 8224
4545
;;
4646

4747
;;
@@ -71,11 +71,11 @@
7171
;; const buf32 = this.buf32;
7272
;; const buf8 = this.buf8;
7373
;; const char0 = buf32[CHAR0_SLOT];
74-
i32.const 2060
74+
i32.const 8204
7575
i32.load align=4
7676
local.set $char0
7777
;; const aR = buf32[HAYSTACK_SIZE_SLOT];
78-
i32.const 2048
78+
i32.const 8192
7979
i32.load align=4
8080
local.set $aR
8181
;; let al = ai;
@@ -253,7 +253,7 @@
253253
;; const buf32 = this.buf32;
254254
;; const buf8 = this.buf8;
255255
;; const char0 = buf32[CHAR0_SLOT];
256-
i32.const 2060
256+
i32.const 8204
257257
i32.load align=4
258258
local.set $char0
259259
block $matchFound
@@ -433,15 +433,15 @@
433433
;; }
434434
end
435435
;; this.buf32[RESULT_IU_SLOT] = iu;
436-
i32.const 2076
436+
i32.const 8220
437437
local.get $iu
438438
i32.store align=4
439439
;; this.buf32[RESULT_L_SLOT] = l;
440-
i32.const 2068
440+
i32.const 8212
441441
local.get $l
442442
i32.store align=4
443443
;; this.buf32[RESULT_R_SLOT] = r;
444-
i32.const 2072
444+
i32.const 8216
445445
local.get $r
446446
i32.store align=4
447447
end ;; $succeed
@@ -483,7 +483,7 @@
483483
;; const charCodes = this.buf8;
484484
;; needleLeft += this.buf32[CHAR0_SLOT];
485485
local.get $needleLeft
486-
i32.const 2060 ;; CHAR0_SLOT memory address
486+
i32.const 8204 ;; CHAR0_SLOT memory address
487487
i32.load align=4 ;; CHAR0 memory address
488488
i32.add ;; needle memory address
489489
local.tee $needleLeft
@@ -559,7 +559,7 @@
559559
br_if $fail
560560
;; needleLeft += this.buf32[CHAR0_SLOT];
561561
local.get $needleLeft
562-
i32.const 2060 ;; CHAR0_SLOT memory address
562+
i32.const 8204 ;; CHAR0_SLOT memory address
563563
i32.load align=4 ;; CHAR0 memory address
564564
i32.add ;; needle memory address
565565
local.tee $needleLeft
@@ -659,7 +659,7 @@
659659
br_if $fail
660660
;; needleLeft += this.buf32[CHAR0_SLOT];
661661
local.get $needleLeft
662-
i32.const 2060 ;; CHAR0_SLOT memory address
662+
i32.const 8204 ;; CHAR0_SLOT memory address
663663
i32.load align=4 ;; CHAR0 memory address
664664
i32.add ;; needle memory address
665665
local.tee $needleLeft

0 commit comments

Comments
 (0)