perf(boot): run independent providers concurrently
`bootProviders` was a `for...of` with an `await` inside, so three providers that share nothing took the sum of their latencies rather than the longest one. Measured on the issue's case — three providers, no edges, 200ms each — 616ms before, 210ms after. The graph already knew. `topoSort` proves which nodes cannot constrain each other and then throws that away by returning a flat list; `topoLevels` groups the sorted list back into dependency levels in one forward pass, and boot runs a level with `Promise.all`. Nothing else about the ordering changes: level N is fully registered and warmed before level N+1 starts, which is the same guarantee the sequential walk gave. This is the second thing users get for declaring `needs`/`provides` instead of ordering `app.use` calls by hand. Pruning was the first. An Express app cannot parallelize its middleware because nothing declares what is independent — we have that declaration and were not spending it. Three things had to survive the change, and each has a test: - **Teardown stays deterministic.** Registration follows level order, not completion order, so the registry running in reverse still gives dependants teardown before their dependencies. Following whichever provider settled first would have turned that from a guarantee into luck. - **A failed sibling does not strand the level.** `bootProvider` returns an outcome instead of rejecting, and successful teardowns are registered before any failure is raised — so a boot aborted by a required provider still leaves whatever its siblings opened closeable. Previously they had not started at all; now they may be in flight. - **A provider that threw gets no teardown**, unchanged: it never provided anything, and `dispose()` on a half-constructed one is how a teardown finds a null connection. Behaviour visible to a bus consumer: `boot:provider:start` no longer strictly alternates with `:ok`. A level emits its starts together, then its results. That is the truth about what is happening rather than a change in what is reported. Verified non-vacuous: reverting `src/` fails the overlap assertion and all five `topoLevels` cases, and puts the benchmark back at 616ms. Closes #71 Signed-off-by: Diego Resendez <diego.resendez@zero-oneit.com>
This commit is contained in:
4 files changed
+319
-25
No files matched your search
+29
-1
@@ -1,5 +1,5 @@
|
||||
import { describe, expect, it, test } from 'vitest';
|
||||
import { topoSort, GraphNode, subgraphFor, nearest } from '../src/graph';
|
||||
import { topoSort, topoLevels, GraphNode, subgraphFor, nearest } from '../src/graph';
|
||||
|
||||
const node = (name: string, needs: string[], provides: string[]): GraphNode =>
|
||||
({ name, needs, provides, origin: 'test' });
|
||||
@@ -42,6 +42,34 @@ describe('subgraphFor', () => {
|
||||
});
|
||||
});
|
||||
|
||||
describe('topoLevels', () => {
|
||||
const levels = (ordered: GraphNode[]): string[][] => topoLevels(ordered).map((l) => l.map((n) => n.name));
|
||||
|
||||
it('puts nodes that cannot constrain each other in the same level', () => {
|
||||
expect(levels([N('a'), N('b'), N('c')])).toEqual([['a', 'b', 'c']]);
|
||||
});
|
||||
|
||||
it('pushes a node past every producer it needs, not just its first', () => {
|
||||
// `late` needs a depth-0 and a depth-1 node, so it belongs at 2 — taking the max, not the first.
|
||||
const ordered = [N('config'), N('db', ['config']), N('late', ['config', 'db'])];
|
||||
expect(levels(ordered)).toEqual([['config'], ['db'], ['late']]);
|
||||
});
|
||||
|
||||
it('ignores needs nothing here produces, so a seed does not push a node down a level', () => {
|
||||
expect(levels([N('a'), N('user', ['req', 'params'])])).toEqual([['a', 'user']]);
|
||||
});
|
||||
|
||||
it('leaves no gaps, so every level has something to run', () => {
|
||||
const ordered = topoSort([N('c', ['b']), N('a'), N('b', ['a']), N('solo')], []);
|
||||
expect(topoLevels(ordered).every((level) => level.length > 0)).toBe(true);
|
||||
});
|
||||
|
||||
it('keeps every node exactly once', () => {
|
||||
const ordered = topoSort([N('c', ['b']), N('a'), N('b', ['a']), N('solo')], []);
|
||||
expect(topoLevels(ordered).flat().map((n) => n.name).sort()).toEqual(['a', 'b', 'c', 'solo']);
|
||||
});
|
||||
});
|
||||
|
||||
describe('nearest', () => {
|
||||
it('suggests a close candidate (edit distance <= 2)', () => {
|
||||
expect(nearest('usr', ['user', 'config', 'db'])).toBe('user');
|
||||
|
||||
Reference in new issue
Block a user