Skip to content

Commit a082cae

Browse files
committed
chore: reduce memory footprint
1 parent a793466 commit a082cae

12 files changed

Lines changed: 578 additions & 324 deletions

File tree

‎src/__tests__/caching.test.mjs‎

Lines changed: 145 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
1+
import { deepStrictEqual, ok, strictEqual } from 'node:assert';
2+
import { describe, it } from 'node:test';
3+
4+
import { collectAsyncGenerator, createCache } from '../caching.mjs';
5+
6+
describe('caching', () => {
7+
describe('collectAsyncGenerator', () => {
8+
it('should collect all chunks into a flat array', async () => {
9+
async function* gen() {
10+
yield [1, 2];
11+
yield [3, 4];
12+
yield [5];
13+
}
14+
15+
const result = await collectAsyncGenerator(gen());
16+
17+
deepStrictEqual(result, [1, 2, 3, 4, 5]);
18+
});
19+
20+
it('should return empty array for empty generator', async () => {
21+
async function* gen() {
22+
// empty generator
23+
}
24+
25+
const result = await collectAsyncGenerator(gen());
26+
27+
deepStrictEqual(result, []);
28+
});
29+
30+
it('should handle empty chunks', async () => {
31+
async function* gen() {
32+
yield [];
33+
yield [1];
34+
yield [];
35+
yield [2, 3];
36+
}
37+
38+
const result = await collectAsyncGenerator(gen());
39+
40+
deepStrictEqual(result, [1, 2, 3]);
41+
});
42+
});
43+
44+
describe('createCache', () => {
45+
it('should expose the expected methods', () => {
46+
const cache = createCache();
47+
48+
ok(cache);
49+
strictEqual(typeof cache.has, 'function');
50+
strictEqual(typeof cache.store, 'function');
51+
strictEqual(typeof cache.expectConsumers, 'function');
52+
strictEqual(typeof cache.consume, 'function');
53+
});
54+
55+
it('should report whether a generator is stored', () => {
56+
const cache = createCache();
57+
58+
strictEqual(cache.has('a'), false);
59+
cache.store('a', Promise.resolve(1));
60+
strictEqual(cache.has('a'), true);
61+
});
62+
63+
it('should pass through non-streaming results', async () => {
64+
const cache = createCache();
65+
66+
cache.store('a', Promise.resolve({ value: 42 }));
67+
68+
deepStrictEqual(await cache.consume('a'), { value: 42 });
69+
});
70+
71+
it('should collect a streaming result only once for all consumers', async () => {
72+
const cache = createCache();
73+
74+
let iterations = 0;
75+
76+
async function* gen() {
77+
iterations++;
78+
yield [1, 2];
79+
yield [3];
80+
}
81+
82+
cache.store('a', gen());
83+
cache.expectConsumers({ a: 2 });
84+
85+
const first = await cache.consume('a');
86+
const second = await cache.consume('a');
87+
88+
deepStrictEqual(first, [1, 2, 3]);
89+
// The same collected array is shared, collection happened a single time
90+
strictEqual(first, second);
91+
strictEqual(iterations, 1);
92+
});
93+
94+
it('should evict a result once the final consumer has read it', async () => {
95+
const cache = createCache();
96+
97+
cache.store('a', Promise.resolve('value'));
98+
cache.expectConsumers({ a: 1 });
99+
100+
strictEqual(await cache.consume('a'), 'value');
101+
// After the last consumer, the entry is dropped so it can be GC'd
102+
strictEqual(cache.has('a'), false);
103+
});
104+
105+
it('should keep a result until all expected consumers have read it', async () => {
106+
const cache = createCache();
107+
108+
cache.store('a', Promise.resolve('value'));
109+
cache.expectConsumers({ a: 2 });
110+
111+
await cache.consume('a');
112+
strictEqual(cache.has('a'), true);
113+
114+
await cache.consume('a');
115+
strictEqual(cache.has('a'), false);
116+
});
117+
118+
it('should count consumers across a dependency closure', async () => {
119+
const cache = createCache();
120+
121+
// graph: a -> b -> c and d -> b; requested targets are a and d
122+
const graph = { a: 'b', b: 'c', d: 'b' };
123+
124+
cache.populateConsumerCounts(['a', 'd'], name => graph[name]);
125+
126+
// `b` is depended on by both `a` and `d`, so it needs two reads before
127+
// it is evicted.
128+
cache.store('b', Promise.resolve('B'));
129+
await cache.consume('b');
130+
strictEqual(cache.has('b'), true);
131+
await cache.consume('b');
132+
strictEqual(cache.has('b'), false);
133+
134+
// `c` is depended on only by `b`, so a single read evicts it.
135+
cache.store('c', Promise.resolve('C'));
136+
await cache.consume('c');
137+
strictEqual(cache.has('c'), false);
138+
139+
// `a` is a requested target, consumed exactly once by the final read.
140+
cache.store('a', Promise.resolve('A'));
141+
await cache.consume('a');
142+
strictEqual(cache.has('a'), false);
143+
});
144+
});
145+
});

