Perf: offload reject fetch & parse into a worker thread

This commit is contained in:
SukkaW
2026-09-02 17:55:09 +08:00
parent 2647e5d0ed
commit 6449aabce7
5 changed files with 207 additions and 148 deletions

View File

@@ -189,19 +189,32 @@ export class FileOutput {
return this;
}
private addDomainsetLine(line: string) {
const otherPoundSign = line.lastIndexOf('#');
if (otherPoundSign > 0) {
line = line.slice(0, otherPoundSign).trimEnd();
}
if (line[0] === '.') {
this.addDomainSuffix(line);
} else {
this.domainTrie.add(line);
}
}
private async addFromDomainsetPromise(source: MaybePromise<AsyncIterable<string> | Iterable<string> | string[]>) {
for await (let line of await source) {
const otherPoundSign = line.lastIndexOf('#');
if (otherPoundSign > 0) {
line = line.slice(0, otherPoundSign).trimEnd();
}
if (line[0] === '.') {
this.addDomainSuffix(line);
} else {
this.domainTrie.add(line);
const resolved = await source;
// `for await` over a plain array still yields to the microtask queue once
// per element; the parsed remote lists are hundreds of thousands of lines
if (Array.isArray(resolved)) {
for (let i = 0, len = resolved.length; i < len; i++) {
this.addDomainsetLine(resolved[i]);
}
return;
}
for await (const line of resolved) {
this.addDomainsetLine(line);
}
}