Skip to content

Commit f5c780f

Browse files
authored
rescript format: process files in batches (#7081)
* rescript format: process files in batches * CHANGELOG
1 parent 597e453 commit f5c780f

File tree

2 files changed

+35
-8
lines changed

2 files changed

+35
-8
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929

3030
- Fix tuple coercion. https://github.com/rescript-lang/rescript-compiler/pull/7024
3131
- Fix attribute printing. https://github.com/rescript-lang/rescript-compiler/pull/7025
32+
- Fix "rescript format" with many files. https://github.com/rescript-lang/rescript-compiler/pull/7081
3233

3334
#### :nail_care: Polish
3435

cli/rescript_format.js

Lines changed: 34 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
//@ts-check
2+
var os = require("os");
23
var arg = require("./rescript_arg.js");
4+
35
var format_usage = `Usage: rescript format <options> [files]
46
57
\`rescript format\` formats the current directory
@@ -67,18 +69,43 @@ async function readStdin() {
6769
return Buffer.concat(chunks).toString("utf8");
6870
}
6971

72+
const numThreads = os.cpus().length;
73+
74+
/**
75+
* Splits an array into smaller chunks of a specified size.
76+
*
77+
* @template T
78+
* @param {T[]} array - The array to split into chunks.
79+
* @param {number} chunkSize - The size of each chunk.
80+
* @returns {T[][]} - An array of chunks, where each chunk is an array of type T.
81+
*/
82+
function chunkArray(array, chunkSize) {
83+
/** @type {T[][]} */
84+
const result = [];
85+
86+
for (let i = 0; i < array.length; i += chunkSize) {
87+
result.push(array.slice(i, i + chunkSize));
88+
}
89+
90+
return result;
91+
}
92+
7093
/**
7194
* @param {string[]} files
7295
* @param {string} bsc_exe
7396
* @param {(x: string) => boolean} isSupportedFile
7497
* @param {boolean} checkFormatting
7598
*/
7699
async function formatFiles(files, bsc_exe, isSupportedFile, checkFormatting) {
77-
var incorrectlyFormattedFiles = 0;
100+
const supportedFiles = files.filter(isSupportedFile);
101+
const batchSize = 4 * os.cpus().length;
102+
const batches = chunkArray(supportedFiles, batchSize);
103+
104+
let incorrectlyFormattedFiles = 0;
78105
try {
79-
const _promises = await Promise.all(
80-
files.map(async file => {
81-
if (isSupportedFile(file)) {
106+
for (const batch of batches) {
107+
await Promise.all(
108+
batch.map(async file => {
82109
const flags = checkFormatting
83110
? ["-format", file]
84111
: ["-o", file, "-format", file];
@@ -90,10 +117,9 @@ async function formatFiles(files, bsc_exe, isSupportedFile, checkFormatting) {
90117
incorrectlyFormattedFiles++;
91118
}
92119
}
93-
}
94-
return null;
95-
}),
96-
);
120+
}),
121+
);
122+
}
97123
} catch (err) {
98124
console.error(err);
99125
process.exit(2);

0 commit comments

Comments
 (0)