Skip to content

Commit

Permalink
radix-trie-cache: faster find(q, cursor) instead of get(q)
Browse files Browse the repository at this point in the history
  • Loading branch information
ignoramous committed Mar 2, 2022
1 parent fd3d936 commit 09d79c3
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 13 deletions.
2 changes: 1 addition & 1 deletion import_map.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"imports": {
"@serverless-dns/dns-parser": "https://github.com/serverless-dns/dns-parser/raw/v2.1.1/index.js",
"@serverless-dns/lfu-cache": "https://github.com/serverless-dns/lfu-cache/raw/v3.3.3/lfu.js",
"@serverless-dns/lfu-cache": "https://github.com/serverless-dns/lfu-cache/raw/v3.4.1/lfu.js",
"buffer": "https://deno.land/std@0.110.0/node/buffer.ts",
"dotenv": "https://deno.land/x/dotenv@v3.0.0/mod.ts"
}
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
"homepage": "https://github.com/serverless-dns/serverless-dns#readme",
"dependencies": {
"@serverless-dns/dns-parser": "github:serverless-dns/dns-parser#v2.1.1",
"@serverless-dns/lfu-cache": "github:serverless-dns/lfu-cache#v3.3.3",
"@serverless-dns/lfu-cache": "github:serverless-dns/lfu-cache#v3.4.1",
"dotenv": "^10.0.0",
"proxy-protocol-js": "^4.0.5",
"undici": "^4.13.0"
Expand Down
35 changes: 26 additions & 9 deletions src/plugins/blocklist-wrapper/radixTrie.js
Original file line number Diff line number Diff line change
Expand Up @@ -481,6 +481,7 @@ function FrozenTrieNode(trie, index) {
let valCached;
let flagCached;
let wordCached;
let cursorCached;

this.trie = trie;
this.index = index;
Expand Down Expand Up @@ -540,8 +541,8 @@ function FrozenTrieNode(trie, index) {

this.letter = () => this.where();

this.radix = (parent) => {
if (typeof wordCached !== "undefined") return wordCached;
this.radix = (parent, cachecursor = null) => {
if (typeof wordCached !== "undefined") return [wordCached, cursorCached];

// location of this child among all other children of its parent
const loc = this.index - parent.firstChild();
Expand All @@ -551,12 +552,15 @@ function FrozenTrieNode(trie, index) {
const isThisNodeCompressed = this.compressed() && !this.flag();

if (isThisNodeCompressed || isPrevNodeCompressed) {
const entry = this.trie.nodecache.get(this.index);
if (entry) {
wordCached = entry;
const cc = this.trie.nodecache.find(this.index, cachecursor);
if (cc != null && cc.value != null) {
wordCached = cc.value;
cursorCached = cc.cursor;
if (config.debug) console.log("\t\t\tnode-c-hit", this.index);
return wordCached;
} else if (config.debug) console.log("\t\t\tnode-c-miss", this.index);
return [wordCached, cursorCached];
}

if (config.debug) console.log("\t\t\tnode-c-miss, add:", this.index);

const startchild = [];
const endchild = [];
Expand Down Expand Up @@ -612,7 +616,7 @@ function FrozenTrieNode(trie, index) {
};
}

return wordCached;
return [wordCached, cursorCached || null];
};

this.firstChild = () => {
Expand Down Expand Up @@ -757,8 +761,13 @@ FrozenTrie.prototype = {
const index = word.lastIndexOf(ENC_DELIM[0]);
if (index > 0) word = word.slice(0, index);

// cursor tracks position of previous cache-hit in frozentrie:nodecache
let cachecursor = null;
// the output of this fn
let returnValue = false;
// the current trie node to query
let node = this.getRoot();
// index in the incoming word utf-8 array
let i = 0;
while (i < word.length) {
if (node == null) {
Expand Down Expand Up @@ -794,14 +803,19 @@ FrozenTrie.prototype = {
while (high - low > 1) {
const probe = ((high + low) / 2) | 0;
const child = node.getChild(probe);
const r = child.radix(node);
const [r, cc] = child.radix(node, cachecursor);
const comp = r.word;
const w = word.slice(i, i + comp.length);

if (debug) {
console.log("\t\tl/h:", low, high, "p:", probe, "s:", comp, "w:", w);
const pr = cachecursor && cachecursor.range;
const nr = cc && cc.range;
if (cc) console.log("index", child.index, "now:cc", nr, "p:cc", pr);
}

cachecursor = cc != null ? cc : cachecursor;

if (comp[0] > w[0]) {
// binary search the lower half of the trie
high = r.loc;
Expand Down Expand Up @@ -830,6 +844,7 @@ FrozenTrie.prototype = {
i += w.length;
break;
}

if (debug) console.log("\tnext:", next && next.letter());
node = next; // next is null when no match is found
}
Expand All @@ -840,7 +855,9 @@ FrozenTrie.prototype = {
if (!returnValue) returnValue = new Map();
returnValue.set(TxtDec.decode(word.reverse()), node.value());
}

if (debug) console.log("...lookup complete:", returnValue);

// fixme: see above re returning "false" vs [false] vs [[0], false]
return returnValue;
},
Expand Down
14 changes: 12 additions & 2 deletions src/plugins/blocklist-wrapper/trie-cache.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ export class TrieCache {
}

put(lo, hi, val) {
if (hi < lo) {
this.log.w("put not allowed hi < lo:", hi, "<", lo);
if (hi < lo || val == null) {
this.log.w(val, "put not allowed hi < lo:", hi, "<", lo);
return;
}
try {
Expand All @@ -40,4 +40,14 @@ export class TrieCache {
this.log.e("put", lo, hi, val, e.stack);
}
}

find(n, cursor = null) {
try {
// returns {value: v, cursor: c}
return this.cache.find(n, cursor);
} catch (e) {
this.log.e("find", n, cursor, e.stack);
}
return false;
}
}

0 comments on commit 09d79c3

Please sign in to comment.