balancedCliShards function
Implementation
List<List<CliShardFile>> balancedCliShards(
List<CliShardFile> files,
int workerCount,
) {
return RunnerBenchmark.sync('worker', 'balancedShards', () {
if (workerCount <= 0) return const [];
final shards = List.generate(workerCount, (_) => <CliShardFile>[]);
final shardDurations = List.filled(workerCount, 0);
final sorted = [...files]..sort((a, b) {
final byDuration =
b.estimatedDurationMs.compareTo(a.estimatedDurationMs);
if (byDuration != 0) return byDuration;
final byPath = a.path.compareTo(b.path);
return byPath != 0 ? byPath : a.id.compareTo(b.id);
});
for (final file in sorted) {
var target = 0;
for (var i = 1; i < shardDurations.length; i++) {
if (shardDurations[i] < shardDurations[target]) target = i;
}
shards[target].add(file);
shardDurations[target] += file.estimatedDurationMs;
}
return shards;
});
}