Skip to content

Commit c41acdf

Browse files
committed
perf: only copy jsx-ast's input on the main thread
Copying every chunk's input with one thread cost ~2.5s of a ~24s build, while only `jsx-ast` needs it: building a page changes its entries, and on the main thread those are the metadata other generators read. It now copies its own input there, and workers keep using theirs as is. Assisted-by: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 955c8ac commit c41acdf

2 files changed

Lines changed: 9 additions & 10 deletions

File tree

‎packages/core/src/threading/parallel.mjs‎

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -96,17 +96,10 @@ export default function createParallelWorker(
9696
{ generator: name, chunks: chunks.length, chunkSize, threads }
9797
);
9898

99-
// On this thread, one chunk at a time, so their work isn't all held at
100-
// once, and on a copy, as a worker gets, which the chunk can change
99+
// On this thread, one chunk at a time, so their work isn't all held at once
101100
if (threads <= 1 || items.length <= 2) {
102101
for (const indices of chunks) {
103-
const input = structuredClone(indices.map(i => items[i]));
104-
105-
yield await generator.processChunk(
106-
input,
107-
input.keys().toArray(),
108-
extra
109-
);
102+
yield await generator.processChunk(items, indices, extra);
110103
}
111104

112105
return;

‎packages/react/src/jsx-ast/generate.mjs‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { isMainThread } from 'node:worker_threads';
2+
13
import getConfig from '@doc-kit/core/utils/configuration/index.mjs';
24
import { groupNodesByModule } from '@doc-kit/core/utils/generators.mjs';
35
import { jsx, toJs } from 'estree-util-to-js';
@@ -24,7 +26,11 @@ export async function processChunk(slicedInput, itemIndices) {
2426
const results = [];
2527

2628
for (const idx of itemIndices) {
27-
const { head, entries } = slicedInput[idx];
29+
// Building a page changes its entries. A worker has its own copy, but on
30+
// the main thread they're the metadata other generators read too
31+
const { head, entries } = isMainThread
32+
? structuredClone(slicedInput[idx])
33+
: slicedInput[idx];
2834

2935
const { content, ...page } = await buildContent(entries, head);
3036

0 commit comments

Comments
 (0)