Browse Source

Perf: improve build speed

SukkaW 1 year ago
parent
commit
e347587e6c
3 changed files with 16 additions and 14 deletions
  1. 4 4
      Build/build-cdn-download-conf.ts
  2. 6 8
      Build/build-speedtest-domainset.ts
  3. 6 2
      Build/lib/rules/base.ts

+ 4 - 4
Build/build-cdn-download-conf.ts

@@ -29,7 +29,7 @@ const getS3OSSDomainsPromise = (async (): Promise<string[]> => {
       (line.startsWith('s3-') || line.startsWith('s3.'))
       && !line.includes('cn-')
     ) {
-      S3OSSDomains.add(line);
+      S3OSSDomains.add('.' + line);
     }
   });
   trie.find('.scw.cloud').forEach((line: string) => {
@@ -37,14 +37,14 @@ const getS3OSSDomainsPromise = (async (): Promise<string[]> => {
       (line.startsWith('s3-') || line.startsWith('s3.'))
       && !line.includes('cn-')
     ) {
-      S3OSSDomains.add(line);
+      S3OSSDomains.add('.' + line);
     }
   });
   trie.find('sakurastorage.jp').forEach((line: string) => {
     if (
       (line.startsWith('s3-') || line.startsWith('s3.'))
     ) {
-      S3OSSDomains.add(line);
+      S3OSSDomains.add('.' + line);
     }
   });
 
@@ -66,7 +66,7 @@ export const buildCdnDownloadConf = task(require.main === module, __filename)(as
   ]);
 
   // Move S3 domains to download domain set, since S3 files may be large
-  appendArrayInPlace(downloadDomainSet, S3OSSDomains.map(domain => `.${domain}`));
+  appendArrayInPlace(downloadDomainSet, S3OSSDomains);
   appendArrayInPlace(downloadDomainSet, steamDomainSet);
 
   // we have whitelisted the crashlytics domain, and we also want to put it in CDN policy

+ 6 - 8
Build/build-speedtest-domainset.ts

@@ -9,6 +9,7 @@ import { readFileIntoProcessedArray } from './lib/fetch-text-by-line';
 
 import { DomainsetOutput } from './lib/create-file';
 import { OUTPUT_SURGE_DIR } from './constants/dir';
+import { createMemoizedPromise } from './lib/memo-promise';
 
 const KEYWORDS = [
   'Hong Kong',
@@ -183,6 +184,8 @@ async function querySpeedtestApi(keyword: string): Promise<Array<string | null>>
   }
 }
 
+const getSpeedtestHostsGroupsPromise = createMemoizedPromise(() => Promise.all(KEYWORDS.flatMap(querySpeedtestApi)));
+
 export const buildSpeedtestDomainSet = task(require.main === module, __filename)(async (span) => {
   const output = new DomainsetOutput(span, 'speedtest')
     .withTitle('Sukka\'s Ruleset - Speedtest Domains')
@@ -194,14 +197,9 @@ export const buildSpeedtestDomainSet = task(require.main === module, __filename)
     .addFromDomainset(PREDEFINE_DOMAINS)
     .addFromDomainset(await readFileIntoProcessedArray(path.resolve(OUTPUT_SURGE_DIR, 'domainset/speedtest.conf')));
 
-  await Promise.all(KEYWORDS.map((keyword) => span.traceChildAsync(
-    `fetch speedtest endpoints: ${keyword}`,
-    () => querySpeedtestApi(keyword)
-  ).then(hostnameGroup => hostnameGroup.forEach(hostname => {
-    if (hostname) {
-      output.addDomain(hostname);
-    }
-  }))));
+  const hostnameGroup = await span.traceChildPromise('get speedtest hosts groups', getSpeedtestHostsGroupsPromise());
+
+  hostnameGroup.forEach(hostname => output.bulkAddDomain(hostname));
 
   return output.write();
 });

+ 6 - 2
Build/lib/rules/base.ts

@@ -90,9 +90,13 @@ export abstract class RuleOutput<TPreprocessed = unknown> {
     return this;
   }
 
-  bulkAddDomain(domains: string[]) {
+  bulkAddDomain(domains: Array<string | null>) {
+    let d: string | null;
     for (let i = 0, len = domains.length; i < len; i++) {
-      this.addDomain(domains[i]);
+      d = domains[i];
+      if (d !== null) {
+        this.addDomain(d);
+      }
     }
     return this;
   }