Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .jules/bolt.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
## 2024-03-24 - Parallelize sequential I/O queries in map loops
**Learning:** Sequential `for...of` loops performing I/O queries (like DB reads) for each iteration cause O(N) latency, creating significant bottlenecks as data grows.
**Action:** Replace sequential loops with `Promise.all(array.map(async item => { ... }))` to run independent queries concurrently, reducing latency to O(1) bounded by the slowest query. Always ensure queries are truly independent and DB connection limits allow the concurrent load.
16 changes: 14 additions & 2 deletions apps/api/src/application/usecase-invocations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,21 @@ export async function invocationGraph(
projectId: string
): Promise<InvocationGraph> {
const graph: InvocationGraph = new Map();
for (const usecase of await deps.useCaseStore.listUseCases(projectId)) {
graph.set(usecase.key, await invocationEdgesFor(deps, usecase));
const usecases = await deps.useCaseStore.listUseCases(projectId);

// ⚡ Bolt: Execute invocation edge queries concurrently instead of sequentially
// Reduces latency from O(N) to O(1) in the number of use cases
const results = await Promise.all(
usecases.map(async (usecase) => {
const edges = await invocationEdgesFor(deps, usecase);
return { key: usecase.key, edges };
})
);

for (const { key, edges } of results) {
graph.set(key, edges);
}

return graph;
}

Expand Down