From 36404543e4071b0c84c0a0e8a38ee155fb325797 Mon Sep 17 00:00:00 2001 From: Raymond Hill Date: Sat, 1 Mar 2025 14:48:51 -0500 Subject: [PATCH] Increase URL buffer size to 8192 (from 2048) Related: https://github.com/easylist/easylist/commit/777d7ba9 --- src/devtools.html | 2 +- src/js/background.js | 2 +- src/js/biditrie.js | 76 ++++++++++++++++----------------- src/js/devtools.js | 1 + src/js/s14e-serializer.js | 10 ++++- src/js/static-net-filtering.js | 29 +++---------- src/js/wasm/README.md | 2 +- src/js/wasm/biditrie.wasm | Bin 990 -> 999 bytes src/js/wasm/biditrie.wat | 38 ++++++++--------- 9 files changed, 74 insertions(+), 86 deletions(-) diff --git a/src/devtools.html b/src/devtools.html index 8f66827bf..561037661 100644 --- a/src/devtools.html +++ b/src/devtools.html @@ -33,7 +33,7 @@ -
+
diff --git a/src/js/background.js b/src/js/background.js index 01fd3b5d6..b648ee9c0 100644 --- a/src/js/background.js +++ b/src/js/background.js @@ -181,7 +181,7 @@ const µBlock = { // jshint ignore:line // Read-only systemSettings: { compiledMagic: 57, // Increase when compiled format changes - selfieMagic: 58, // Increase when selfie format changes + selfieMagic: 59, // Increase when selfie format changes }, // https://github.com/uBlockOrigin/uBlock-issues/issues/759#issuecomment-546654501 diff --git a/src/js/biditrie.js b/src/js/biditrie.js index 560d5918a..33e6a1f75 100644 --- a/src/js/biditrie.js +++ b/src/js/biditrie.js @@ -24,13 +24,16 @@ A BidiTrieContainer is mostly a large buffer in which distinct but related tries are stored. The memory layout of the buffer is as follow: - 0-2047: haystack section - 2048-2051: number of significant characters in the haystack - 2052-2055: offset to start of trie data section (=> trie0) - 2056-2059: offset to end of trie data section (=> trie1) - 2060-2063: offset to start of character data section (=> char0) - 2064-2067: offset to end of character data section (=> char1) - 2068: start of trie data section + 0-8192: haystack section + 8192-8195: number of significant characters in the haystack + 8196-8199: offset to start of trie data section (=> trie0) + 8200-8203: offset to end of trie data section (=> trie1) + 8204-8207: offset to start of character data section (=> char0) + 8208-8211: offset to end of character data section (=> char1) + 8212-8215: offset to left index result (=> result_l) + 8216-8219: offset to right index result (=> result_r) + 8220-8223: offset to extra unit result (=> result_iu) + 8224: start of trie data section +--------------+ Normal cell: | And | If "Segment info" matches: @@ -93,18 +96,19 @@ */ +const VERSION = 2; const PAGE_SIZE = 65536*2; const HAYSTACK_START = 0; -const HAYSTACK_SIZE = 2048; // i32 / i8 -const HAYSTACK_SIZE_SLOT = HAYSTACK_SIZE >>> 2; // 512 / 2048 -const TRIE0_SLOT = HAYSTACK_SIZE_SLOT + 1; // 513 / 2052 -const TRIE1_SLOT = HAYSTACK_SIZE_SLOT + 2; // 514 / 2056 -const CHAR0_SLOT = HAYSTACK_SIZE_SLOT + 3; // 515 / 2060 -const CHAR1_SLOT = HAYSTACK_SIZE_SLOT + 4; // 516 / 2064 -const RESULT_L_SLOT = HAYSTACK_SIZE_SLOT + 5; // 517 / 2068 -const RESULT_R_SLOT = HAYSTACK_SIZE_SLOT + 6; // 518 / 2072 -const RESULT_IU_SLOT = HAYSTACK_SIZE_SLOT + 7; // 519 / 2076 -const TRIE0_START = HAYSTACK_SIZE_SLOT + 8 << 2; // 2080 +const HAYSTACK_SIZE = 8192; // i32 / i8 +const HAYSTACK_SIZE_SLOT = HAYSTACK_SIZE >>> 2; // 2048 / 8192 +const TRIE0_SLOT = HAYSTACK_SIZE_SLOT + 1; // 2049 / 8196 +const TRIE1_SLOT = HAYSTACK_SIZE_SLOT + 2; // 2050 / 8200 +const CHAR0_SLOT = HAYSTACK_SIZE_SLOT + 3; // 2051 / 8204 +const CHAR1_SLOT = HAYSTACK_SIZE_SLOT + 4; // 2052 / 8208 +const RESULT_L_SLOT = HAYSTACK_SIZE_SLOT + 5; // 2053 / 8212 +const RESULT_R_SLOT = HAYSTACK_SIZE_SLOT + 6; // 2054 / 8216 +const RESULT_IU_SLOT = HAYSTACK_SIZE_SLOT + 7; // 2055 / 8220 +const TRIE0_START = HAYSTACK_SIZE_SLOT + 8 << 2; // 8224 const CELL_BYTE_LENGTH = 12; const MIN_FREE_CELL_BYTE_LENGTH = CELL_BYTE_LENGTH * 8; @@ -156,28 +160,21 @@ class BidiTrieContainer { // Public methods //-------------------------------------------------------------------------- - get haystackLen() { + getHaystackLen() { return this.buf32[HAYSTACK_SIZE_SLOT]; } - set haystackLen(v) { + setHaystackLen(v) { + if ( v > HAYSTACK_SIZE ) { + v = HAYSTACK_SIZE; + } this.buf32[HAYSTACK_SIZE_SLOT] = v; + return v; } - reset(details) { - if ( - details instanceof Object && - typeof details.byteLength === 'number' && - typeof details.char0 === 'number' - ) { - if ( details.byteLength > this.buf8.byteLength ) { - this.reallocateBuf(details.byteLength); - } - this.buf32[CHAR0_SLOT] = details.char0; - } + reset() { this.buf32[TRIE1_SLOT] = this.buf32[TRIE0_SLOT]; this.buf32[CHAR1_SLOT] = this.buf32[CHAR0_SLOT]; - this.lastStored = ''; this.lastStoredLen = this.lastStoredIndex = 0; } @@ -571,23 +568,22 @@ class BidiTrieContainer { this.buf32[iboundary+BCELL_EXTRA] = v; } - optimize(shrink = false) { - if ( shrink ) { - this.shrinkBuf(); - } - return { - byteLength: this.buf8.byteLength, - char0: this.buf32[CHAR0_SLOT], - }; + optimize() { + this.shrinkBuf(); } toSelfie() { const buf32 = this.buf32.subarray(0, this.buf32[CHAR1_SLOT] + 3 >>> 2); - return { buf32, checksum: i32Checksum(buf32) }; + return { + version: VERSION, + buf32, + checksum: i32Checksum(buf32), + }; } fromSelfie(selfie) { if ( typeof selfie !== 'object' || selfie === null ) { return false; } + if ( selfie.version !== VERSION ) { return false; } if ( selfie.buf32 instanceof Uint32Array === false ) { return false; } if ( selfie.checksum !== i32Checksum(selfie.buf32) ) { return false; } const byteLength = selfie.buf32.length << 2; diff --git a/src/js/devtools.js b/src/js/devtools.js index 92cd07894..5183d2192 100644 --- a/src/js/devtools.js +++ b/src/js/devtools.js @@ -331,6 +331,7 @@ cmEditor.on('beforeChange', (cm, details) => { const fields = line.slice(5).split(/\s+/); const query = {}; for ( const field of fields ) { + if ( field === '' ) { continue; } if ( /[/.]/.test(field) ) { if ( query.url === undefined ) { query.url = field; diff --git a/src/js/s14e-serializer.js b/src/js/s14e-serializer.js index f88e5b5e2..db40388e9 100644 --- a/src/js/s14e-serializer.js +++ b/src/js/s14e-serializer.js @@ -1422,8 +1422,14 @@ if ( isInstanceOf(globalThis, 'DedicatedWorkerGlobalScope') ) { break; } case THREAD_DESERIALIZE: { - const result = deserialize(msg.data); - globalThis.postMessage({ id: msg.id, size: msg.size, result }); + let result; + try { + result = deserialize(msg.data); + } catch(ex) { + console.error(ex); + } finally { + globalThis.postMessage({ id: msg.id, size: msg.size, result }); + } break; } default: diff --git a/src/js/static-net-filtering.js b/src/js/static-net-filtering.js index af5f5691c..b1a56d232 100644 --- a/src/js/static-net-filtering.js +++ b/src/js/static-net-filtering.js @@ -550,14 +550,6 @@ const bidiTrieMatchExtra = (l, r, ix) => { const bidiTrie = new BidiTrieContainer(bidiTrieMatchExtra); -const bidiTriePrime = ( ) => { - bidiTrie.reset(keyvalStore.getItem('SNFE.bidiTrie')); -}; - -const bidiTrieOptimize = (shrink = false) => { - keyvalStore.setItem('SNFE.bidiTrie', bidiTrie.optimize(shrink)); -}; - /******************************************************************************* Each filter class will register itself in the map. @@ -800,7 +792,7 @@ class FilterPatternPlain { if ( bidiTrie.startsWith( left, - bidiTrie.haystackLen, + bidiTrie.getHaystackLen(), filterData[idata+1], n ) === 0 @@ -877,7 +869,7 @@ class FilterPatternPlain1 extends FilterPatternPlain { if ( bidiTrie.startsWith( left, - bidiTrie.haystackLen, + bidiTrie.getHaystackLen(), filterData[idata+1], n ) === 0 @@ -900,7 +892,7 @@ class FilterPatternPlainX extends FilterPatternPlain { if ( bidiTrie.startsWith( left, - bidiTrie.haystackLen, + bidiTrie.getHaystackLen(), filterData[idata+1], n ) === 0 @@ -1035,7 +1027,7 @@ class FilterAnchorHnLeft { lastBeg = len !== 0 ? haystackCodes.indexOf(0x3A) : -1; if ( lastBeg !== -1 ) { if ( - lastBeg >= bidiTrie.haystackLen || + lastBeg >= bidiTrie.getHaystackLen() || haystackCodes[lastBeg+1] !== 0x2F || haystackCodes[lastBeg+2] !== 0x2F ) { @@ -3199,14 +3191,9 @@ const urlTokenizer = new (class { _tokenize(encodeInto) { const tokens = this._tokens; - let url = this._urlOut; - let l = url.length; + const url = this._urlOut; + const l = encodeInto.setHaystackLen(url.length); if ( l === 0 ) { return 0; } - if ( l > 2048 ) { - url = url.slice(0, 2048); - l = 2048; - } - encodeInto.haystackLen = l; let j = 0; let hasq = -1; mainLoop: { @@ -4197,7 +4184,6 @@ StaticNetFilteringEngine.prototype.prime = function() { destHNTrieContainer.reset( keyvalStore.getItem('SNFE.destHNTrieContainer.trieDetails') ); - bidiTriePrime(); }; /******************************************************************************/ @@ -4793,7 +4779,6 @@ StaticNetFilteringEngine.prototype.optimize = function(throttle = 0) { 'SNFE.destHNTrieContainer.trieDetails', destHNTrieContainer.optimize() ); - bidiTrieOptimize(); filterDataShrink(); }; @@ -4801,7 +4786,7 @@ StaticNetFilteringEngine.prototype.optimize = function(throttle = 0) { StaticNetFilteringEngine.prototype.toSelfie = function() { this.optimize(0); - bidiTrieOptimize(true); + bidiTrie.optimize(); keyvalStore.setItem('SNFE.origHNTrieContainer.trieDetails', origHNTrieContainer.optimize() ); diff --git a/src/js/wasm/README.md b/src/js/wasm/README.md index 32aef076f..01041b421 100644 --- a/src/js/wasm/README.md +++ b/src/js/wasm/README.md @@ -13,7 +13,7 @@ Assuming: ### `wat2wasm` tool The `wat2wasm` tool can be downloaded from an official WebAssembly project: -. + ### `wat2wasm` tool online diff --git a/src/js/wasm/biditrie.wasm b/src/js/wasm/biditrie.wasm index 5bfc6b7d0480ef5fefe921bcb3f7b4afd571e778..1a133986798515f18e9d482c8dae44d6ae467cdb 100644 GIT binary patch delta 153 zcmcb|{+xY6hs<3zwm*!FZ1oBZj!Y$r430eq7&Mp|6qy_w4lqny^px`oBO_})NOW>P z<5@=XPY^4x2omNrnliXAhHwd=Vo{ rJ(B~Ig95WFFHo5RbG8zr0)q!HqXHAyTp{KV#)`@5%nBe49n4_>P-rEl delta 144 zcmaFPevf@Zhtw4|wqJ~lZ1oBZj!Y$r430el8cYm|OpXl#6PG;YxXj4NS`QSPJc;ov zqvPZRCS@A|1!glQ2FED^3Jf56hJXSikmhz|myasV2~;K9qNz@WgK nrNHP7WHEt^Im~3iSUC9)lMJK4WGUtl#`4Me%nCsEMCLF6uazJE diff --git a/src/js/wasm/biditrie.wat b/src/js/wasm/biditrie.wat index a6c80baf1..7a7750091 100644 --- a/src/js/wasm/biditrie.wat +++ b/src/js/wasm/biditrie.wat @@ -32,16 +32,16 @@ ;; ;; Memory layout, byte offset: ;; const HAYSTACK_START = 0; -;; const HAYSTACK_SIZE = 2048; // i32 / i8 -;; const HAYSTACK_SIZE_SLOT = HAYSTACK_SIZE >>> 2; // 512 / 2048 -;; const TRIE0_SLOT = HAYSTACK_SIZE_SLOT + 1; // 513 / 2052 -;; const TRIE1_SLOT = HAYSTACK_SIZE_SLOT + 2; // 514 / 2056 -;; const CHAR0_SLOT = HAYSTACK_SIZE_SLOT + 3; // 515 / 2060 -;; const CHAR1_SLOT = HAYSTACK_SIZE_SLOT + 4; // 516 / 2064 -;; const RESULT_L_SLOT = HAYSTACK_SIZE_SLOT + 5; // 517 / 2068 -;; const RESULT_R_SLOT = HAYSTACK_SIZE_SLOT + 6; // 518 / 2072 -;; const RESULT_IU_SLOT = HAYSTACK_SIZE_SLOT + 7; // 519 / 2076 -;; const TRIE0_START = HAYSTACK_SIZE_SLOT + 8 << 2; // 2080 +;; const HAYSTACK_SIZE = 8192; // i32 / i8 +;; const HAYSTACK_SIZE_SLOT = HAYSTACK_SIZE >>> 2; // 2048 / 8192 +;; const TRIE0_SLOT = HAYSTACK_SIZE_SLOT + 1; // 2049 / 8196 +;; const TRIE1_SLOT = HAYSTACK_SIZE_SLOT + 2; // 2050 / 8200 +;; const CHAR0_SLOT = HAYSTACK_SIZE_SLOT + 3; // 2051 / 8204 +;; const CHAR1_SLOT = HAYSTACK_SIZE_SLOT + 4; // 2052 / 8208 +;; const RESULT_L_SLOT = HAYSTACK_SIZE_SLOT + 5; // 2053 / 8212 +;; const RESULT_R_SLOT = HAYSTACK_SIZE_SLOT + 6; // 2054 / 8216 +;; const RESULT_IU_SLOT = HAYSTACK_SIZE_SLOT + 7; // 2055 / 8220 +;; const TRIE0_START = HAYSTACK_SIZE_SLOT + 8 << 2; // 8224 ;; ;; @@ -71,11 +71,11 @@ ;; const buf32 = this.buf32; ;; const buf8 = this.buf8; ;; const char0 = buf32[CHAR0_SLOT]; - i32.const 2060 + i32.const 8204 i32.load align=4 local.set $char0 ;; const aR = buf32[HAYSTACK_SIZE_SLOT]; - i32.const 2048 + i32.const 8192 i32.load align=4 local.set $aR ;; let al = ai; @@ -253,7 +253,7 @@ ;; const buf32 = this.buf32; ;; const buf8 = this.buf8; ;; const char0 = buf32[CHAR0_SLOT]; - i32.const 2060 + i32.const 8204 i32.load align=4 local.set $char0 block $matchFound @@ -433,15 +433,15 @@ ;; } end ;; this.buf32[RESULT_IU_SLOT] = iu; - i32.const 2076 + i32.const 8220 local.get $iu i32.store align=4 ;; this.buf32[RESULT_L_SLOT] = l; - i32.const 2068 + i32.const 8212 local.get $l i32.store align=4 ;; this.buf32[RESULT_R_SLOT] = r; - i32.const 2072 + i32.const 8216 local.get $r i32.store align=4 end ;; $succeed @@ -483,7 +483,7 @@ ;; const charCodes = this.buf8; ;; needleLeft += this.buf32[CHAR0_SLOT]; local.get $needleLeft - i32.const 2060 ;; CHAR0_SLOT memory address + i32.const 8204 ;; CHAR0_SLOT memory address i32.load align=4 ;; CHAR0 memory address i32.add ;; needle memory address local.tee $needleLeft @@ -559,7 +559,7 @@ br_if $fail ;; needleLeft += this.buf32[CHAR0_SLOT]; local.get $needleLeft - i32.const 2060 ;; CHAR0_SLOT memory address + i32.const 8204 ;; CHAR0_SLOT memory address i32.load align=4 ;; CHAR0 memory address i32.add ;; needle memory address local.tee $needleLeft @@ -659,7 +659,7 @@ br_if $fail ;; needleLeft += this.buf32[CHAR0_SLOT]; local.get $needleLeft - i32.const 2060 ;; CHAR0_SLOT memory address + i32.const 8204 ;; CHAR0_SLOT memory address i32.load align=4 ;; CHAR0 memory address i32.add ;; needle memory address local.tee $needleLeft