Skip to content

Commit 9c54343

Browse files
authored
feat: add --max-workers option & limit Rspack workers by default (#1257)
1 parent d41d7a3 commit 9c54343

File tree

11 files changed

+71
-3
lines changed

11 files changed

+71
-3
lines changed

.changeset/heavy-toes-lose.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@callstack/repack": minor
3+
---
4+
5+
Add `--max-workers` option to start & bundle commands. By default the number of workers is derived from available cores. This option is only effective for Rspack configurations
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import os from 'node:os';
2+
3+
export function getMaxWorkers(): number {
4+
const cores = os.availableParallelism();
5+
6+
const decayCoefficient = 0.07;
7+
const decay = Math.exp(-cores * decayCoefficient);
8+
9+
// Scale transitions from 1.0 (low cores) toward 0.5 (many cores)
10+
const scale = 0.5 + 0.5 * decay;
11+
12+
// Adjust down slightly to leave headroom for main thread and I/O
13+
const adjusted = cores * scale - 1;
14+
15+
return Math.max(1, Math.ceil(adjusted));
16+
}

packages/repack/src/commands/common/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
export * from './getDevMiddleware.js';
2+
export * from './getMaxWorkers.js';
23
export * from './getMimeType.js';
34
export * from './parseUrl.js';
45
export * from './resetPersistentCache.js';

packages/repack/src/commands/common/setupEnvironment.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import {
22
ASSETS_DEST_ENV_KEY,
33
BUNDLE_FILENAME_ENV_KEY,
4+
RSPACK_RAYON_THREADS_ENV_KEY,
5+
RSPACK_TOKIO_THREADS_ENV_KEY,
46
SOURCEMAP_FILENAME_ENV_KEY,
57
VERBOSE_ENV_KEY,
68
} from '../../env.js';
@@ -24,3 +26,8 @@ export function setupEnvironment(args: EnvironmentArgs): void {
2426
setEnvVar(SOURCEMAP_FILENAME_ENV_KEY, args.sourcemapOutput);
2527
setEnvVar(ASSETS_DEST_ENV_KEY, args.assetsDest);
2628
}
29+
30+
export function setupRspackEnvironment(maxWorkers: string): void {
31+
setEnvVar(RSPACK_TOKIO_THREADS_ENV_KEY, maxWorkers);
32+
setEnvVar(RSPACK_RAYON_THREADS_ENV_KEY, maxWorkers);
33+
}

packages/repack/src/commands/options.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,12 @@ export const startCommandOptions = [
5959
name: '--verbose',
6060
description: 'Enables verbose logging',
6161
},
62+
{
63+
name: '--max-workers <number>',
64+
description:
65+
'(Rspack only) The maximum number of workers to use for transformation parallelization ',
66+
parse: (val: string) => Number(val),
67+
},
6268
{
6369
name: '--config <path>',
6470
description: 'Path to a bundler config file, e.g webpack.config.js',
@@ -149,6 +155,12 @@ export const bundleCommandOptions = [
149155
name: '--watch',
150156
description: 'Watch for file changes',
151157
},
158+
{
159+
name: '--max-workers <number>',
160+
description:
161+
'(Rspack only) The maximum number of workers to use for transformation parallelization ',
162+
parse: (val: string) => Number(val),
163+
},
152164
{
153165
name: '--config <path>',
154166
description: 'Path to a bundler config file, e.g webpack.config.js',

packages/repack/src/commands/rspack/bundle.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,13 @@ import type { Stats } from '@rspack/core';
33
import { CLIError } from '../../helpers/index.js';
44
import { makeCompilerConfig } from '../common/config/makeCompilerConfig.js';
55
import {
6+
getMaxWorkers,
67
normalizeStatsOptions,
78
resetPersistentCache,
9+
setupEnvironment,
10+
setupRspackEnvironment,
811
writeStats,
912
} from '../common/index.js';
10-
import { setupEnvironment } from '../common/setupEnvironment.js';
1113
import type { BundleArguments, CliConfig } from '../types.js';
1214

1315
/**
@@ -35,6 +37,9 @@ export async function bundle(
3537
// expose selected args as environment variables
3638
setupEnvironment(args);
3739

40+
const maxWorkers = args.maxWorkers ?? getMaxWorkers();
41+
setupRspackEnvironment(maxWorkers.toString());
42+
3843
if (!args.entryFile && !config.entry) {
3944
throw new CLIError("Option '--entry-file <path>' argument is missing");
4045
}

packages/repack/src/commands/rspack/start.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,17 @@ import {
1212
import { makeCompilerConfig } from '../common/config/makeCompilerConfig.js';
1313
import {
1414
getDevMiddleware,
15+
getMaxWorkers,
1516
getMimeType,
1617
parseUrl,
1718
resetPersistentCache,
1819
resolveProjectPath,
20+
runAdbReverse,
21+
setupEnvironment,
1922
setupInteractions,
23+
setupRspackEnvironment,
2024
} from '../common/index.js';
21-
import { runAdbReverse } from '../common/index.js';
2225
import logo from '../common/logo.js';
23-
import { setupEnvironment } from '../common/setupEnvironment.js';
2426
import type { CliConfig, StartArguments } from '../types.js';
2527
import { Compiler } from './Compiler.js';
2628

@@ -58,6 +60,9 @@ export async function start(
5860
// expose selected args as environment variables
5961
setupEnvironment(args);
6062

63+
const maxWorkers = args.maxWorkers ?? getMaxWorkers();
64+
setupRspackEnvironment(maxWorkers.toString());
65+
6166
const isVerbose = isTruthyEnv(process.env[VERBOSE_ENV_KEY]);
6267
const devServerOptions = configs[0].devServer ?? {};
6368
const showHttpRequests = isVerbose || args.logRequests;

packages/repack/src/commands/types.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ export interface BundleArguments {
1313
resetCache?: boolean;
1414
verbose?: boolean;
1515
watch?: boolean;
16+
maxWorkers?: number;
1617
config?: string;
1718
webpackConfig?: string;
1819
}
@@ -31,6 +32,7 @@ export interface StartArguments {
3132
resetCache?: boolean;
3233
reversePort?: boolean;
3334
verbose?: boolean;
35+
maxWorkers?: number;
3436
config?: string;
3537
webpackConfig?: string;
3638
}

packages/repack/src/env.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,7 @@ export const BUNDLE_FILENAME_ENV_KEY = 'REPACK_BUNDLE_FILENAME';
55
export const SOURCEMAP_FILENAME_ENV_KEY = 'REPACK_SOURCEMAP_FILENAME';
66
export const ASSETS_DEST_ENV_KEY = 'REPACK_ASSETS_DEST';
77

8+
export const RSPACK_TOKIO_THREADS_ENV_KEY = 'TOKIO_WORKER_THREADS';
9+
export const RSPACK_RAYON_THREADS_ENV_KEY = 'RAYON_NUM_THREADS';
10+
811
export const EXPERIMENTAL_CACHE_ENV_KEY = 'REPACK_EXPERIMENTAL_CACHE';

website/src/latest/api/cli/bundle.mdx

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,12 @@ Enables verbose logging.
120120

121121
Watch for file changes.
122122

123+
### `--max-workers`
124+
125+
- Type: `number`
126+
127+
(Rspack only) The maximum number of workers to use for transformation parallelization. By default, the number of workers is derived from available CPU cores.
128+
123129
### `--config`
124130

125131
- Type: `string`

0 commit comments

Comments
 (0)