1
1
//@ts -check
2
+ var os = require ( "os" ) ;
2
3
var arg = require ( "./rescript_arg.js" ) ;
4
+
3
5
var format_usage = `Usage: rescript format <options> [files]
4
6
5
7
\`rescript format\` formats the current directory
@@ -67,18 +69,43 @@ async function readStdin() {
67
69
return Buffer . concat ( chunks ) . toString ( "utf8" ) ;
68
70
}
69
71
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
+
70
93
/**
71
94
* @param {string[] } files
72
95
* @param {string } bsc_exe
73
96
* @param {(x: string) => boolean } isSupportedFile
74
97
* @param {boolean } checkFormatting
75
98
*/
76
99
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 ;
78
105
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 => {
82
109
const flags = checkFormatting
83
110
? [ "-format" , file ]
84
111
: [ "-o" , file , "-format" , file ] ;
@@ -90,10 +117,9 @@ async function formatFiles(files, bsc_exe, isSupportedFile, checkFormatting) {
90
117
incorrectlyFormattedFiles ++ ;
91
118
}
92
119
}
93
- }
94
- return null ;
95
- } ) ,
96
- ) ;
120
+ } ) ,
121
+ ) ;
122
+ }
97
123
} catch ( err ) {
98
124
console . error ( err ) ;
99
125
process . exit ( 2 ) ;
0 commit comments