11//@ts -check
2+ var os = require ( "os" ) ;
23var arg = require ( "./rescript_arg.js" ) ;
4+
35var 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 */
7699async 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