mirror of
https://github.com/SukkaW/Surge.git
synced 2025-12-12 17:20:35 +08:00
Perf: reduce if in trie.add
This commit is contained in:
parent
fa85fff3c3
commit
6031d9652e
@ -25,14 +25,8 @@ const deepTrieNodeToJSON = (node: TrieNode) => {
|
||||
return obj;
|
||||
};
|
||||
|
||||
function trieNodeInspectCustom(this: TrieNode) {
|
||||
return JSON.stringify(deepTrieNodeToJSON(this), null, 2);
|
||||
}
|
||||
|
||||
const createNode = (parent: TrieNode | null = null): TrieNode => {
|
||||
const node = [false, parent, new Map<string, TrieNode>()] as TrieNode;
|
||||
Object.defineProperty(node, Bun.inspect.custom, { value: trieNodeInspectCustom });
|
||||
return node;
|
||||
return [false, parent, new Map<string, TrieNode>()] as TrieNode;
|
||||
};
|
||||
|
||||
const hostnameToTokens = (hostname: string): string[] => {
|
||||
@ -70,7 +64,8 @@ export const createTrie = (from?: string[] | Set<string> | null, hostnameMode =
|
||||
/**
|
||||
* Method used to add the given suffix to the trie.
|
||||
*/
|
||||
const add = (suffix: string): void => {
|
||||
const add = smolTree
|
||||
? (suffix: string): void => {
|
||||
let node: TrieNode = root;
|
||||
let token: string;
|
||||
|
||||
@ -84,7 +79,7 @@ export const createTrie = (from?: string[] | Set<string> | null, hostnameMode =
|
||||
|
||||
// During the adding of `[start]blog|.skk.moe` and find out that there is a `[start].skk.moe` in the trie
|
||||
// Dedupe the covered subdomain by skipping
|
||||
if (smolTree && token === '.' && node[0]) {
|
||||
if (token === '.' && node[0]) {
|
||||
return;
|
||||
}
|
||||
} else {
|
||||
@ -95,7 +90,6 @@ export const createTrie = (from?: string[] | Set<string> | null, hostnameMode =
|
||||
}
|
||||
|
||||
// If we are in smolTree mode, we need to do something at the end of the loop
|
||||
if (smolTree) {
|
||||
if (tokens[0] === '.') {
|
||||
// Trying to add `[start].sub.example.com` where there is already a `[start]blog.sub.example.com` in the trie
|
||||
|
||||
@ -114,11 +108,31 @@ export const createTrie = (from?: string[] | Set<string> | null, hostnameMode =
|
||||
// No need to increment size and set SENTINEL to true (skip this "new" item)
|
||||
return;
|
||||
}
|
||||
} else if (!node[0]) { // smol tree don't have size, so else-if here
|
||||
size++;
|
||||
}
|
||||
|
||||
node[0] = true;
|
||||
}
|
||||
: (suffix: string): void => {
|
||||
let node: TrieNode = root;
|
||||
let token: string;
|
||||
|
||||
const tokens = suffixToTokens(suffix);
|
||||
|
||||
for (let i = tokens.length - 1; i >= 0; i--) {
|
||||
token = tokens[i];
|
||||
|
||||
if (node[2].has(token)) {
|
||||
node = node[2].get(token)!;
|
||||
} else {
|
||||
const newNode = createNode(node);
|
||||
node[2].set(token, newNode);
|
||||
node = newNode;
|
||||
}
|
||||
}
|
||||
|
||||
if (!node[0]) { // smol tree don't have size, so else-if here
|
||||
size++;
|
||||
node[0] = true;
|
||||
}
|
||||
};
|
||||
|
||||
const walkIntoLeafWithTokens = (
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user