From 4bbdc4bce84a549ad6828c26d43c4aa29235e5cd Mon Sep 17 00:00:00 2001 From: Christoph Knittel Date: Sat, 5 Oct 2024 19:31:51 +0200 Subject: [PATCH 1/2] rescript format: process files in batches --- cli/rescript_format.js | 42 ++++++++++++++++++++++++++++++++++-------- 1 file changed, 34 insertions(+), 8 deletions(-) diff --git a/cli/rescript_format.js b/cli/rescript_format.js index 51037f2580..0873b95bbb 100644 --- a/cli/rescript_format.js +++ b/cli/rescript_format.js @@ -1,5 +1,7 @@ //@ts-check +var os = require("os"); var arg = require("./rescript_arg.js"); + var format_usage = `Usage: rescript format [files] \`rescript format\` formats the current directory @@ -67,6 +69,27 @@ 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 @@ -74,11 +97,15 @@ async function readStdin() { * @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]; @@ -90,10 +117,9 @@ async function formatFiles(files, bsc_exe, isSupportedFile, checkFormatting) { incorrectlyFormattedFiles++; } } - } - return null; - }), - ); + }), + ); + } } catch (err) { console.error(err); process.exit(2); From 3d321b0b0b5d9e0c2a7e6a94971e6df48624c5bc Mon Sep 17 00:00:00 2001 From: Christoph Knittel Date: Sat, 5 Oct 2024 20:51:00 +0200 Subject: [PATCH 2/2] CHANGELOG --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index b144bb80c8..a8e756cbf2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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