Perf: avoid iterate domainSets twice

This commit is contained in:
SukkaW 2024-07-21 23:02:09 +08:00
parent 9d47cb9f57
commit 26a4d72b06

View File

@ -111,32 +111,37 @@ export const buildRejectDomainSet = task(import.meta.main, import.meta.path)(asy
return domainKeywordsSet; return domainKeywordsSet;
}); });
const [baseTrie, extraTrie] = span.traceChildSync('create smol trie while deduping black keywords', () => { const [baseTrie, extraTrie] = span.traceChildSync('create smol trie while deduping black keywords', (childSpan) => {
const baseTrie = createTrie(null, true, true); const baseTrie = createTrie(null, true, true);
const extraTrie = createTrie(null, true, true); const extraTrie = createTrie(null, true, true);
const kwfilter = createKeywordFilter(domainKeywordsSet); const kwfilter = createKeywordFilter(domainKeywordsSet);
for (const domain of domainSets) { childSpan.traceChildSync('add items to trie (extra)', () => {
// exclude keyword when creating trie for (const domain of domainSetsExtra) {
if (!kwfilter(domain)) { // exclude keyword when creating trie
baseTrie.add(domain); if (!kwfilter(domain)) {
extraTrie.add(domain);
}
} }
} });
for (const domain of domainSetsExtra) { childSpan.traceChildSync('add items to trie (base) + dedupe extra trie', () => {
// exclude keyword when creating trie for (const domain of domainSets) {
if (!kwfilter(domain)) { // exclude keyword when creating trie
extraTrie.add(domain); if (!kwfilter(domain)) {
baseTrie.add(domain);
extraTrie.whitelist(domain);
}
} }
} });
return [baseTrie, extraTrie] as const; return [baseTrie, extraTrie] as const;
}); });
span.traceChildSync('dedupe from white suffixes (base)', () => filterRuleWhitelistDomainSets.forEach(baseTrie.whitelist)); span.traceChildSync('dedupe from white suffixes (base)', () => filterRuleWhitelistDomainSets.forEach(baseTrie.whitelist));
span.traceChildSync('dedupe from white suffixes and base (extra)', () => { span.traceChildSync('dedupe from white suffixes and base (extra)', () => {
domainSets.forEach(extraTrie.whitelist);
filterRuleWhitelistDomainSets.forEach(extraTrie.whitelist); filterRuleWhitelistDomainSets.forEach(extraTrie.whitelist);
}); });