Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

rescript format: process files in batches #7081

Merged
merged 2 commits into from
Oct 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@

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

#### :nail_care: Polish

Expand Down
42 changes: 34 additions & 8 deletions cli/rescript_format.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
//@ts-check
var os = require("os");
var arg = require("./rescript_arg.js");

var format_usage = `Usage: rescript format <options> [files]

\`rescript format\` formats the current directory
Expand Down Expand Up @@ -67,18 +69,43 @@ async function readStdin() {
return Buffer.concat(chunks).toString("utf8");
}

const numThreads = os.cpus().length;

/**
* Splits an array into smaller chunks of a specified size.
*
* @template T
* @param {T[]} array - The array to split into chunks.
* @param {number} chunkSize - The size of each chunk.
* @returns {T[][]} - An array of chunks, where each chunk is an array of type T.
*/
function chunkArray(array, chunkSize) {
/** @type {T[][]} */
const result = [];

for (let i = 0; i < array.length; i += chunkSize) {
result.push(array.slice(i, i + chunkSize));
}

return result;
}

/**
* @param {string[]} files
* @param {string} bsc_exe
* @param {(x: string) => boolean} isSupportedFile
* @param {boolean} checkFormatting
*/
async function formatFiles(files, bsc_exe, isSupportedFile, checkFormatting) {
var incorrectlyFormattedFiles = 0;
const supportedFiles = files.filter(isSupportedFile);
const batchSize = 4 * os.cpus().length;
const batches = chunkArray(supportedFiles, batchSize);

let incorrectlyFormattedFiles = 0;
try {
const _promises = await Promise.all(
files.map(async file => {
if (isSupportedFile(file)) {
for (const batch of batches) {
await Promise.all(
batch.map(async file => {
const flags = checkFormatting
? ["-format", file]
: ["-o", file, "-format", file];
Expand All @@ -90,10 +117,9 @@ async function formatFiles(files, bsc_exe, isSupportedFile, checkFormatting) {
incorrectlyFormattedFiles++;
}
}
}
return null;
}),
);
}),
);
}
} catch (err) {
console.error(err);
process.exit(2);
Expand Down