Skip to content

Commit ee779b6

Browse files
authored
perf: keep none of the CLI's target output
runGenerators collected every requested generator's output into the arrays it returns, though neither generate nor preview reads them. With { results: false } it drains them instead: each generator still runs to its end, and once nothing else reads its output, its chunks go as they are recorded. Both commands now use it. Assisted-by: Claude Opus 5.5 <noreply@anthropic.com> Assisted-by: Claude Sonnet 5.5 <noreply@anthropic.com>
1 parent 265eb17 commit ee779b6

7 files changed

Lines changed: 150 additions & 11 deletions

File tree

‎.changeset/drain-results.md‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
'@doc-kit/core': minor
3+
'@doc-kit/cli': patch
4+
---
5+
6+
`runGenerators(configuration, { results: false })` keeps none of the requested generators' output: each still runs to its end, and once nothing else reads its output, its chunks go as they are recorded. The CLI's `generate` and `preview` commands use it, as they don't read the results.

‎packages/cli/bin/commands/generate.mjs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,6 @@ export default generate.action(
6464
const config = await setConfig(opts);
6565
assertRunnableOptions(config);
6666

67-
await runGenerators(config);
67+
await runGenerators(config, { results: false });
6868
})
6969
);

‎packages/cli/bin/commands/preview.mjs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ export default preview.action(
6262
const config = await setConfig(opts);
6363
assertRunnableOptions(config);
6464

65-
await runGenerators(config);
65+
await runGenerators(config, { results: false });
6666

6767
// With the default coming from the configuration, this is always set
6868
output = config.global.output;

‎packages/core/src/__tests__/caching.test.mjs‎

Lines changed: 84 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { deepStrictEqual, strictEqual, throws } from 'node:assert';
1+
import { deepStrictEqual, rejects, strictEqual, throws } from 'node:assert';
22
import { describe, it } from 'node:test';
33

44
import { collectAsyncGenerator, createCache } from '../caching.mjs';
@@ -212,6 +212,61 @@ describe('caching', () => {
212212
deepStrictEqual(await Array.fromAsync(stream), [[1], [2], [3]]);
213213
});
214214

215+
it('should drain a result, alongside its other readers', async () => {
216+
const cache = createCache();
217+
const produced = [];
218+
219+
cache.populateConsumerCounts(['a', 'b'], name =>
220+
name === 'b' ? 'a' : undefined
221+
);
222+
223+
async function* gen() {
224+
for (const value of [1, 2, 3]) {
225+
produced.push(value);
226+
227+
yield [value];
228+
}
229+
}
230+
231+
cache.store('a', gen());
232+
233+
const reader = await cache.stream('a');
234+
235+
await cache.drain('a');
236+
237+
strictEqual(cache.has('a'), false);
238+
deepStrictEqual(produced, [1, 2, 3]);
239+
240+
deepStrictEqual(await Array.fromAsync(reader), [[1], [2], [3]]);
241+
});
242+
243+
it('should let go of the chunks of a drained result as they are produced', async () => {
244+
const cache = createCache();
245+
const gate = Promise.withResolvers();
246+
const { released, chunkOf } = trackReleases();
247+
248+
cache.populateConsumerCounts(['a'], () => undefined);
249+
250+
async function* gen() {
251+
yield chunkOf(1);
252+
yield chunkOf(2);
253+
yield chunkOf(3);
254+
await gate.promise;
255+
}
256+
257+
cache.store('a', gen());
258+
259+
const draining = cache.drain('a');
260+
261+
// The generator waits, past its chunks: the first is no longer held
262+
await collectGarbage(() => released.has(1));
263+
264+
strictEqual(released.has(1), true);
265+
266+
gate.resolve();
267+
await draining;
268+
});
269+
215270
it('should let go of a chunk once every streaming reader has read it, while it still runs', async () => {
216271
const cache = createCache();
217272
const gate = Promise.withResolvers();
@@ -252,5 +307,33 @@ describe('caching', () => {
252307
await readValues(first);
253308
await readValues(second);
254309
});
310+
311+
it('should drain a non-streaming result', async () => {
312+
const cache = createCache();
313+
314+
cache.populateConsumerCounts(['a'], () => undefined);
315+
cache.store('a', Promise.resolve({ value: 1 }));
316+
317+
const drained = await cache.drain('a');
318+
319+
strictEqual(drained, undefined);
320+
strictEqual(cache.has('a'), false);
321+
});
322+
323+
it('should reject draining a generator that fails', async () => {
324+
const cache = createCache();
325+
326+
cache.populateConsumerCounts(['a'], () => undefined);
327+
328+
async function* gen() {
329+
yield [1];
330+
331+
throw new Error('drained failure');
332+
}
333+
334+
cache.store('a', gen());
335+
336+
await rejects(cache.drain('a'), /drained failure/);
337+
});
255338
});
256339
});

