Skip to content

Commit

Permalink
chore: use os.availableParallelism when available (#5183)
Browse files Browse the repository at this point in the history
* chore: use `os.availableParallelism` when available

* refactor: move to nodeUtils
  • Loading branch information
merceyz committed Jan 8, 2023
1 parent a215aac commit 29d91b6
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 7 deletions.
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 * as t from 'typanion';
Expand Down Expand Up @@ -196,7 +195,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 @@ -92,3 +93,12 @@ export function getArchitectureSet() {
libc: architecture.libc ? [architecture.libc] : [],
};
}

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);
}

0 comments on commit 29d91b6

Please sign in to comment.