From a8c53617b19db8e5269e2983d3ba00fc2b389427 Mon Sep 17 00:00:00 2001 From: SukkaW Date: Mon, 23 Sep 2024 16:45:58 +0800 Subject: [PATCH] Refactor: prefer smol trie --- Build/build-cdn-download-conf.ts | 2 +- Build/build-microsoft-cdn.ts | 2 +- Build/lib/domain-deduper.ts | 15 --------------- Build/lib/rules/base.ts | 5 +---- Build/lib/trie.ts | 15 ++++++--------- Build/validate-domestic.ts | 2 +- 6 files changed, 10 insertions(+), 31 deletions(-) delete mode 100644 Build/lib/domain-deduper.ts diff --git a/Build/build-cdn-download-conf.ts b/Build/build-cdn-download-conf.ts index 4ff864ed..93863503 100644 --- a/Build/build-cdn-download-conf.ts +++ b/Build/build-cdn-download-conf.ts @@ -21,7 +21,7 @@ const getS3OSSDomainsPromise = (async (): Promise => { }, [] ), - false + true ); /** diff --git a/Build/build-microsoft-cdn.ts b/Build/build-microsoft-cdn.ts index 3f76e78f..6501d7f5 100644 --- a/Build/build-microsoft-cdn.ts +++ b/Build/build-microsoft-cdn.ts @@ -27,7 +27,7 @@ const BLACKLIST = [ export const getMicrosoftCdnRulesetPromise = createMemoizedPromise<[domains: string[], domainSuffixes: string[]]>(async () => { // First trie is to find the microsoft domains that matches probe domains - const trie = createTrie(null, false); + const trie = createTrie(null, true); for await (const line of await fetchRemoteTextByLine('https://raw.githubusercontent.com/felixonmars/dnsmasq-china-list/master/accelerated-domains.china.conf')) { const domain = extractDomainsFromFelixDnsmasq(line); if (domain) { diff --git a/Build/lib/domain-deduper.ts b/Build/lib/domain-deduper.ts deleted file mode 100644 index c85611a7..00000000 --- a/Build/lib/domain-deduper.ts +++ /dev/null @@ -1,15 +0,0 @@ -import { createTrie } from './trie'; -import type { Trie } from './trie'; - -export function domainsetDeduper(inputDomains: string[] | Trie): string[] { - let trie: Trie; - if (Array.isArray(inputDomains)) { - trie = createTrie(inputDomains, true); - } else if (inputDomains.smolTree) { - trie = inputDomains; - } else { - throw new Error('Invalid trie'); - } - - return trie.dump(); -} diff --git a/Build/lib/rules/base.ts b/Build/lib/rules/base.ts index 07b4d797..65282356 100644 --- a/Build/lib/rules/base.ts +++ b/Build/lib/rules/base.ts @@ -64,13 +64,10 @@ export abstract class RuleOutput { return result; }; - protected span: Span; - constructor( - span: Span, + protected readonly span: Span, protected readonly id: string ) { - this.span = span.traceChild('RuleOutput'); } protected title: string | null = null; diff --git a/Build/lib/trie.ts b/Build/lib/trie.ts index 0a0db1d8..030b79e1 100644 --- a/Build/lib/trie.ts +++ b/Build/lib/trie.ts @@ -4,8 +4,7 @@ import { fastStringArrayJoin } from './misc'; import util from 'node:util'; - -const noop = () => { /** noop */ }; +import { noop } from 'foxact/noop'; type TrieNode = [ boolean, /** sentinel */ @@ -121,10 +120,8 @@ export const createTrie = (from?: string[] | Set | null, smo if (suffix[0] === '.') { // Trying to add `[start].sub.example.com` where there is already a `[start]blog.sub.example.com` in the trie - const parent = node[1]!; - // Make sure parent `[start]sub.example.com` (without dot) is removed (SETINEL to false) - parent[0] = false; + (/** parent */ node[1]!)[0] = false; // Removing the rest of the parent's child nodes node[2].clear(); @@ -308,9 +305,9 @@ export const createTrie = (from?: string[] | Set | null, smo inputSuffix: string, /** @default true */ includeEqualWithSuffix = true ): string[] => { - if (smolTree) { - throw new Error('A Trie with smolTree enabled cannot perform find!'); - } + // if (smolTree) { + // throw new Error('A Trie with smolTree enabled cannot perform find!'); + // } const inputTokens = hostnameToTokens(inputSuffix); const res = walkIntoLeafWithTokens(inputTokens); @@ -419,7 +416,7 @@ export const createTrie = (from?: string[] | Set | null, smo if (tokens[0] === '.') { // If there is a `[start]sub.example.com` here, remove it parent[0] = false; - // Removing all the child nodes by disconnecting "." + // Removing all the child nodes by disconnecting ".", which removes "blog.sub.example.com" parent[2].delete('.'); } diff --git a/Build/validate-domestic.ts b/Build/validate-domestic.ts index a09cc34d..707358f8 100644 --- a/Build/validate-domestic.ts +++ b/Build/validate-domestic.ts @@ -15,7 +15,7 @@ export const parseDomesticList = async () => { } } - const trie = createTrie(set); + const trie = createTrie(set, true); const top5000 = new Set();