Skip to content

Commit 5ae946d

Browse files
screetBloomfacebook-github-bot
authored andcommitted
feat: optimize getMaxWorkers using os.availableParallelism (#1378)
Summary: Optimize the maximum number of worker parallelism using os.availableParallelism `os.cpus().length` returns the number of CPUs in the physical device, which is static. It should be replaced with the maximum number of computations the system can execute simultaneously. In Node.js, this is provided by `os.availableParallelism()`. Currently, Metro requires Node.js version 18.18 or higher, which includes `os.availableParallelism`. Changelog: [Internal] Pull Request resolved: #1378 Test Plan: Green CI Reviewed By: vzaidman Differential Revision: D65277683 Pulled By: robhogan fbshipit-source-id: 69c22a28626a89588082ca309dda6ebc9d08413a
1 parent f2a130c commit 5ae946d

File tree

3 files changed

+6
-5
lines changed

3 files changed

+6
-5
lines changed

docs/Configuration.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ If `true`, Metro will use a stable mapping from files to transformer workers, so
143143

144144
Type: `number`
145145

146-
The number of workers to use for parallel processing in Metro. Defaults to approximately half of the number of cores available on the machine, as reported by [`os.cpus()`](https://nodejs.org/api/os.html#oscpus).
146+
The number of workers to use for parallel processing in Metro. Defaults to approximately half of the number of cores available on the machine, as reported by [`os.availableParallelism()`](https://nodejs.org/api/os.html#osavailableparallelism).
147147

148148
:::note
149149
1. Values exceeding the number of available cores have no effect.

packages/metro/src/lib/__tests__/getMaxWorkers-test.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -20,17 +20,17 @@ test('calculates the number of max workers', () => {
2020
/* $FlowFixMe(>=0.99.0 site=react_native_fb) This comment suppresses an error
2121
* found when Flow v0.99 was deployed. To see the error, delete this comment
2222
* and run Flow. */
23-
os.cpus.mockReturnValue({length: 1});
23+
os.availableParallelism.mockReturnValue(1);
2424
expect(getMaxWorkers()).toBe(1);
2525
/* $FlowFixMe(>=0.99.0 site=react_native_fb) This comment suppresses an error
2626
* found when Flow v0.99 was deployed. To see the error, delete this comment
2727
* and run Flow. */
28-
os.cpus.mockReturnValue({length: 8});
28+
os.availableParallelism.mockReturnValue(8);
2929
expect(getMaxWorkers()).toBe(6);
3030
/* $FlowFixMe(>=0.99.0 site=react_native_fb) This comment suppresses an error
3131
* found when Flow v0.99 was deployed. To see the error, delete this comment
3232
* and run Flow. */
33-
os.cpus.mockReturnValue({length: 24});
33+
os.availableParallelism.mockReturnValue(24);
3434
expect(getMaxWorkers()).toBe(14);
3535
expect(getMaxWorkers(5)).toBe(5);
3636
});

packages/metro/src/lib/getMaxWorkers.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@
1414
const os = require('os');
1515

1616
module.exports = (workers: ?number): number => {
17-
const cores = os.cpus().length;
17+
// $FlowFixMe[prop-missing] Missing Flow lib def for availableParallelism
18+
const cores = os.availableParallelism();
1819
return typeof workers === 'number' && Number.isInteger(workers)
1920
? Math.min(cores, workers > 0 ? workers : 1)
2021
: Math.max(1, Math.ceil(cores * (0.5 + 0.5 * Math.exp(-cores * 0.07)) - 1));

0 commit comments

Comments
 (0)