Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: optimize getMaxWorkers using os.availableParallelism #1378

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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();
robhogan marked this conversation as resolved.
Show resolved Hide resolved
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
Loading