Perf: slightly improve trie.whitelist performance

This commit is contained in:
SukkaW 2024-10-02 23:29:13 +08:00
parent 5d0a974b7f
commit 274c2453e2

View File

@ -328,12 +328,16 @@ abstract class Triebase<Meta = any> {
return results; return results;
}; };
public dumpWithMeta() { public dumpWithMeta(onSuffix: (suffix: string, meta: Meta | undefined) => void): void;
const results: Array<[string, Meta]> = []; public dumpWithMeta(): string[];
public dumpWithMeta(onSuffix?: (suffix: string, meta: Meta | undefined) => void): string[] | void {
const results: string[] = [];
this.walk((suffix, meta) => { const handleSuffix = onSuffix
results.push([fastStringArrayJoin(suffix, ''), meta]); ? (suffix: string[], meta: Meta | undefined) => onSuffix(fastStringArrayJoin(suffix, ''), meta)
}); : (suffix: string[]) => results.push(fastStringArrayJoin(suffix, ''));
this.walk(handleSuffix);
return results; return results;
}; };
@ -417,13 +421,13 @@ export class HostnameSmolTrie<Meta = any> extends Triebase<Meta> {
// Removing all the child nodes by empty the children // Removing all the child nodes by empty the children
// This removes the only child ".", which removes "blog.sub.example.com" // This removes the only child ".", which removes "blog.sub.example.com"
parent[2].clear(); parent[2].clear();
} } else {
// Trying to whitelist `example.com` when there is already a `.example.com` in the trie // Trying to whitelist `example.com` when there is already a `.example.com` in the trie
const dotNode = node[2].get('.'); const dotNode = node[2].get('.');
if (dotNode) { if (dotNode) {
dotNode[0] = false; dotNode[0] = false;
} }
}
// return early if not found // return early if not found
if (!node[0]) return; if (!node[0]) return;