Skip to content

Commit

Permalink
feat: optimize getMaxWorkers using os.availableParallelism (#1378)
Browse files Browse the repository at this point in the history
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
  • Loading branch information
screetBloom authored and facebook-github-bot committed Nov 1, 2024
1 parent f2a130c commit 5ae946d
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 5 deletions.
2 changes: 1 addition & 1 deletion docs/Configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ If `true`, Metro will use a stable mapping from files to transformer workers, so

Type: `number`

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).
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).

:::note
1. Values exceeding the number of available cores have no effect.
Expand Down
6 changes: 3 additions & 3 deletions packages/metro/src/lib/__tests__/getMaxWorkers-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,17 @@ test('calculates the number of max workers', () => {
/* $FlowFixMe(>=0.99.0 site=react_native_fb) This comment suppresses an error
* found when Flow v0.99 was deployed. To see the error, delete this comment
* and run Flow. */
os.cpus.mockReturnValue({length: 1});
os.availableParallelism.mockReturnValue(1);
expect(getMaxWorkers()).toBe(1);
/* $FlowFixMe(>=0.99.0 site=react_native_fb) This comment suppresses an error
* found when Flow v0.99 was deployed. To see the error, delete this comment
* and run Flow. */
os.cpus.mockReturnValue({length: 8});
os.availableParallelism.mockReturnValue(8);
expect(getMaxWorkers()).toBe(6);
/* $FlowFixMe(>=0.99.0 site=react_native_fb) This comment suppresses an error
* found when Flow v0.99 was deployed. To see the error, delete this comment
* and run Flow. */
os.cpus.mockReturnValue({length: 24});
os.availableParallelism.mockReturnValue(24);
expect(getMaxWorkers()).toBe(14);
expect(getMaxWorkers(5)).toBe(5);
});
3 changes: 2 additions & 1 deletion packages/metro/src/lib/getMaxWorkers.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@
const os = require('os');

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

0 comments on commit 5ae946d

Please sign in to comment.