-
Notifications
You must be signed in to change notification settings - Fork 0
⚡ Bolt: Optimize ZimaOS V1 models fetch with concurrent promises #39
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: dev
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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. | ||
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -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
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The comment on line 122 is misleading. The current implementation using a
Suggested change
|
||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The description of the action is slightly inaccurate.
Promise.allreturns 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. UsingPromise.alldirectly is the standard and most efficient way to handle this pattern.