From 33636285e9ae8c2c9ea0369382ca54d4a1d6f9ae Mon Sep 17 00:00:00 2001 From: SukkaW Date: Wed, 26 Jun 2024 17:49:32 +0800 Subject: [PATCH] Perf: avoid spread operator --- Build/build-common.ts | 75 +++++++++++-------- ...c-direct-lan-ruleset-dns-mapping-module.ts | 11 ++- Build/build-mitm-hostname.ts | 10 +-- Build/build-reject-domainset.ts | 9 +-- Build/build-sgmodule-redirect.ts | 14 ++-- Build/lib/constants.ts | 2 +- Build/lib/create-file.ts | 2 +- Build/lib/text-decoder-stream.ts | 2 +- 8 files changed, 69 insertions(+), 56 deletions(-) diff --git a/Build/build-common.ts b/Build/build-common.ts index 8f17b237..f982be3f 100644 --- a/Build/build-common.ts +++ b/Build/build-common.ts @@ -8,8 +8,8 @@ import { domainDeduper } from './lib/domain-deduper'; import type { Span } from './trace'; import { task } from './trace'; import { SHARED_DESCRIPTION } from './lib/constants'; -import picocolors from 'picocolors'; import { fdir as Fdir } from 'fdir'; +import { appendArrayInPlace } from './lib/append-array-in-place'; const MAGIC_COMMAND_SKIP = '# $ custom_build_script'; const MAGIC_COMMAND_TITLE = '# $ meta_title '; @@ -24,31 +24,45 @@ export const buildCommon = task(import.meta.main, import.meta.path)(async (span) const paths = await new Fdir() .withRelativePaths() + // .exclude((dirName, dirPath) => { + // if (dirName === 'domainset' || dirName === 'ip' || dirName === 'non_ip') { + // return false; + // } + + // console.error(picocolors.red(`[build-comman] Unknown dir: ${dirPath}`)); + + // return true; + // }) + .filter((filepath, isDirectory) => { + if (isDirectory) return true; + + const extname = path.extname(filepath); + if (extname === '.js' || extname === '.ts') { + return false; + } + + return true; + }) .crawl(sourceDir) .withPromise(); for (let i = 0, len = paths.length; i < len; i++) { const relativePath = paths[i]; - - const extname = path.extname(relativePath); - if (extname === '.js' || extname === '.ts') { - continue; - } const fullPath = sourceDir + path.sep + relativePath; if (relativePath.startsWith('domainset/')) { promises.push(transformDomainset(span, fullPath, relativePath)); continue; } - if ( - relativePath.startsWith('ip/') - || relativePath.startsWith('non_ip/') - ) { - promises.push(transformRuleset(span, fullPath, relativePath)); - continue; - } + // if ( + // relativePath.startsWith('ip/') + // || relativePath.startsWith('non_ip/') + // ) { + promises.push(transformRuleset(span, fullPath, relativePath)); + // continue; + // } - console.error(picocolors.red(`[build-comman] Unknown file: ${relativePath}`)); + // console.error(picocolors.red(`[build-comman] Unknown file: ${relativePath}`)); } return Promise.all(promises); @@ -103,14 +117,15 @@ function transformDomainset(parentSpan: Span, sourcePath: string, relativePath: const [title, descriptions, lines] = res; const deduped = domainDeduper(lines); - const description = [ - ...SHARED_DESCRIPTION, - ...( - descriptions.length - ? ['', ...descriptions] - : [] - ) - ]; + + let description: string[]; + if (descriptions.length) { + description = SHARED_DESCRIPTION.slice(); + description.push(''); + appendArrayInPlace(description, descriptions); + } else { + description = SHARED_DESCRIPTION; + } return createRuleset( span, @@ -138,14 +153,14 @@ async function transformRuleset(parentSpan: Span, sourcePath: string, relativePa const [title, descriptions, lines] = res; - const description = [ - ...SHARED_DESCRIPTION, - ...( - descriptions.length - ? ['', ...descriptions] - : [] - ) - ]; + let description: string[]; + if (descriptions.length) { + description = SHARED_DESCRIPTION.slice(); + description.push(''); + appendArrayInPlace(description, descriptions); + } else { + description = SHARED_DESCRIPTION; + } return createRuleset( span, diff --git a/Build/build-domestic-direct-lan-ruleset-dns-mapping-module.ts b/Build/build-domestic-direct-lan-ruleset-dns-mapping-module.ts index 81d4661e..431bed6c 100644 --- a/Build/build-domestic-direct-lan-ruleset-dns-mapping-module.ts +++ b/Build/build-domestic-direct-lan-ruleset-dns-mapping-module.ts @@ -8,6 +8,7 @@ import { task } from './trace'; import { SHARED_DESCRIPTION } from './lib/constants'; import { createMemoizedPromise } from './lib/memo-promise'; import * as yaml from 'yaml'; +import { appendArrayInPlace } from './lib/append-array-in-place'; export const getDomesticAndDirectDomainsRulesetPromise = createMemoizedPromise(async () => { const domestics = await readFileIntoProcessedArray(path.resolve(import.meta.dir, '../Source/non_ip/domestic.conf')); @@ -15,13 +16,13 @@ export const getDomesticAndDirectDomainsRulesetPromise = createMemoizedPromise(a const lans: string[] = []; Object.entries(DOMESTICS).forEach(([, { domains }]) => { - domestics.push(...domains.map((domain) => `DOMAIN-SUFFIX,${domain}`)); + appendArrayInPlace(domestics, domains.map((domain) => `DOMAIN-SUFFIX,${domain}`)); }); Object.entries(DIRECTS).forEach(([, { domains }]) => { - directs.push(...domains.map((domain) => `DOMAIN-SUFFIX,${domain}`)); + appendArrayInPlace(directs, domains.map((domain) => `DOMAIN-SUFFIX,${domain}`)); }); Object.entries(LANS).forEach(([, { domains }]) => { - lans.push(...domains.map((domain) => `DOMAIN-SUFFIX,${domain}`)); + appendArrayInPlace(lans, domains.map((domain) => `DOMAIN-SUFFIX,${domain}`)); }); return [domestics, directs, lans] as const; @@ -30,7 +31,9 @@ export const getDomesticAndDirectDomainsRulesetPromise = createMemoizedPromise(a export const buildDomesticRuleset = task(import.meta.main, import.meta.path)(async (span) => { const res = await getDomesticAndDirectDomainsRulesetPromise(); - const dataset = [...Object.entries(DOMESTICS), ...Object.entries(DIRECTS), ...Object.entries(LANS)]; + const dataset = Object.entries(DOMESTICS); + appendArrayInPlace(dataset, Object.entries(DIRECTS)); + appendArrayInPlace(dataset, Object.entries(LANS)); return Promise.all([ createRuleset( diff --git a/Build/build-mitm-hostname.ts b/Build/build-mitm-hostname.ts index db751c53..d777554e 100644 --- a/Build/build-mitm-hostname.ts +++ b/Build/build-mitm-hostname.ts @@ -61,9 +61,9 @@ const PRESET_MITM_HOSTNAMES = [ .replaceAll('^https://', '') .replaceAll('^http://', '') .split('/')[0] - .replaceAll('\\.', '.') + .replaceAll(String.raw`\.`, '.') .replaceAll('.+', '*') - .replaceAll('\\d', '*') + .replaceAll(String.raw`\d`, '*') .replaceAll('([a-z])', '*') .replaceAll('[a-z]', '*') .replaceAll('([0-9])', '*') @@ -104,7 +104,7 @@ const PRESET_MITM_HOSTNAMES = [ return new RegExp( escapeRegExp(i) .replaceAll('{www or not}', '(www.)?') - .replaceAll('\\*', '(.*)') + .replaceAll(String.raw`\*`, '(.*)') ); }); @@ -135,7 +135,7 @@ const PRESET_MITM_HOSTNAMES = [ })); console.log('--------------------'); console.log('Parsed Failed'); - console.log([...parsedFailures].join('\n')); + console.log(Array.from(parsedFailures).join('\n')); })(); /** Util function */ @@ -159,6 +159,6 @@ function escapeRegExp(string = '') { const reHasRegExpChar = new RegExp(reRegExpChar.source); return string && reHasRegExpChar.test(string) - ? string.replaceAll(reRegExpChar, '\\$&') + ? string.replaceAll(reRegExpChar, String.raw`\$&`) : string; } diff --git a/Build/build-reject-domainset.ts b/Build/build-reject-domainset.ts index 929e5856..1b618502 100644 --- a/Build/build-reject-domainset.ts +++ b/Build/build-reject-domainset.ts @@ -55,11 +55,10 @@ export const buildRejectDomainSet = task(import.meta.main, import.meta.path)(asy 'https://raw.githubusercontent.com/AdguardTeam/AdGuardSDNSFilter/master/Filters/exceptions.txt', 'https://raw.githubusercontent.com/AdguardTeam/AdGuardSDNSFilter/master/Filters/exclusions.txt' ].map( - input => processFilterRules(childSpan, input) - .then(({ white, black }) => { - setAddFromArray(filterRuleWhitelistDomainSets, white); - setAddFromArray(filterRuleWhitelistDomainSets, black); - }) + input => processFilterRules(childSpan, input).then(({ white, black }) => { + setAddFromArray(filterRuleWhitelistDomainSets, white); + setAddFromArray(filterRuleWhitelistDomainSets, black); + }) )), getPhishingDomains(childSpan).then(appendArrayToDomainSets), getRejectSukkaConfPromise.then(appendArrayToDomainSets) diff --git a/Build/build-sgmodule-redirect.ts b/Build/build-sgmodule-redirect.ts index c60c3ec4..c4fb7924 100644 --- a/Build/build-sgmodule-redirect.ts +++ b/Build/build-sgmodule-redirect.ts @@ -8,7 +8,7 @@ function escapeRegExp(string = '') { const reHasRegExpChar = new RegExp(reRegExpChar.source); return string && reHasRegExpChar.test(string) - ? string.replaceAll(reRegExpChar, '\\$&') + ? string.replaceAll(reRegExpChar, String.raw`\$&`) : string; } @@ -119,14 +119,10 @@ const REDIRECT_FAKEWEBSITES = [ ] as const; export const buildRedirectModule = task(import.meta.main, import.meta.path)(async (span) => { - const domains = Array.from( - new Set( - [ - ...REDIRECT_MIRROR.map(([from]) => getHostname(from, { detectIp: false })), - ...REDIRECT_FAKEWEBSITES.flatMap(([from]) => [from, `www.${from}`]) - ] - ) - ).filter(Boolean); + const domains = Array.from(new Set([ + ...REDIRECT_MIRROR.map(([from]) => getHostname(from, { detectIp: false })), + ...REDIRECT_FAKEWEBSITES.flatMap(([from]) => [from, `www.${from}`]) + ])).filter(Boolean); return compareAndWriteFile( span, diff --git a/Build/lib/constants.ts b/Build/lib/constants.ts index 503c74e1..e3adad18 100644 --- a/Build/lib/constants.ts +++ b/Build/lib/constants.ts @@ -2,4 +2,4 @@ export const SHARED_DESCRIPTION = [ 'License: AGPL 3.0', 'Homepage: https://ruleset.skk.moe', 'GitHub: https://github.com/SukkaW/Surge' -] as const; +]; diff --git a/Build/lib/create-file.ts b/Build/lib/create-file.ts index f031a65a..0b8faa99 100644 --- a/Build/lib/create-file.ts +++ b/Build/lib/create-file.ts @@ -26,7 +26,7 @@ export async function compareAndWriteFile(span: Span, linesA: string[], filePath let index = 0; for await (const lineB of readFileByLine(file)) { - const lineA = linesA[index]; + const lineA = linesA[index] as string | undefined; index++; if (lineA == null) { diff --git a/Build/lib/text-decoder-stream.ts b/Build/lib/text-decoder-stream.ts index 251c135e..bc0e8257 100644 --- a/Build/lib/text-decoder-stream.ts +++ b/Build/lib/text-decoder-stream.ts @@ -28,7 +28,7 @@ export class PolyfillTextDecoderStream extends TransformStream) {