‎packages/core/src/__tests__/generators.test.mjs‎

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -347,4 +347,27 @@ describe('createGenerator orchestration', () => {
347347
assert.deepStrictEqual(one, [{ array: true }]);
348348
assert.deepStrictEqual(two, [{ array: false }]);
349349
});
350+
351+
it('runs every generator but keeps no results with results: false', async () => {
352+
const { runGenerators } = createGenerator();
353+
354+
const results = await runGenerators(
355+
{ target: ['gen-c', 'gen-c-all'], threads: 1 },
356+
{ results: false }
357+
);
358+
359+
assert.equal(results, undefined);
360+
assert.equal(runs['gen-c'], 1);
361+
assert.equal(runs['gen-c-all'], 1);
362+
});
363+
364+
it('rejects with a failure with results: false', async () => {
365+
await assert.rejects(
366+
createGenerator().runGenerators(
367+
{ target: ['failing-reader'], threads: 2 },
368+
{ results: false }
369+
),
370+
/source failed/
371+
);
372+
});
350373
});

‎packages/core/src/caching.mjs‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,30 @@ export const createCache = () => {
214214
return value;
215215
},
216216

217+
/**
218+
* Runs a generator to its end, keeping none of its result, which counts as
219+
* one of its reads.
220+
*
221+
* @param {string} name - Cache key (resolved generator specifier) to drain
222+
* @returns {Promise<void>}
223+
*/
224+
drain: async name => {
225+
const result = await results[name];
226+
227+
if (!isAsyncIterable(result)) {
228+
release(name);
229+
230+
return;
231+
}
232+
233+
const { ended } = teeOf(name, result);
234+
235+
// Once nothing else reads it, its chunks go as they're recorded
236+
release(name);
237+
238+
await ended;
239+
},
240+
217241
/**
218242
* Gives a reader of a generator's result as it is produced: chunk by chunk
219243
* if it streams, or as a single chunk once it resolves. As with `consume`,

‎packages/core/src/generators.mjs‎

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -112,9 +112,13 @@ const createGenerator = () => {
112112
* Runs all requested generators with their dependencies.
113113
*
114114
* @param {import('./utils/configuration/types').Configuration} configuration - Runtime options
115-
* @returns {Promise<unknown[]>} Results of all requested generators
115+
* @param {{ results?: boolean }} [options] - With `results: false`, none of
116+
* the requested generators' results is kept, so each can be let go as soon
117+
* as it is produced
118+
* @returns {Promise<unknown[] | undefined>} Results of all requested
119+
* generators, or `undefined` with `results: false`
116120
*/
117-
const runGenerators = async configuration => {
121+
const runGenerators = async (configuration, { results = true } = {}) => {
118122
const { target, threads } = configuration;
119123

120124
// Resolve shorthand names and load the full dependency closure up front,
@@ -147,15 +151,14 @@ const createGenerator = () => {
147151
scheduleGenerator(specifier, generators, inputOf, configuration);
148152
}
149153

150-
// Start all collections in parallel (don't await sequentially). Consuming
151-
// through the shared path lets the final read also trigger eviction.
152-
const results = await Promise.all(
153-
targets.map(specifier => cache.consume(specifier))
154-
);
154+
// Start all reads in parallel (don't await sequentially). Reading through
155+
// the shared path lets the final read also trigger eviction.
156+
const read = results ? cache.consume : cache.drain;
157+
const collected = await Promise.all(targets.map(read));
155158

156159
await pool.destroy();
157160

158-
return results;
161+
return results ? collected : undefined;
159162
};
160163

161164
return { runGenerators };

0 commit comments

Comments
 (0)