mirror of
https://github.com/SukkaW/Surge.git
synced 2025-12-13 17:50:34 +08:00
Refactor: prefer smol trie
This commit is contained in:
parent
3ca9122a84
commit
a8c53617b1
@ -21,7 +21,7 @@ const getS3OSSDomainsPromise = (async (): Promise<string[]> => {
|
||||
},
|
||||
[]
|
||||
),
|
||||
false
|
||||
true
|
||||
);
|
||||
|
||||
/**
|
||||
|
||||
@ -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) {
|
||||
|
||||
@ -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();
|
||||
}
|
||||
@ -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;
|
||||
|
||||
@ -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('.');
|
||||
}
|
||||
|
||||
|
||||
@ -15,7 +15,7 @@ export const parseDomesticList = async () => {
|
||||
}
|
||||
}
|
||||
|
||||
const trie = createTrie(set);
|
||||
const trie = createTrie(set, true);
|
||||
|
||||
const top5000 = new Set<string>();
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user