From 541ad0a2ca7a82ebc4f80f18d5ff669872f97e63 Mon Sep 17 00:00:00 2001 From: Yuxuan Zhang Date: Sat, 30 Dec 2023 11:15:00 -0500 Subject: [PATCH] cleanup logic, improve perf --- src/node/build/render-worker.ts | 14 ++++++++++---- src/node/siteConfig.ts | 2 +- 2 files changed, 11 insertions(+), 5 deletions(-) diff --git a/src/node/build/render-worker.ts b/src/node/build/render-worker.ts index 04ce2773..8f8c890d 100644 --- a/src/node/build/render-worker.ts +++ b/src/node/build/render-worker.ts @@ -12,7 +12,14 @@ export default async function cluster( pages: string[], update: UpdateHandle ) { - const concurrency = context.config.buildConcurrency || 1 + // - Each render worker could consume up to 150% of a CPU core. + // - One extra core is allocated to the main thread. + // - Excess worker will cause too much RPC workload for main thread, + // therefore harm the overall performance. + const concurrency = Math.round( + Math.max((context.config.buildConcurrency - 1) / 1.5, 1) + ) + const num_tasks = pages.length const pageAlloc: TaskAllocator = async () => { @@ -52,12 +59,10 @@ async function renderWorker() { const ctx = new RpcContext(parentPort!) try { const { - concurrency, entryPath, pageAlloc, context }: { - concurrency: number entryPath: string pageAlloc: TaskAllocator context: RenderPageContext @@ -72,7 +77,8 @@ async function renderWorker() { await renderPage(render, page, context) } } - await Promise.all(Array.from({ length: concurrency * 4 }, () => executor())) + const concurrency = Math.max(context.config.buildConcurrency, 1) + await Promise.all(Array.from({ length: concurrency }, () => executor())) } catch (e) { console.error(e) } finally { diff --git a/src/node/siteConfig.ts b/src/node/siteConfig.ts index 21b99bce..92bae1d0 100644 --- a/src/node/siteConfig.ts +++ b/src/node/siteConfig.ts @@ -152,7 +152,7 @@ export interface UserConfig * A lower number will reduce the memory usage but will increase the build time. * * @experimental - * @default 64 + * @default "Number of CPU cores available" */ buildConcurrency?: number