recommendJobs function
JobsRecommendation
recommendJobs({})
Computes a safe recommended worker count.
The result is the minimum of: the CPU count (minus one on interactive
machines so the desktop stays responsive; all cores in isCi), what
kMemoryUseFraction of availableMemoryMb can hold at the per-worker
budget, a minimum files-per-worker ratio, and a ceiling. Always at least
- When memory is unknown the memory term is skipped but the ceiling still applies.
Implementation
JobsRecommendation recommendJobs({
required int fileCount,
required int cores,
int? availableMemoryMb,
bool isCi = false,
bool resolved = false,
}) {
final perWorker = resolved
? kResolvedWorkerMemoryBudgetMb
: kWorkerMemoryBudgetMb;
final candidates = <(String, int)>[
('CPU', isCi ? cores : cores - 1),
(
'file count',
fileCount ~/ (resolved ? kMinResolvedFilesPerWorker : kMinFilesPerWorker),
),
('ceiling', resolved ? kMaxRecommendedResolvedJobs : kMaxRecommendedJobs),
];
if (availableMemoryMb != null) {
final usable = availableMemoryMb * kMemoryUseFraction - kParentMemoryBaseMb;
candidates.add(('memory', (usable / perWorker).floor()));
}
var best = candidates.first;
for (final c in candidates) {
if (c.$2 < best.$2) best = c;
}
return JobsRecommendation(
jobs: best.$2 < 1 ? 1 : best.$2,
cores: cores,
availableMemoryMb: availableMemoryMb,
limitedBy: best.$1,
);
}