Reuse duplicate strings stored in tries

This is particularly helpful for static network filters
used with filter options causing the same pattern to be
reused across multiple filter instances, i.e. `all` or
`~css`, etc.
This commit is contained in:
Raymond Hill 2020-12-04 07:53:01 -05:00
parent e8e4a1ac74
commit c77f697b4b
No known key found for this signature in database
GPG Key ID: 25E1490B761470C2
2 changed files with 24 additions and 2 deletions

View File

@ -141,6 +141,9 @@ const HNTrieContainer = class {
this.buf32[CHAR0_SLOT] = len >>> 1;
this.buf32[CHAR1_SLOT] = this.buf32[CHAR0_SLOT];
this.wasmMemory = null;
this.lastStored = '';
this.lastStoredLen = this.lastStoredIndex = 0;
}
//--------------------------------------------------------------------------
@ -160,6 +163,9 @@ const HNTrieContainer = class {
}
this.buf32[TRIE1_SLOT] = this.buf32[TRIE0_SLOT];
this.buf32[CHAR1_SLOT] = this.buf32[CHAR0_SLOT];
this.lastStored = '';
this.lastStoredLen = this.lastStoredIndex = 0;
}
setNeedle(needle) {
@ -419,6 +425,11 @@ const HNTrieContainer = class {
hn = hn.slice(-255);
n = 255;
}
if ( n === this.lastStoredLen && hn === this.lastStored ) {
return this.lastStoredIndex;
}
this.lastStored = hn;
this.lastStoredLen = n;
if ( (this.buf.length - this.buf32[CHAR1_SLOT]) < n ) {
this.growBuf(0, n);
}
@ -428,7 +439,7 @@ const HNTrieContainer = class {
for ( let i = 0; i < n; i++ ) {
buf8[offset+i] = hn.charCodeAt(i);
}
return offset - this.buf32[CHAR0_SLOT];
return (this.lastStoredIndex = offset - this.buf32[CHAR0_SLOT]);
}
extractHostname(i, n) {

View File

@ -147,6 +147,9 @@ const roundToPageSize = v => (v + PAGE_SIZE-1) & ~(PAGE_SIZE-1);
this.extraHandler = extraHandler;
this.textDecoder = null;
this.wasmMemory = null;
this.lastStored = '';
this.lastStoredLen = this.lastStoredIndex = 0;
}
//--------------------------------------------------------------------------
@ -174,6 +177,9 @@ const roundToPageSize = v => (v + PAGE_SIZE-1) & ~(PAGE_SIZE-1);
}
this.buf32[TRIE1_SLOT] = this.buf32[TRIE0_SLOT];
this.buf32[CHAR1_SLOT] = this.buf32[CHAR0_SLOT];
this.lastStored = '';
this.lastStoredLen = this.lastStoredIndex = 0;
}
matches(icell, ai) {
@ -602,6 +608,11 @@ const roundToPageSize = v => (v + PAGE_SIZE-1) & ~(PAGE_SIZE-1);
storeString(s) {
const n = s.length;
if ( n === this.lastStoredLen && s === this.lastStored ) {
return this.lastStoredIndex;
}
this.lastStored = s;
this.lastStoredLen = n;
if ( (this.buf8.length - this.buf32[CHAR1_SLOT]) < n ) {
this.growBuf(0, n);
}
@ -611,7 +622,7 @@ const roundToPageSize = v => (v + PAGE_SIZE-1) & ~(PAGE_SIZE-1);
for ( let i = 0; i < n; i++ ) {
buf8[offset+i] = s.charCodeAt(i);
}
return offset - this.buf32[CHAR0_SLOT];
return (this.lastStoredIndex = offset - this.buf32[CHAR0_SLOT]);
}
extractString(i, n) {