ソースを参照

Refactor: prefer smol trie

SukkaW 1 年間 前
コミット
a8c53617b1

+ 1 - 1
Build/build-cdn-download-conf.ts

@@ -21,7 +21,7 @@ const getS3OSSDomainsPromise = (async (): Promise<string[]> => {
       },
       []
     ),
-    false
+    true
   );
 
   /**

+ 1 - 1
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) {

+ 0 - 15
Build/lib/domain-deduper.ts

@@ -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();
-}

+ 1 - 4
Build/lib/rules/base.ts

@@ -64,13 +64,10 @@ export abstract class RuleOutput<TPreprocessed = unknown> {
     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;

+ 6 - 9
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<Meta = any> = [
   boolean, /** sentinel */
@@ -121,10 +120,8 @@ export const createTrie = <Meta = any>(from?: string[] | Set<string> | 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 = <Meta = any>(from?: string[] | Set<string> | 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 = <Meta = any>(from?: string[] | Set<string> | 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('.');
     }
 

+ 1 - 1
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<string>();