Skip to content

Commit 9199a04

Browse files
committed
rescript format: process files in batches (#7081)
1 parent 2ed005b commit 9199a04

File tree

2 files changed

+44
-11
lines changed

2 files changed

+44
-11
lines changed

CHANGELOG.md

+10-3
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,19 @@
1212
1313
# 11.1.5 (Unreleased)
1414

15-
- Handle absolute file paths in gentype https://github.com/rescript-lang/rescript-compiler/pull/7111
16-
- Deprecate JSX 3 https://github.com/rescript-lang/rescript-compiler/pull/7042
17-
- Deprecate js_cast.res https://github.com/rescript-lang/rescript-compiler/pull/7074
15+
#### :boom: Breaking Change
16+
17+
- Deprecate JSX 3. https://github.com/rescript-lang/rescript-compiler/pull/7042
18+
- Deprecate js_cast.res. https://github.com/rescript-lang/rescript-compiler/pull/7074
1819
- Deprecate top-level `"suffix"` option in `rescript.json`. https://github.com/rescript-lang/rescript-compiler/pull/7056
1920

21+
#### :bug: Bug Fix
22+
23+
- Handle absolute file paths in gentype. https://github.com/rescript-lang/rescript-compiler/pull/7111
24+
- Fix "rescript format" with many files. https://github.com/rescript-lang/rescript-compiler/pull/7081
25+
2026
#### :house: Internal
27+
2128
- Playground: Bundle and upload stdlib runtime so that the playground can execute functions from Core/Belt/Js. https://github.com/rescript-lang/rescript/pull/7268
2229

2330
# 11.1.4

scripts/rescript_format.js

+34-8
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)