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

chore: use os.availableParallelism when available #5183

Merged
merged 2 commits into from
Jan 8, 2023
Merged
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
34 changes: 34 additions & 0 deletions .yarn/versions/cad178bf.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
releases:
"@yarnpkg/cli": patch
"@yarnpkg/core": patch
"@yarnpkg/plugin-workspace-tools": patch

declined:
- "@yarnpkg/plugin-compat"
- "@yarnpkg/plugin-constraints"
- "@yarnpkg/plugin-dlx"
- "@yarnpkg/plugin-essentials"
- "@yarnpkg/plugin-exec"
- "@yarnpkg/plugin-file"
- "@yarnpkg/plugin-git"
- "@yarnpkg/plugin-github"
- "@yarnpkg/plugin-http"
- "@yarnpkg/plugin-init"
- "@yarnpkg/plugin-interactive-tools"
- "@yarnpkg/plugin-link"
- "@yarnpkg/plugin-nm"
- "@yarnpkg/plugin-npm"
- "@yarnpkg/plugin-npm-cli"
- "@yarnpkg/plugin-pack"
- "@yarnpkg/plugin-patch"
- "@yarnpkg/plugin-pnp"
- "@yarnpkg/plugin-pnpm"
- "@yarnpkg/plugin-stage"
- "@yarnpkg/plugin-typescript"
- "@yarnpkg/plugin-version"
- "@yarnpkg/builder"
- "@yarnpkg/doctor"
- "@yarnpkg/extensions"
- "@yarnpkg/nm"
- "@yarnpkg/pnpify"
- "@yarnpkg/sdks"
5 changes: 2 additions & 3 deletions packages/plugin-workspace-tools/sources/commands/foreach.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import {BaseCommand, WorkspaceRequiredError} from '@yarnpkg/cli';
import {Configuration, LocatorHash, Project, scriptUtils, Workspace} from '@yarnpkg/core';
import {DescriptorHash, MessageName, Report, StreamReport} from '@yarnpkg/core';
import {formatUtils, miscUtils, structUtils} from '@yarnpkg/core';
import {formatUtils, miscUtils, structUtils, nodeUtils} from '@yarnpkg/core';
import {gitUtils} from '@yarnpkg/plugin-git';
import {Command, Option, Usage, UsageError} from 'clipanion';
import micromatch from 'micromatch';
import {cpus} from 'os';
import pLimit from 'p-limit';
import {Writable} from 'stream';
import {WriteStream} from 'tty';
Expand Down Expand Up @@ -200,7 +199,7 @@ export default class WorkspacesForeachCommand extends BaseCommand {
const concurrency = this.parallel ?
(this.jobs === `unlimited`
? Infinity
: Number(this.jobs) || Math.max(1, cpus().length / 2))
: Number(this.jobs) || Math.ceil(nodeUtils.availableParallelism() / 2))
: 1;

// No need to parallelize if we were explicitly asked for one job
Expand Down
9 changes: 5 additions & 4 deletions packages/yarnpkg-core/sources/WorkerPool.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import {cpus} from 'os';
import PLimit from 'p-limit';
import {Worker} from 'worker_threads';
import PLimit from 'p-limit';
import {Worker} from 'worker_threads';

import * as nodeUtils from './nodeUtils';

const kTaskInfo = Symbol(`kTaskInfo`);

Expand All @@ -10,7 +11,7 @@ type PoolWorker<TOut> = Worker & {

export class WorkerPool<TIn, TOut> {
private workers: Array<PoolWorker<TOut>> = [];
private limit = PLimit(Math.max(1, cpus().length));
private limit = PLimit(nodeUtils.availableParallelism());
private cleanupInterval: ReturnType<typeof setInterval>;

constructor(private source: string) {
Expand Down
10 changes: 10 additions & 0 deletions packages/yarnpkg-core/sources/nodeUtils.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import {ppath} from '@yarnpkg/fslib';
import Module from 'module';
import os from 'os';

import * as execUtils from './execUtils';
import * as miscUtils from './miscUtils';
Expand Down Expand Up @@ -136,3 +137,12 @@ export function getCaller() {

return parseStackLine(line);
}

export function availableParallelism() {
// TODO: Use os.availableParallelism directly when dropping support for Node.js < 19.4.0
if (`availableParallelism` in os)
// @ts-expect-error - No types yet
return os.availableParallelism();

return Math.max(1, os.cpus().length);
}