Skip to content

Commit 1ac19c4

Browse files
committed
Perf: run cpu intensive task with worker
1 parent b42bd05 commit 1ac19c4

File tree

4 files changed

+113
-8
lines changed

4 files changed

+113
-8
lines changed

Build/index.js

+32-2
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,43 @@ const { buildTelegramCIDR } = require('./build-telegram-cidr');
99
const { buildChnCidr } = require('./build-chn-cidr');
1010
const { buildSpeedtestDomainSet } = require('./build-speedtest-domainset');
1111
const { buildInternalCDNDomains } = require('./build-internal-cdn-rules');
12-
const { buildInternalReverseChnCIDR } = require('./build-internal-reverse-chn-cidr');
1312
const { buildInternalChnDomains } = require('./build-internal-chn-domains');
1413
const { buildDomesticRuleset } = require('./build-domestic-ruleset');
1514
const { validate } = require('./validate-domainset');
1615

1716
const { buildPublicHtml } = require('./build-public');
1817

18+
const { Worker } = require('jest-worker');
19+
20+
/**
21+
* @template T
22+
* @typedef {import('jest-worker').Worker & T} WithWorker
23+
*/
24+
25+
/**
26+
* @template T
27+
* @param {string} path
28+
* @returns {WithWorker<T>}
29+
*/
30+
const requireWorker = (path) => {
31+
const _worker = (new Worker(
32+
require.resolve(path),
33+
{
34+
numWorkers: 1,
35+
maxRetries: 0,
36+
enableWorkerThreads: true
37+
}
38+
));
39+
_worker.getStderr().pipe(process.stderr);
40+
_worker.getStdout().pipe(process.stdout);
41+
42+
return _worker;
43+
};
44+
1945
(async () => {
46+
const buildInternalReverseChnCIDRWorker = /** @type {WithWorker<import('./build-internal-reverse-chn-cidr')>} */ (requireWorker('./build-internal-reverse-chn-cidr'));
47+
const { buildInternalReverseChnCIDR } = buildInternalReverseChnCIDRWorker;
48+
2049
// download-previous-build
2150
const downloadPreviousBuildPromise = downloadPreviousBuild();
2251
const downloadPublicSuffixListPromise = downloadPublicSuffixList();
@@ -77,6 +106,7 @@ const { buildPublicHtml } = require('./build-public');
77106

78107
await Promise.all([
79108
buildPublicHtml(),
80-
validate()
109+
validate(),
110+
buildInternalReverseChnCIDRWorker.end()
81111
]);
82112
})();

Build/lib/is-domain-loose.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,10 @@ module.exports.isDomainLoose = (domain) => {
1010
};
1111

1212
/**
13-
* @param {string} domain
13+
* @param {string | null | undefined} domain
1414
*/
1515
module.exports.normalizeDomain = (domain) => {
16-
if (domain == null) {
16+
if (!domain) {
1717
return null;
1818
}
1919

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
"cidr-tools-wasm": "^0.0.11",
2222
"fs-extra": "^11.1.1",
2323
"gorhill-publicsuffixlist": "github:gorhill/publicsuffixlist.js",
24+
"jest-worker": "^29.7.0",
2425
"mnemonist": "^0.39.5",
2526
"path-scurry": "^1.10.1",
2627
"picocolors": "^1.0.0",

pnpm-lock.yaml

+78-4
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)