Skip to content

Commit 2448cbe

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

6 files changed

+131
-11
lines changed

Build/build-reject-domainset.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ const buildRejectDomainSet = task(__dirname, async () => {
3333
let shouldStop = false;
3434

3535
const [gorhill] = await Promise.all([
36-
getGorhillPublicSuffixPromise,
36+
getGorhillPublicSuffixPromise(),
3737
// Parse from remote hosts & domain lists
3838
...HOSTS.map(entry => processHosts(entry[0], entry[1]).then(hosts => {
3939
hosts.forEach(host => {

Build/index.js

+43-2
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,54 @@ 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 & { __sukka_worker_name: string } & T} WithWorker
23+
*/
24+
25+
/**
26+
* @template T
27+
* @param {string} path
28+
* @returns {WithWorker<T>}
29+
*/
30+
const requireWorker = (path) => {
31+
const _worker = /** @type {WithWorker<T>} */ (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+
_worker.__sukka_worker_name = path;
42+
return _worker;
43+
};
44+
45+
/**
46+
* @template T
47+
* @param {WithWorker<T>} worker
48+
*/
49+
const endWorker = async (worker) => {
50+
const { forceExited } = worker.end();
51+
if (forceExited && worker.__sukka_worker_name) {
52+
console.log(worker.__sukka_worker_name, 'forceExited');
53+
}
54+
};
55+
1956
(async () => {
57+
const buildInternalReverseChnCIDRWorker = /** @type {WithWorker<import('./build-internal-reverse-chn-cidr')>} */ (requireWorker('./build-internal-reverse-chn-cidr'));
58+
const { buildInternalReverseChnCIDR } = buildInternalReverseChnCIDRWorker;
59+
2060
// download-previous-build
2161
const downloadPreviousBuildPromise = downloadPreviousBuild();
2262
const downloadPublicSuffixListPromise = downloadPublicSuffixList();
@@ -77,6 +117,7 @@ const { buildPublicHtml } = require('./build-public');
77117

78118
await Promise.all([
79119
buildPublicHtml(),
80-
validate()
120+
validate(),
121+
endWorker(buildInternalReverseChnCIDRWorker)
81122
]);
82123
})();

Build/lib/get-gorhill-publicsuffix.js

+6-2
Original file line numberDiff line numberDiff line change
@@ -30,5 +30,9 @@ const getGorhillPublicSuffix = async () => {
3030
return gorhill;
3131
};
3232

33-
const getGorhillPublicSuffixPromise = getGorhillPublicSuffix();
34-
module.exports.getGorhillPublicSuffixPromise = getGorhillPublicSuffixPromise;
33+
/** @type {Promise<import('gorhill-publicsuffixlist').default | null>} */
34+
let gorhillPublicSuffixPromise = null;
35+
module.exports.getGorhillPublicSuffixPromise = () => {
36+
gorhillPublicSuffixPromise ||= getGorhillPublicSuffix();
37+
return gorhillPublicSuffixPromise;
38+
};

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)