Perf: improve trie add

This commit is contained in:
SukkaW
2024-06-09 01:04:50 +08:00
parent 3709ae73ac
commit 479032d2ba

View File

@@ -87,7 +87,9 @@ export const createTrie = (from?: string[] | Set<string> | null, hostnameMode =
// During the adding of `[start]blog.skk.moe` and find out that there is a `[start].skk.moe` in the trie // During the adding of `[start]blog.skk.moe` and find out that there is a `[start].skk.moe` in the trie
// Dedupe the covered subdomain by skipping // Dedupe the covered subdomain by skipping
if (smolTree && (node.get('.')?.[SENTINEL])) return; if (smolTree && hostnameMode && (node.get('.')?.[SENTINEL])) {
return;
}
} else { } else {
const newNode = createNode(node); const newNode = createNode(node);
node.set(token, newNode); node.set(token, newNode);
@@ -113,19 +115,17 @@ export const createTrie = (from?: string[] | Set<string> | null, hostnameMode =
// Now the real leaf-est node is the new node, change the pointer to it // Now the real leaf-est node is the new node, change the pointer to it
node = newNode; node = newNode;
}
if (node.get('.')?.[SENTINEL] === true) { // we can use else-if here, because new node is empty, so we don't need to check the leading "."
} else if (node.get('.')?.[SENTINEL] === true) {
// Trying to add `example.com` when there is already a `.example.com` in the trie // Trying to add `example.com` when there is already a `.example.com` in the trie
// No need to increment size and set SENTINEL to true (skip this "new" item) // No need to increment size and set SENTINEL to true (skip this "new" item)
return; return;
} }
} } else if (!node[SENTINEL]) { // smol tree don't have size, so else-if here
// Do we need to increase size?
if (!node[SENTINEL]) {
size++; size++;
} }
node[SENTINEL] = true; node[SENTINEL] = true;
}; };
@@ -295,8 +295,7 @@ export const createTrie = (from?: string[] | Set<string> | null, hostnameMode =
* Method used to delete a prefix from the trie. * Method used to delete a prefix from the trie.
*/ */
const remove = (suffix: string): boolean => { const remove = (suffix: string): boolean => {
const suffixTokens = suffixToTokens(suffix); const res = getSingleChildLeaf(suffixToTokens(suffix));
const res = getSingleChildLeaf(suffixTokens);
if (res === null) return false; if (res === null) return false;
if (!res.node[SENTINEL]) return false; if (!res.node[SENTINEL]) return false;