Perf: preload/hoist more promises, make ts happy

This commit is contained in:
SukkaW 2024-01-14 23:47:22 +08:00
parent 6daf8e3bb4
commit a3e1a85c70
7 changed files with 80 additions and 80 deletions

View File

@ -8,14 +8,13 @@ import { SHARED_DESCRIPTION } from './lib/constants';
import { isProbablyIpv4, isProbablyIpv6 } from './lib/is-fast-ip'; import { isProbablyIpv4, isProbablyIpv6 } from './lib/is-fast-ip';
import { TTL, deserializeArray, fsCache, serializeArray } from './lib/cache-filesystem'; import { TTL, deserializeArray, fsCache, serializeArray } from './lib/cache-filesystem';
const getBogusNxDomainIPs = async () => { const URL = 'https://raw.githubusercontent.com/felixonmars/dnsmasq-china-list/master/bogus-nxdomain.china.conf';
const url = 'https://raw.githubusercontent.com/felixonmars/dnsmasq-china-list/master/bogus-nxdomain.china.conf';
return fsCache.apply( const getBogusNxDomainIPsPromise = fsCache.apply(
url, URL,
async () => { async () => {
const result: string[] = []; const result: string[] = [];
for await (const line of await fetchRemoteTextByLine('https://raw.githubusercontent.com/felixonmars/dnsmasq-china-list/master/bogus-nxdomain.china.conf')) { for await (const line of await fetchRemoteTextByLine(URL)) {
if (line && line.startsWith('bogus-nxdomain=')) { if (line && line.startsWith('bogus-nxdomain=')) {
const ip = line.slice(15).trim(); const ip = line.slice(15).trim();
if (isProbablyIpv4(ip)) { if (isProbablyIpv4(ip)) {
@ -33,11 +32,8 @@ const getBogusNxDomainIPs = async () => {
deserializer: deserializeArray deserializer: deserializeArray
} }
); );
};
export const buildAntiBogusDomain = task(import.meta.path, async (span) => { export const buildAntiBogusDomain = task(import.meta.path, async (span) => {
const bogusIpPromise = getBogusNxDomainIPs();
const result: string[] = []; const result: string[] = [];
for await (const line of readFileByLine(path.resolve(import.meta.dir, '../Source/ip/reject.conf'))) { for await (const line of readFileByLine(path.resolve(import.meta.dir, '../Source/ip/reject.conf'))) {
const l = processLine(line); const l = processLine(line);
@ -46,7 +42,7 @@ export const buildAntiBogusDomain = task(import.meta.path, async (span) => {
} }
} }
result.push(...(await bogusIpPromise)); result.push(...(await getBogusNxDomainIPsPromise));
const description = [ const description = [
...SHARED_DESCRIPTION, ...SHARED_DESCRIPTION,

View File

@ -2,16 +2,12 @@
import path from 'path'; import path from 'path';
import { createRuleset } from './lib/create-file'; import { createRuleset } from './lib/create-file';
import { parseFelixDnsmasq } from './lib/parse-dnsmasq'; import { parseFelixDnsmasq } from './lib/parse-dnsmasq';
import { traceAsync } from './lib/trace-runner';
import { task } from './trace'; import { task } from './trace';
import { SHARED_DESCRIPTION } from './lib/constants'; import { SHARED_DESCRIPTION } from './lib/constants';
import picocolors from 'picocolors';
import { createMemoizedPromise } from './lib/memo-promise'; import { createMemoizedPromise } from './lib/memo-promise';
import { TTL, deserializeArray, fsCache, serializeArray } from './lib/cache-filesystem'; import { TTL, deserializeArray, fsCache, serializeArray } from './lib/cache-filesystem';
export const getAppleCdnDomainsPromise = createMemoizedPromise(() => traceAsync( export const getAppleCdnDomainsPromise = createMemoizedPromise(() => fsCache.apply(
picocolors.gray('download dnsmasq-china-list apple.china.conf'),
() => fsCache.apply(
'https://raw.githubusercontent.com/felixonmars/dnsmasq-china-list/master/apple.china.conf', 'https://raw.githubusercontent.com/felixonmars/dnsmasq-china-list/master/apple.china.conf',
() => parseFelixDnsmasq('https://raw.githubusercontent.com/felixonmars/dnsmasq-china-list/master/apple.china.conf'), () => parseFelixDnsmasq('https://raw.githubusercontent.com/felixonmars/dnsmasq-china-list/master/apple.china.conf'),
{ {
@ -19,8 +15,6 @@ export const getAppleCdnDomainsPromise = createMemoizedPromise(() => traceAsync(
serializer: serializeArray, serializer: serializeArray,
deserializer: deserializeArray deserializer: deserializeArray
} }
),
picocolors.gray
)); ));
export const buildAppleCdn = task(import.meta.path, async (span) => { export const buildAppleCdn = task(import.meta.path, async (span) => {

View File

@ -6,7 +6,8 @@ import { task } from './trace';
import { processLine } from './lib/process-line'; import { processLine } from './lib/process-line';
import { SHARED_DESCRIPTION } from './lib/constants'; import { SHARED_DESCRIPTION } from './lib/constants';
import { getPublicSuffixListTextPromise } from './download-publicsuffixlist'; import { getPublicSuffixListTextPromise } from './download-publicsuffixlist';
const getS3OSSDomains = async (): Promise<Set<string>> => {
const getS3OSSDomainsPromise = (async (): Promise<Set<string>> => {
const trie = createTrie((await getPublicSuffixListTextPromise()).split('\n')); const trie = createTrie((await getPublicSuffixListTextPromise()).split('\n'));
/** /**
@ -39,14 +40,12 @@ const getS3OSSDomains = async (): Promise<Set<string>> => {
}); });
return S3OSSDomains; return S3OSSDomains;
}; })();
const buildCdnConf = task(import.meta.path, async (span) => { const buildCdnConf = task(import.meta.path, async (span) => {
/** @type {string[]} */ /** @type {string[]} */
const cdnDomainsList: string[] = []; const cdnDomainsList: string[] = [];
const getS3OSSDomainsPromise: Promise<Set<string>> = getS3OSSDomains();
for await (const l of readFileByLine(path.resolve(import.meta.dir, '../Source/non_ip/cdn.conf'))) { for await (const l of readFileByLine(path.resolve(import.meta.dir, '../Source/non_ip/cdn.conf'))) {
const line = processLine(l); const line = processLine(l);
if (line) { if (line) {

View File

@ -125,8 +125,9 @@ async function transformDomainset(parentSpan: Span, sourcePath: string, relative
* Output Surge RULE-SET and Clash classical text format * Output Surge RULE-SET and Clash classical text format
*/ */
async function transformRuleset(parentSpan: Span, sourcePath: string, relativePath: string) { async function transformRuleset(parentSpan: Span, sourcePath: string, relativePath: string) {
const span = parentSpan.traceChild(`transform ruleset: ${path.basename(sourcePath, path.extname(sourcePath))}`); return parentSpan
.traceChild(`transform ruleset: ${path.basename(sourcePath, path.extname(sourcePath))}`)
.traceAsyncFn(async (span) => {
const res = await processFile(span, sourcePath); const res = await processFile(span, sourcePath);
if (!res) return null; if (!res) return null;
@ -151,4 +152,5 @@ async function transformRuleset(parentSpan: Span, sourcePath: string, relativePa
path.resolve(outputSurgeDir, relativePath), path.resolve(outputSurgeDir, relativePath),
path.resolve(outputClashDir, `${relativePath.slice(0, -path.extname(relativePath).length)}.txt`) path.resolve(outputClashDir, `${relativePath.slice(0, -path.extname(relativePath).length)}.txt`)
); );
});
} }

View File

@ -24,6 +24,7 @@ const BLACKLIST = [
export const getMicrosoftCdnRulesetPromise = createMemoizedPromise(async () => { export const getMicrosoftCdnRulesetPromise = createMemoizedPromise(async () => {
const set = await traceAsync('fetch accelerated-domains.china.conf', async () => { const set = await traceAsync('fetch accelerated-domains.china.conf', async () => {
// First trie is to find the microsoft domains that matches probe domains
const trie = createTrie(); const trie = createTrie();
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')) {
if (line.startsWith('server=/') && line.endsWith('/114.114.114.114')) { if (line.startsWith('server=/') && line.endsWith('/114.114.114.114')) {
@ -34,6 +35,7 @@ export const getMicrosoftCdnRulesetPromise = createMemoizedPromise(async () => {
return new Set(PROBE_DOMAINS.flatMap(domain => trie.find(domain, false))); return new Set(PROBE_DOMAINS.flatMap(domain => trie.find(domain, false)));
}); });
// Second trie is to remove blacklisted domains
const trie2 = createTrie(set); const trie2 = createTrie(set);
const black = BLACKLIST.flatMap(domain => trie2.find(domain, true)); const black = BLACKLIST.flatMap(domain => trie2.find(domain, true));
for (let i = 0, len = black.length; i < len; i++) { for (let i = 0, len = black.length; i < len; i++) {

View File

@ -16,7 +16,10 @@ const folderAndFilesToBeDeployed = [
'LICENSE' 'LICENSE'
]; ];
export const buildPublic = task(import.meta.path, async () => { export const buildPublic = task(import.meta.path, async (span) => {
await span
.traceChild('copy public files')
.traceAsyncFn(async () => {
const filesToBeCopied = (await listDir( const filesToBeCopied = (await listDir(
rootPath, { rootPath, {
ignoreHidden: true, ignoreHidden: true,
@ -24,14 +27,18 @@ export const buildPublic = task(import.meta.path, async () => {
} }
)).filter(file => folderAndFilesToBeDeployed.some(folderOrFile => file.startsWith(folderOrFile))); )).filter(file => folderAndFilesToBeDeployed.some(folderOrFile => file.startsWith(folderOrFile)));
await Promise.all(filesToBeCopied.map(file => { return Promise.all(filesToBeCopied.map(file => {
const src = path.resolve(rootPath, file); const src = path.resolve(rootPath, file);
const dest = path.resolve(publicPath, file); const dest = path.resolve(publicPath, file);
return Bun.write(dest, Bun.file(src)); return Bun.write(dest, Bun.file(src));
})); }));
});
const html = await span
.traceChild('generate index.html')
.traceAsyncFn(() => treeDir(publicPath).then(generateHtml));
const html = generateHtml(await treeDir(publicPath));
return Bun.write(path.join(publicPath, 'index.html'), html); return Bun.write(path.join(publicPath, 'index.html'), html);
}); });

View File

@ -3,7 +3,7 @@ import { traceAsync } from './trace-runner';
import { createMemoizedPromise } from './memo-promise'; import { createMemoizedPromise } from './memo-promise';
import { getPublicSuffixListTextPromise } from '../download-publicsuffixlist'; import { getPublicSuffixListTextPromise } from '../download-publicsuffixlist';
const customFetch = (url: string | URL) => Promise.resolve(Bun.file(url)); const customFetch = (url: string | URL): Promise<Blob> => Promise.resolve(Bun.file(url));
export const getGorhillPublicSuffixPromise = createMemoizedPromise(() => traceAsync('create gorhill public suffix instance', async () => { export const getGorhillPublicSuffixPromise = createMemoizedPromise(() => traceAsync('create gorhill public suffix instance', async () => {
const [publicSuffixListDat, { default: gorhill }] = await Promise.all([ const [publicSuffixListDat, { default: gorhill }] = await Promise.all([