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-05-18 - Optimize Stateful Sequential Iteration Over Network I/O
**Learning:** Sequential network calls inside a `for...of` loop can become a significant bottleneck when querying multiple endpoints or paths, especially when the calls are independent.
**Action:** Use `Promise.all` to fetch all resources concurrently. To preserve side-effect logic dependent on order (like keeping the "last" status or error), store the resolved promises in an array and iterate over them sequentially in the same order as the original paths array.
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The description of the action is slightly inaccurate. Promise.all returns results in the same order as the input array, so there is no need to manually store promises and iterate over them sequentially to preserve order. Using Promise.all directly is the standard and most efficient way to handle this pattern.

Suggested change
**Action:** Use `Promise.all` to fetch all resources concurrently. To preserve side-effect logic dependent on order (like keeping the "last" status or error), store the resolved promises in an array and iterate over them sequentially in the same order as the original paths array.
**Action:** Use `Promise.all` to initiate network requests concurrently. This allows the system to wait for all responses in parallel while receiving the results in the original order, ensuring that stateful logic (like fallback resolution) remains consistent and predictable.

9 changes: 7 additions & 2 deletions src/lib/forge-openai-surface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,8 +118,13 @@ 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);
// ⚡ Bolt: Initiate all network requests concurrently to avoid sequential network delays.
// We don't await Promise.all so we process them individually as they arrive
// while preserving the original stateful fallback resolution logic.
const fetches = V1_MODELS_PATHS.map((path) => fetchZimaOSJson(email, path));

for (const p of fetches) {
const r = await p;
lastStatus = r.status;
lastError = r.error;
lastRaw = r.data;
Comment on lines +122 to 130
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The comment on line 122 is misleading. The current implementation using a for...of loop over an array of promises awaits them in their original array order, meaning it does not process them "individually as they arrive". If the first request is slow, the second request's result will wait even if it finished earlier. To achieve concurrent fetching while maintaining the original stateful logic in a more idiomatic way, I suggest using Promise.all which natively preserves the order of results.

Suggested change
// We don't await Promise.all so we process them individually as they arrive
// while preserving the original stateful fallback resolution logic.
const fetches = V1_MODELS_PATHS.map((path) => fetchZimaOSJson(email, path));
for (const p of fetches) {
const r = await p;
lastStatus = r.status;
lastError = r.error;
lastRaw = r.data;
// We await them in order to preserve the original stateful fallback resolution logic.
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;

Expand Down