diff --git a/.jules/bolt.md b/.jules/bolt.md new file mode 100644 index 00000000..d5d04628 --- /dev/null +++ b/.jules/bolt.md @@ -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. diff --git a/apps/api/src/application/usecase-invocations.ts b/apps/api/src/application/usecase-invocations.ts index 8625c08b..e85821c7 100644 --- a/apps/api/src/application/usecase-invocations.ts +++ b/apps/api/src/application/usecase-invocations.ts @@ -46,9 +46,21 @@ export async function invocationGraph( projectId: string ): Promise { 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; }