From 8563bff49d0259aabedfe4b556e4c335a3c52df4 Mon Sep 17 00:00:00 2001 From: XMadrid Date: Wed, 4 Jun 2025 11:16:11 +0800 Subject: [PATCH 1/2] improve performance and reduce memory cost during precompile and compile stage --- src/core/compile.ts | 9 +++++---- src/core/precompile.ts | 11 +++++++---- 2 files changed, 12 insertions(+), 8 deletions(-) diff --git a/src/core/compile.ts b/src/core/compile.ts index 239de7b..a24c71a 100644 --- a/src/core/compile.ts +++ b/src/core/compile.ts @@ -3,7 +3,6 @@ import { join, relative } from "node:path"; import { findRoot } from "../utils/pathResolver.js"; export async function compile(testCodePaths: string[], outputFolder: string, compileFlags: string): Promise { - const pms: Promise[] = []; const wasm: string[] = []; const root = findRoot(testCodePaths); const compile = async (testCodePath: string) => { @@ -33,10 +32,12 @@ export async function compile(testCodePaths: string[], outputFolder: string, com throw error; } }; - for (const testCodePath of testCodePaths) { - pms.push(compile(testCodePath)); + + // Here, for-await is more efficient and less memory cost than Promise.all() + for (const codePath of testCodePaths) { + await compile(codePath); } - await Promise.all(pms); + return wasm; } diff --git a/src/core/precompile.ts b/src/core/precompile.ts index ecc66de..1fe290b 100644 --- a/src/core/precompile.ts +++ b/src/core/precompile.ts @@ -22,11 +22,14 @@ export async function precompile( const testCodePaths = testcases ?? getRelatedFiles(includes, excludes, (path: string) => path.endsWith(".test.ts")); const sourceCodePaths = getRelatedFiles(includes, excludes, (path: string) => !path.endsWith(".test.ts")); - const sourceCodeTransforms: Promise[] = []; - for (const sourceCodePath of sourceCodePaths.values()) { - sourceCodeTransforms.push(transform(sourceCodePath, transformFunction, flags)); + + // The batchSize = 2 is empirical data after benchmarking + const batchSize = 2; + for (let i = 0; i < sourceCodePaths.length; i += batchSize) { + await Promise.all( + sourceCodePaths.slice(i, i + batchSize).map((sourcePath) => transform(sourcePath, transformFunction, flags)) + ); } - await Promise.all(sourceCodeTransforms); return { testCodePaths, From 8efd196cf047e496ad2ee05f923e8dbd471555f3 Mon Sep 17 00:00:00 2001 From: XMadrid Date: Wed, 4 Jun 2025 14:06:22 +0800 Subject: [PATCH 2/2] return -1 while test crash due to node heap out of memory --- bin/as-test.js | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/bin/as-test.js b/bin/as-test.js index 5f9fac7..1a381a1 100755 --- a/bin/as-test.js +++ b/bin/as-test.js @@ -26,3 +26,10 @@ const testapp = spawn("node", argv, { stdio: "inherit" }); testapp.on("close", function (code) { exit(code); }); + +testapp.on("exit", (_code, signal) => { + if (signal === "SIGABRT") { + // node heap out of memory + exit(-1); + } +});