perf(dump): refactor async iterator merging with promise slots#33
perf(dump): refactor async iterator merging with promise slots#33
Conversation
Replace the index-based findIndex/splice approach in mergeAsyncIterables with a more efficient "promise slot" pattern. By maintaining a fixed-length array of promises and using Promise.race, we: - Achieve O(1) slot updates after each resolve. - Avoid unnecessary array allocations and filter/splice calls in the loop. - Use NEVER_RESOLVE to cleanly manage finished generators. Also rename the function to mergeAsyncGenerators to better reflect its specialized usage with async generators. Co-authored-by: Gemini CLI <gemini-cli@google.com>
There was a problem hiding this comment.
Code Review
This pull request replaces the mergeAsyncIterables function with mergeAsyncGenerators, implementing a promise slot pattern to merge multiple async generators. The review feedback identifies opportunities to improve throughput by refilling promise slots before yielding and to prevent resource leaks by ensuring source generators are properly closed via a try...finally block.
Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>
|
Why it keeps OOM? So frustrating |
|
@claude do you have any idea why it keeps OOM when scanning the index and output? |
|
Claude encountered an error —— View job I'll analyze this and get back to you. |
|
The likely cause of the OOM is the In the previous implementation, finished iterators were removed from the In the new implementation, a finished slot is replaced with a promise that never settles: const NEVER_RESOLVE = new Promise(() => {});
promises[winner.idx] = NEVER_RESOLVE;while the loop continues to run: await Promise.race(promises);That means the finished slot stays in the race forever. If The old loop did not have this behavior because finished iterators were removed entirely instead of being kept as permanently pending slots. |
|
But NEVER_RESOLVE gets only instantiated once, all loop executions share exactly the same NEVER_RESOLVE instance. It should only occupy 1 slot of memory, the others are just references to the same promise. |
Refactor the async iterator merging logic to use a more efficient promise slot pattern with NEVER_RESOLVE and index-based tracking. This improves performance by avoiding O(N) searches and frequent array allocations.