mirror of
https://github.com/SukkaW/Surge.git
synced 2026-01-31 19:11:53 +08:00
Perf: many changes
- Hoist process hosts line callback - Reduce dp hosts file size - Reduce domain sort
This commit is contained in:
@@ -46,37 +46,38 @@ export function processDomainLists(span: Span, domainListsUrl: string, includeAl
|
||||
}
|
||||
));
|
||||
}
|
||||
|
||||
const hostsLineCb = (l: string, set: Set<string>, includeAllSubDomain: boolean, meta: string) => {
|
||||
const line = processLine(l);
|
||||
if (!line) {
|
||||
return;
|
||||
}
|
||||
|
||||
const _domain = line.split(/\s/)[1]?.trim();
|
||||
if (!_domain) {
|
||||
return;
|
||||
}
|
||||
const domain = normalizeDomain(_domain);
|
||||
if (!domain) {
|
||||
return;
|
||||
}
|
||||
if (DEBUG_DOMAIN_TO_FIND && domain.includes(DEBUG_DOMAIN_TO_FIND)) {
|
||||
console.warn(picocolors.red(meta), '(black)', domain.replaceAll(DEBUG_DOMAIN_TO_FIND, picocolors.bold(DEBUG_DOMAIN_TO_FIND)));
|
||||
foundDebugDomain = true;
|
||||
}
|
||||
|
||||
set.add(includeAllSubDomain ? `.${domain}` : domain);
|
||||
};
|
||||
|
||||
export function processHosts(span: Span, hostsUrl: string, mirrors: string[] | null, includeAllSubDomain = false, ttl: number | null = null) {
|
||||
const domainSets = new Set<string>();
|
||||
|
||||
const lineCb = (l: string) => {
|
||||
const line = processLine(l);
|
||||
if (!line) {
|
||||
return;
|
||||
}
|
||||
|
||||
const _domain = line.split(/\s/)[1]?.trim();
|
||||
if (!_domain) {
|
||||
return;
|
||||
}
|
||||
const domain = normalizeDomain(_domain);
|
||||
if (!domain) {
|
||||
return;
|
||||
}
|
||||
if (DEBUG_DOMAIN_TO_FIND && domain.includes(DEBUG_DOMAIN_TO_FIND)) {
|
||||
console.warn(picocolors.red(hostsUrl), '(black)', domain.replaceAll(DEBUG_DOMAIN_TO_FIND, picocolors.bold(DEBUG_DOMAIN_TO_FIND)));
|
||||
foundDebugDomain = true;
|
||||
}
|
||||
|
||||
domainSets.add(includeAllSubDomain ? `.${domain}` : domain);
|
||||
};
|
||||
|
||||
return span.traceChild(`processhosts: ${hostsUrl}`).traceAsyncFn((childSpan) => fsFetchCache.apply(
|
||||
hostsUrl,
|
||||
async () => {
|
||||
if (mirrors == null || mirrors.length === 0) {
|
||||
for await (const l of await fetchRemoteTextByLine(hostsUrl)) {
|
||||
lineCb(l);
|
||||
hostsLineCb(l, domainSets, includeAllSubDomain, hostsUrl);
|
||||
}
|
||||
} else {
|
||||
const filterRules = await childSpan
|
||||
@@ -85,7 +86,7 @@ export function processHosts(span: Span, hostsUrl: string, mirrors: string[] | n
|
||||
|
||||
childSpan.traceChild('parse hosts').traceSyncFn(() => {
|
||||
for (let i = 0, len = filterRules.length; i < len; i++) {
|
||||
lineCb(filterRules[i]);
|
||||
hostsLineCb(filterRules[i], domainSets, includeAllSubDomain, hostsUrl);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@@ -9,7 +9,8 @@ export const HOSTS: HostsSource[] = [
|
||||
true,
|
||||
TTL.THREE_HOURS()
|
||||
],
|
||||
['https://someonewhocares.org/hosts/hosts', null, true, TTL.THREE_HOURS()],
|
||||
// Dan Pollock's hosts file, 0.0.0.0 version is 30 KiB smaller
|
||||
['https://someonewhocares.org/hosts/zero/hosts', null, true, TTL.THREE_HOURS()],
|
||||
// no coin list is not actively maintained, but it updates daily when being maintained, so we set a 3 days cache ttl
|
||||
['https://raw.githubusercontent.com/hoshsadiq/adblock-nocoin-list/master/hosts.txt', null, true, TTL.THREE_DAYS()],
|
||||
// have not been updated for more than a year, so we set a 14 days cache ttl
|
||||
|
||||
@@ -42,7 +42,15 @@ export const sortDomains = (inputs: string[], gorhill: PublicSuffixList) => {
|
||||
|
||||
const sorter = (a: string, b: string) => {
|
||||
if (a === b) return 0;
|
||||
return compare(domains.get(a)!, domains.get(b)!) || compare(a, b);
|
||||
|
||||
const $a = domains.get(a)!;
|
||||
const $b = domains.get(b)!;
|
||||
|
||||
// avoid compare same thing twice
|
||||
if (a === $a && b === $b) {
|
||||
return compare(a, b);
|
||||
}
|
||||
return compare($a, $b) || compare(a, b);
|
||||
};
|
||||
|
||||
return inputs.sort(sorter);
|
||||
|
||||
Reference in New Issue
Block a user