Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 16 additions & 5 deletions lib/build-pages/build.js
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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))
Expand Down
27 changes: 27 additions & 0 deletions lib/build-pages/build.test.js
Original file line number Diff line number Diff line change
@@ -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])
})
})
Loading