Perf: optimiaztions, avoid spread operator

This commit is contained in:
SukkaW 2024-06-26 18:30:41 +08:00
parent 33636285e9
commit 098e8815ae
9 changed files with 110 additions and 62 deletions

View File

@ -18,8 +18,7 @@ export const getAppleCdnDomainsPromise = createMemoizedPromise(() => fsFetchCach
));
export const buildAppleCdn = task(import.meta.main, import.meta.path)(async (span) => {
const promise = getAppleCdnDomainsPromise();
const res: string[] = await span.traceChildPromise('get apple cdn domains', promise);
const res: string[] = await span.traceChildPromise('get apple cdn domains', getAppleCdnDomainsPromise());
const description = [
...SHARED_DESCRIPTION,

View File

@ -11,9 +11,7 @@ export const buildCloudMounterRules = task(import.meta.main, import.meta.path)(a
// AND,((SRC-IP,192.168.1.110), (DOMAIN, example.com))
const results = DOMAINS.flatMap(domain => {
return PROCESS_NAMES.map(process => {
return `AND,((${domain}),(PROCESS-NAME,${process}))`;
});
return PROCESS_NAMES.map(process => `AND,((${domain}),(PROCESS-NAME,${process}))`);
});
const description = SHARED_DESCRIPTION;

View File

@ -85,12 +85,8 @@ export const buildDomesticRuleset = task(import.meta.main, import.meta.path)(asy
`#!desc=Last Updated: ${new Date().toISOString()}`,
'',
'[Host]',
...dataset.flatMap(([, { domains, dns, ...rest }]) => [
...(
'hosts' in rest
? Object.entries(rest.hosts).flatMap(([dns, ips]: [dns: string, ips: string[]]) => `${dns} = ${ips.join(', ')}`)
: []
),
...dataset.flatMap(([, { domains, dns, hosts }]) => [
...Object.entries(hosts).flatMap(([dns, ips]: [dns: string, ips: string[]]) => `${dns} = ${ips.join(', ')}`),
...domains.flatMap((domain) => [
`${domain} = server:${dns}`,
`*.${domain} = server:${dns}`

View File

@ -2,6 +2,7 @@ import path from 'path';
import { task } from './trace';
import { compareAndWriteFile } from './lib/create-file';
import { getHostname } from 'tldts';
import { isTruthy } from './lib/misc';
function escapeRegExp(string = '') {
const reRegExpChar = /[$()*+.?[\\\]^{|}]/g;
@ -122,7 +123,7 @@ export const buildRedirectModule = task(import.meta.main, import.meta.path)(asyn
const domains = Array.from(new Set([
...REDIRECT_MIRROR.map(([from]) => getHostname(from, { detectIp: false })),
...REDIRECT_FAKEWEBSITES.flatMap(([from]) => [from, `www.${from}`])
])).filter(Boolean);
])).filter(isTruthy);
return compareAndWriteFile(
span,

View File

@ -10,6 +10,8 @@ import { getChnCidrPromise } from './build-chn-cidr';
import { getTelegramCIDRPromise } from './build-telegram-cidr';
import { compareAndWriteFile } from './lib/create-file';
import { getMicrosoftCdnRulesetPromise } from './build-microsoft-cdn';
import { isTruthy } from './lib/misc';
import { appendArrayInPlace } from './lib/append-array-in-place';
const POLICY_GROUPS: Array<[name: string, insertProxy: boolean, insertDirect: boolean]> = [
['Default Proxy', true, false],
@ -120,8 +122,6 @@ export const buildSSPanelUIMAppProfile = task(import.meta.main, import.meta.path
);
});
const isTruthy = <T>(i: T | 0 | '' | false | null | undefined): i is T => !!i;
function generateAppProfile(
directDomains: string[],
microsoftAppleDomains: string[],
@ -135,7 +135,7 @@ function generateAppProfile(
globalCidrs: string[],
lanCidrs: string[]
) {
return [
const redults = [
'<?php',
'',
`// # Build ${new Date().toISOString()}`,
@ -172,8 +172,12 @@ function generateAppProfile(
return acc;
}, [])).slice(1, -1)}];`,
'$_ENV[\'Clash_Group_Config\'] = [',
' \'proxy-groups\' => [',
...POLICY_GROUPS.flatMap(([name, insertProxy, insertDirect]) => {
' \'proxy-groups\' => ['
];
appendArrayInPlace(
redults,
POLICY_GROUPS.flatMap(([name, insertProxy, insertDirect]) => {
return [
' [',
` 'name' => '${name}',`,
@ -184,33 +188,79 @@ function generateAppProfile(
' ],',
' ],'
].filter(isTruthy);
}),
' ],',
' \'rules\' => [',
// domestic - domains
...directDomains.map(line => ` '${line},Domestic',`),
// microsoft & apple - domains
...microsoftAppleDomains.map(line => ` '${line},Microsoft & Apple',`),
// stream - domains
...streamDomains.map(line => ` '${line},Stream',`),
// steam download - domains
...steamDomains.map(line => ` '${line},Steam Download',`),
// global - domains
...globalDomains.map(line => ` '${line},Global',`),
// microsoft & apple - ip cidr (nope)
// lan - domains
...lanDomains.map(line => ` '${line},DIRECT',`),
// stream - ip cidr
...streamCidrs.map(line => ` '${line},Stream',`),
// global - ip cidr
...globalCidrs.map(line => ` '${line},Global',`),
// domestic - ip cidr
...directCidrs.map(line => ` '${line},Domestic',`),
// lan - ip cidr
...lanCidrs.map(line => ` '${line},DIRECT',`),
// match
' \'MATCH,Final Match\',',
' ],',
'];'
];
})
);
appendArrayInPlace(
redults,
[
' ],',
' \'rules\' => ['
]
);
// domestic - domains
appendArrayInPlace(
redults,
directDomains.map(line => ` '${line},Domestic',`)
);
// microsoft & apple - domains
appendArrayInPlace(
redults,
microsoftAppleDomains.map(line => ` '${line},Microsoft & Apple',`)
);
// stream - domains
appendArrayInPlace(
redults,
streamDomains.map(line => ` '${line},Stream',`)
);
// steam download - domains
appendArrayInPlace(
redults,
steamDomains.map(line => ` '${line},Steam Download',`)
);
// global - domains
appendArrayInPlace(
redults,
globalDomains.map(line => ` '${line},Global',`)
);
// microsoft & apple - ip cidr (nope)
// lan - domains
appendArrayInPlace(
redults,
lanDomains.map(line => ` '${line},DIRECT',`)
);
// stream - ip cidr
appendArrayInPlace(
redults,
streamCidrs.map(line => ` '${line},Stream',`)
);
// global - ip cidr
appendArrayInPlace(
redults,
globalCidrs.map(line => ` '${line},Global',`)
);
// domestic - ip cidr
appendArrayInPlace(
redults,
directCidrs.map(line => ` '${line},Domestic',`)
);
// lan - ip cidr
appendArrayInPlace(
redults,
lanCidrs.map(line => ` '${line},DIRECT',`)
);
// match
appendArrayInPlace(
redults,
[
' \'MATCH,Final Match\',',
' ],',
'];'
]
);
return redults;
}

1
Build/lib/misc.ts Normal file
View File

@ -0,0 +1 @@
export const isTruthy = <T>(i: T | 0 | '' | false | null | undefined): i is T => !!i;

View File

@ -72,26 +72,20 @@ export const createSpan = (name: string, parentTraceResult?: TraceResult): Span
stop,
traceChild,
traceSyncFn<T>(fn: (span: Span) => T) {
try {
return fn(span);
} finally {
span.stop();
}
const res = fn(span);
span.stop();
return res;
},
async traceAsyncFn<T>(fn: (span: Span) => T | Promise<T>): Promise<T> {
try {
return await fn(span);
} finally {
span.stop();
}
const res = await fn(span);
span.stop();
return res;
},
traceResult: curTraceResult,
async tracePromise<T>(promise: Promise<T>): Promise<T> {
try {
return await promise;
} finally {
span.stop();
}
const res = await promise;
span.stop();
return res;
},
traceChildSync: <T>(name: string, fn: (span: Span) => T): T => traceChild(name).traceSyncFn(fn),
traceChildAsync: <T>(name: string, fn: (span: Span) => T | Promise<T>): Promise<T> => traceChild(name).traceAsyncFn(fn),

View File

@ -1,5 +1,5 @@
export interface DNSMapping {
hosts?: {
hosts: {
[domain: string]: string[]
},
dns: string,
@ -9,6 +9,7 @@ export interface DNSMapping {
export const DIRECTS = {
ROUTER: {
dns: 'system',
hosts: {},
domains: [
// Aruba Router
'instant.arubanetworks.com',
@ -77,6 +78,7 @@ export const DIRECTS = {
},
SYSTEM: {
dns: 'system',
hosts: {},
domains: [
'_hotspot_.m2m',
'hotspot.cslwifi.com',
@ -92,6 +94,7 @@ export const DIRECTS = {
export const LANS = {
LAN: {
dns: 'system',
hosts: {},
domains: [
'lan',
'localhost',

View File

@ -141,18 +141,21 @@ export const DOMESTICS = {
},
BILIBILI_ALI: {
dns: 'quic://dns.alidns.com:853',
hosts: {},
domains: [
'upos-sz-mirrorali.bilivideo.com'
]
},
BILIBILI_BD: {
dns: '180.76.76.76',
hosts: {},
domains: [
'upos-sz-mirrorbos.bilivideo.com'
]
},
BILIBILI: {
dns: 'https://doh.pub/dns-query',
hosts: {},
domains: [
'upos-sz-mirrorcoso1.bilivideo.com',
'acg.tv',
@ -179,6 +182,7 @@ export const DOMESTICS = {
},
XIAOMI: {
dns: 'https://doh.pub/dns-query',
hosts: {},
domains: [
'mi.com',
'duokan.com',
@ -195,6 +199,7 @@ export const DOMESTICS = {
},
BYTEDANCE: {
dns: '180.184.2.2',
hosts: {},
domains: [
'bytecdn.cn',
'toutiaoimg.com',
@ -235,6 +240,7 @@ export const DOMESTICS = {
},
BAIDU: {
dns: '180.76.76.76',
hosts: {},
domains: [
'91.com',
'hao123.com',