‎src/__tests__/generators.test.mjs‎

Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
1+
import assert from 'node:assert/strict';
2+
import { describe, it, mock, beforeEach } from 'node:test';
3+
4+
// Tracks how many times each synthetic generator actually executed so we can
5+
// assert that a shared dependency runs exactly once even after its result is
6+
// evicted from the cache once its last consumer has read it.
7+
const runs = {};
8+
9+
const record = name => {
10+
runs[name] = (runs[name] ?? 0) + 1;
11+
};
12+
13+
// Yields a single chunk as an async generator (mirrors streaming generators)
14+
const streamOf = chunk =>
15+
(async function* () {
16+
yield chunk;
17+
})();
18+
19+
mock.module('../generators/index.mjs', {
20+
namedExports: {
21+
allGenerators: {
22+
// Root streaming generator (no dependency)
23+
ast: {
24+
name: 'ast',
25+
hasParallelProcessor: true,
26+
generate: async () => {
27+
record('ast');
28+
return streamOf([{ ast: true }]);
29+
},
30+
},
31+
// Streaming generator shared by multiple consumers
32+
metadata: {
33+
name: 'metadata',
34+
dependsOn: 'ast',
35+
hasParallelProcessor: true,
36+
generate: async input => {
37+
record('metadata');
38+
return streamOf([{ meta: input.length }]);
39+
},
40+
},
41+
// Two non-streaming consumers of the shared `metadata` result
42+
'gen-a': {
43+
name: 'gen-a',
44+
dependsOn: 'metadata',
45+
generate: async input => {
46+
record('gen-a');
47+
return { a: input };
48+
},
49+
},
50+
'gen-b': {
51+
name: 'gen-b',
52+
dependsOn: 'metadata',
53+
generate: async input => {
54+
record('gen-b');
55+
return { b: input };
56+
},
57+
},
58+
// A target that is itself depended upon by another target
59+
'gen-c': {
60+
name: 'gen-c',
61+
dependsOn: 'metadata',
62+
hasParallelProcessor: true,
63+
generate: async () => {
64+
record('gen-c');
65+
return streamOf([{ c: true }]);
66+
},
67+
},
68+
'gen-c-all': {
69+
name: 'gen-c-all',
70+
dependsOn: 'gen-c',
71+
generate: async input => {
72+
record('gen-c-all');
73+
return { all: input };
74+
},
75+
},
76+
},
77+
},
78+
});
79+
80+
mock.module('../threading/index.mjs', {
81+
defaultExport: () => ({
82+
run: async () => undefined,
83+
destroy: async () => undefined,
84+
}),
85+
});
86+
87+
mock.module('../threading/parallel.mjs', {
88+
defaultExport: () => ({
89+
async *stream() {
90+
// Unused: the mocked generators return their own async generators
91+
},
92+
}),
93+
});
94+
95+
const createGenerator = (await import('../generators.mjs')).default;
96+
97+
describe('createGenerator orchestration', () => {
98+
beforeEach(() => {
99+
for (const key of Object.keys(runs)) {
100+
delete runs[key];
101+
}
102+
});
103+
104+
it('runs a shared dependency exactly once and returns correct results', async () => {
105+
const { runGenerators } = createGenerator();
106+
107+
const results = await runGenerators({
108+
target: ['gen-a', 'gen-b'],
109+
threads: 1,
110+
});
111+
112+
// ast -> metadata are shared and must each execute a single time
113+
assert.equal(runs.ast, 1);
114+
assert.equal(runs.metadata, 1);
115+
assert.equal(runs['gen-a'], 1);
116+
assert.equal(runs['gen-b'], 1);
117+
118+
// metadata collected one entry whose `meta` is the ast array length (1)
119+
assert.deepStrictEqual(results, [
120+
{ a: [{ meta: 1 }] },
121+
{ b: [{ meta: 1 }] },
122+
]);
123+
});
124+
125+
it('does not re-run a target that is also another target dependency', async () => {
126+
const { runGenerators } = createGenerator();
127+
128+
const results = await runGenerators({
129+
target: ['gen-c', 'gen-c-all'],
130+
threads: 1,
131+
});
132+
133+
// gen-c is both a requested target and a dependency of gen-c-all; eviction
134+
// must not cause it to be scheduled (and run) twice.
135+
assert.equal(runs['gen-c'], 1);
136+
assert.equal(runs['gen-c-all'], 1);
137+
138+
assert.deepStrictEqual(results, [[{ c: true }], { all: [{ c: true }] }]);
139+
});
140+
});

0 commit comments

Comments
 (0)