Skip to content

Commit 80498be

Browse files
authored
Refactor: improve performance and reduce memory cost during precompile and compile stage (#38)
1 parent 5d671a6 commit 80498be

File tree

3 files changed

+19
-8
lines changed

3 files changed

+19
-8
lines changed

bin/as-test.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,3 +26,10 @@ const testapp = spawn("node", argv, { stdio: "inherit" });
2626
testapp.on("close", function (code) {
2727
exit(code);
2828
});
29+
30+
testapp.on("exit", (_code, signal) => {
31+
if (signal === "SIGABRT") {
32+
// node heap out of memory
33+
exit(-1);
34+
}
35+
});

src/core/compile.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ import { join, relative } from "node:path";
33
import { findRoot } from "../utils/pathResolver.js";
44

55
export async function compile(testCodePaths: string[], outputFolder: string, compileFlags: string): Promise<string[]> {
6-
const pms: Promise<void>[] = [];
76
const wasm: string[] = [];
87
const root = findRoot(testCodePaths);
98
const compile = async (testCodePath: string) => {
@@ -33,10 +32,12 @@ export async function compile(testCodePaths: string[], outputFolder: string, com
3332
throw error;
3433
}
3534
};
36-
for (const testCodePath of testCodePaths) {
37-
pms.push(compile(testCodePath));
35+
36+
// Here, for-await is more efficient and less memory cost than Promise.all()
37+
for (const codePath of testCodePaths) {
38+
await compile(codePath);
3839
}
39-
await Promise.all(pms);
40+
4041
return wasm;
4142
}
4243

src/core/precompile.ts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,14 @@ export async function precompile(
2222
const testCodePaths = testcases ?? getRelatedFiles(includes, excludes, (path: string) => path.endsWith(".test.ts"));
2323

2424
const sourceCodePaths = getRelatedFiles(includes, excludes, (path: string) => !path.endsWith(".test.ts"));
25-
const sourceCodeTransforms: Promise<void>[] = [];
26-
for (const sourceCodePath of sourceCodePaths.values()) {
27-
sourceCodeTransforms.push(transform(sourceCodePath, transformFunction, flags));
25+
26+
// The batchSize = 2 is empirical data after benchmarking
27+
const batchSize = 2;
28+
for (let i = 0; i < sourceCodePaths.length; i += batchSize) {
29+
await Promise.all(
30+
sourceCodePaths.slice(i, i + batchSize).map((sourcePath) => transform(sourcePath, transformFunction, flags))
31+
);
2832
}
29-
await Promise.all(sourceCodeTransforms);
3033

3134
return {
3235
testCodePaths,

0 commit comments

Comments
 (0)