Skip to content

Commit fbce898

Browse files
authored
perf: let jsx-ast describe its pages ahead of worker.stream
worker.stream takes its next batch only once there's room for its chunks, so jsx-ast's describe() was read no faster than the pool built pages, and the manifest went out only once the last of section-pages' batches got room: on Node.js's docs that's with 12 to 18 page chunks left. html bundles once the manifest arrives and renders nothing until the bundles are built, so bundling stopped overlapping with building the pages, and so did rendering, all.html's included. jsx-ast now describes every page as it arrives into a channel, which worker.stream reads as it has room. The manifest goes out once the input ends, as before the backpressure. A failure reading the input reaches the output through the manifest. The channel holds what the replay of section-pages' output would hold until read anyway. Assisted-by: Claude Opus 5.5 <noreply@anthropic.com>
1 parent d495db3 commit fbce898

2 files changed

Lines changed: 105 additions & 6 deletions

File tree

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

Lines changed: 80 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -220,7 +220,7 @@ describe('jsx-ast generate', () => {
220220
async *stream(batches) {
221221
const all = [];
222222

223-
// Like the pool, every batch is taken as it arrives
223+
// Every batch is taken as it arrives
224224
for await (const items of batches) {
225225
all.push(items);
226226
}
@@ -259,6 +259,85 @@ describe('jsx-ast generate', () => {
259259
}
260260
);
261261

262+
it(
263+
'emits the manifest while worker.stream holds back the next batch',
264+
// The pool only takes the next batch once there's room, and the first
265+
// page holds it until the manifest has arrived, so a manifest that waited
266+
// for the pool to take every batch would never come
267+
{ timeout: 10_000 },
268+
async () => {
269+
await setConfig({ target: ['jsx-ast'] });
270+
271+
const gate = Promise.withResolvers();
272+
const worker = {
273+
async *stream(batches) {
274+
// One batch at a time: the next is only taken once this one's built
275+
for await (const items of batches) {
276+
if (items.some(({ head }) => head.api === 'fs')) {
277+
await gate.promise;
278+
}
279+
280+
yield items.map(({ head }) => ({ data: head }));
281+
}
282+
},
283+
};
284+
285+
const output = generate(
286+
streamOf([
287+
[createEntry('fs', 'File system')],
288+
[createEntry('zlib', 'Zlib')],
289+
]),
290+
worker
291+
);
292+
293+
const { value } = await output.next();
294+
295+
assert.deepEqual(
296+
value[0].manifest.map(({ api }) => api),
297+
['fs', 'zlib', '404']
298+
);
299+
300+
gate.resolve();
301+
302+
const { pages } = await collect(output);
303+
304+
assert.deepEqual(
305+
pages.map(({ data }) => data.api),
306+
['fs', 'zlib', '404']
307+
);
308+
}
309+
);
310+
311+
it('rejects with the failure of its input, leaving none unhandled', async () => {
312+
await setConfig({ target: ['jsx-ast'] });
313+
314+
const failure = new Error('input failed');
315+
const unhandled = [];
316+
const onUnhandled = error => unhandled.push(error);
317+
318+
async function* failing() {
319+
yield [createEntry('fs', 'File system')];
320+
321+
throw failure;
322+
}
323+
324+
process.on('unhandledRejection', onUnhandled);
325+
326+
try {
327+
await assert.rejects(
328+
collect(generate(failing(), createWorker([]))),
329+
failure
330+
);
331+
332+
// A rejection left unhandled is reported once the microtasks have run
333+
await new Promise(resolve => setImmediate(resolve));
334+
} finally {
335+
process.off('unhandledRejection', onUnhandled);
336+
}
337+
338+
assert.deepEqual(unhandled, []);
339+
});
340+
262341
it(
263342
'lets go of the pages it emits before the manifest',
264343
// The input only ends once the test lets it, so pages that waited for the

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

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { isMainThread } from 'node:worker_threads';
22

3+
import { createChannel } from '@doc-kit/core/utils/channel.mjs';
34
import getConfig from '@doc-kit/core/utils/configuration/index.mjs';
45
import {
56
groupNodesByModule,
@@ -50,10 +51,9 @@ export async function processChunk(slicedInput, itemIndices) {
5051
* is emitted once built, in the order the pages arrived, the synthetic pages
5152
* last: the `html` generator puts them in the order the site lists them.
5253
*
53-
* Once every page has arrived, which `worker.stream` reads as it has room for
54-
* their chunks, it also emits `{ manifest }`, without waiting for the pages
55-
* still being built: every page's head, in that same order, so that `html` can
56-
* prepare what needs the whole site in the meantime.
54+
* Once every page has arrived, it also emits `{ manifest }`, without waiting
55+
* for the pages still being built: every page's head, in that same order, so
56+
* that `html` can prepare what needs the whole site in the meantime.
5757
*
5858
* @type {import('./types').Generator['generate']}
5959
*/
@@ -109,6 +109,26 @@ export async function* generate(input, worker) {
109109
yield [{ manifest: heads }];
110110
}
111111

112+
const pages = createChannel();
113+
114+
/**
115+
* Describes every page as it arrives, ahead of `worker.stream`, which only
116+
* takes the next batch once there's room for its chunks: the manifest then
117+
* resolves once the input ends, not once the last pages are being built.
118+
*/
119+
const readPages = async () => {
120+
try {
121+
for await (const descriptors of describe()) {
122+
pages.push(descriptors);
123+
}
124+
} finally {
125+
pages.end();
126+
}
127+
};
128+
129+
// A failure reading the input reaches the output through the manifest
130+
readPages().catch(error => manifest.reject(error));
131+
112132
// The manifest goes out as soon as it's ready, not after the next pages
113-
yield* merge(worker.stream(describe()), announce());
133+
yield* merge(worker.stream(pages), announce());
114134
}

0 commit comments

Comments
 (0)