diff --git a/.jules/bolt.md b/.jules/bolt.md new file mode 100644 index 00000000..0205bf85 --- /dev/null +++ b/.jules/bolt.md @@ -0,0 +1,3 @@ +## 2024-06-25 - Parallelizing State-Dependent Array Fetches +**Learning:** Sequential network calls inside a loop (`for...of`) create cumulative latency, especially when collecting configuration items across multiple potential reverse proxy paths. It's safe to parallelize these requests if the state side-effects (e.g., updating `lastStatus`, `lastError`, `lastRaw`) depend strictly on the sequence order. +**Action:** Replace sequential loops with `await Promise.all(paths.map(fetchFn))`. Then, iterate through the resolved results array (`for (const r of results)`) to process side effects. This guarantees order-dependent logic remains identical while shifting the I/O bottleneck from serial to parallel execution. diff --git a/src/lib/forge-openai-surface.ts b/src/lib/forge-openai-surface.ts index 93415262..9c58541e 100644 --- a/src/lib/forge-openai-surface.ts +++ b/src/lib/forge-openai-surface.ts @@ -118,8 +118,11 @@ export async function collectZimaOSV1ModelEntries( let lastError: string | undefined; let lastRaw: unknown = null; - for (const path of V1_MODELS_PATHS) { - const r = await fetchZimaOSJson(email, path); + const results = await Promise.all( + V1_MODELS_PATHS.map(path => fetchZimaOSJson(email, path)) + ); + + for (const r of results) { lastStatus = r.status; lastError = r.error; lastRaw = r.data;