Refactor: prefer smol trie

This commit is contained in:
SukkaW 2024-09-23 16:45:58 +08:00
parent 3ca9122a84
commit a8c53617b1
6 changed files with 10 additions and 31 deletions

View File

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

View File

@ -27,7 +27,7 @@ const BLACKLIST = [
export const getMicrosoftCdnRulesetPromise = createMemoizedPromise<[domains: string[], domainSuffixes: string[]]>(async () => { export const getMicrosoftCdnRulesetPromise = createMemoizedPromise<[domains: string[], domainSuffixes: string[]]>(async () => {
// First trie is to find the microsoft domains that matches probe domains // 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')) { for await (const line of await fetchRemoteTextByLine('https://raw.githubusercontent.com/felixonmars/dnsmasq-china-list/master/accelerated-domains.china.conf')) {
const domain = extractDomainsFromFelixDnsmasq(line); const domain = extractDomainsFromFelixDnsmasq(line);
if (domain) { if (domain) {

View File

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

View File

@ -64,13 +64,10 @@ export abstract class RuleOutput<TPreprocessed = unknown> {
return result; return result;
}; };
protected span: Span;
constructor( constructor(
span: Span, protected readonly span: Span,
protected readonly id: string protected readonly id: string
) { ) {
this.span = span.traceChild('RuleOutput');
} }
protected title: string | null = null; protected title: string | null = null;

View File

@ -4,8 +4,7 @@
import { fastStringArrayJoin } from './misc'; import { fastStringArrayJoin } from './misc';
import util from 'node:util'; import util from 'node:util';
import { noop } from 'foxact/noop';
const noop = () => { /** noop */ };
type TrieNode<Meta = any> = [ type TrieNode<Meta = any> = [
boolean, /** sentinel */ boolean, /** sentinel */
@ -121,10 +120,8 @@ export const createTrie = <Meta = any>(from?: string[] | Set<string> | null, smo
if (suffix[0] === '.') { if (suffix[0] === '.') {
// Trying to add `[start].sub.example.com` where there is already a `[start]blog.sub.example.com` in the trie // 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) // 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 // Removing the rest of the parent's child nodes
node[2].clear(); node[2].clear();
@ -308,9 +305,9 @@ export const createTrie = <Meta = any>(from?: string[] | Set<string> | null, smo
inputSuffix: string, inputSuffix: string,
/** @default true */ includeEqualWithSuffix = true /** @default true */ includeEqualWithSuffix = true
): string[] => { ): string[] => {
if (smolTree) { // if (smolTree) {
throw new Error('A Trie with smolTree enabled cannot perform find!'); // throw new Error('A Trie with smolTree enabled cannot perform find!');
} // }
const inputTokens = hostnameToTokens(inputSuffix); const inputTokens = hostnameToTokens(inputSuffix);
const res = walkIntoLeafWithTokens(inputTokens); const res = walkIntoLeafWithTokens(inputTokens);
@ -419,7 +416,7 @@ export const createTrie = <Meta = any>(from?: string[] | Set<string> | null, smo
if (tokens[0] === '.') { if (tokens[0] === '.') {
// If there is a `[start]sub.example.com` here, remove it // If there is a `[start]sub.example.com` here, remove it
parent[0] = false; 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('.'); parent[2].delete('.');
} }

View File

@ -15,7 +15,7 @@ export const parseDomesticList = async () => {
} }
} }
const trie = createTrie(set); const trie = createTrie(set, true);
const top5000 = new Set<string>(); const top5000 = new Set<string>();