From c5cc3501dca26e26f43a1a07ccf31b5585bbb5e9 Mon Sep 17 00:00:00 2001 From: Bret Comnes Date: Thu, 3 Dec 2026 17:44:52 -0800 Subject: [PATCH] Clamp divided concurrency to at least 1 on single-vCPU machines --- lib/build-pages/build.js | 21 ++++++++++++++++----- lib/build-pages/build.test.js | 27 +++++++++++++++++++++++++++ 2 files changed, 43 insertions(+), 5 deletions(-) create mode 100644 lib/build-pages/build.test.js diff --git a/lib/build-pages/build.js b/lib/build-pages/build.js index 795c2101..8f343255 100644 --- a/lib/build-pages/build.js +++ b/lib/build-pages/build.js @@ -31,7 +31,21 @@ import { pageInfoForWorker, serializeBuildError } from './worker/protocol.js' import { createMdResolver } from './page-builders/md/create-md-resolver.js' import { MarkdownPreparationCache } from './source-preparation/markdown-cache.js' -const MAX_CONCURRENCY = Math.min(cpus().length, 24) +const MAX_CONCURRENCY = Math.max(1, Math.min(cpus().length, 24)) + +/** + * Split a concurrency budget into two positive halves for concurrent write phases. + * + * @param {number} maxConcurrency + * @returns {[number, number]} + */ +export function divideConcurrency (maxConcurrency) { + const safeMax = Math.max(1, Math.floor(maxConcurrency)) + return [ + Math.max(1, Math.ceil(safeMax / 2)), + Math.max(1, Math.floor(safeMax / 2)), + ] +} const __dirname = import.meta.dirname @@ -211,10 +225,7 @@ export async function buildPagesDirect (_src, dest, siteData, opts) { ? concretePages.filter(page => pageFilterSet.has(page.pageInfo.pageFile.filepath)) : concretePages - /** @type {[number, number]} Divided concurrency values */ - const dividedConcurrency = MAX_CONCURRENCY % 2 - ? [((MAX_CONCURRENCY - 1) / 2) + 1, (MAX_CONCURRENCY - 1) / 2] // odd - : [MAX_CONCURRENCY / 2, MAX_CONCURRENCY / 2] // even + const dividedConcurrency = divideConcurrency(MAX_CONCURRENCY) const templatesToRender = templateFilterSet ? siteData.templates.filter(t => templateFilterSet.has(t.templateFile.filepath)) diff --git a/lib/build-pages/build.test.js b/lib/build-pages/build.test.js new file mode 100644 index 00000000..7536ded7 --- /dev/null +++ b/lib/build-pages/build.test.js @@ -0,0 +1,27 @@ +import { describe, it } from 'node:test' +import assert from 'node:assert/strict' +import pMap from 'p-map' +import { divideConcurrency } from './build.js' + +describe('divideConcurrency', () => { + it('clamps both halves to at least 1 on single-vCPU machines', async () => { + const [pagesConcurrency, templatesConcurrency] = divideConcurrency(1) + assert.deepEqual([pagesConcurrency, templatesConcurrency], [1, 1]) + + const results = await pMap(['a', 'b'], async item => item, { concurrency: templatesConcurrency }) + assert.deepEqual(results, ['a', 'b']) + }) + + it('clamps non-positive values to [1, 1]', () => { + assert.deepEqual(divideConcurrency(0), [1, 1]) + assert.deepEqual(divideConcurrency(-2), [1, 1]) + }) + + it('splits even and odd concurrency values across both halves', () => { + assert.deepEqual(divideConcurrency(2), [1, 1]) + assert.deepEqual(divideConcurrency(3), [2, 1]) + assert.deepEqual(divideConcurrency(4), [2, 2]) + assert.deepEqual(divideConcurrency(5), [3, 2]) + assert.deepEqual(divideConcurrency(24), [12, 12]) + }) +})