Skip to content

⚡ Bolt: Optimize high-concurrency workflow execution#143

Closed
thalesraymond wants to merge 1 commit intomainfrom
bolt/optimize-workflow-executor-9498767348345128190
Closed

⚡ Bolt: Optimize high-concurrency workflow execution#143
thalesraymond wants to merge 1 commit intomainfrom
bolt/optimize-workflow-executor-9498767348345128190

Conversation

@thalesraymond
Copy link
Owner

💡 What: Replaced the Promise.race loop in WorkflowExecutor with a signal-based synchronization mechanism.
🎯 Why: Promise.race on a large Set of promises inside a loop causes significant O(N²) overhead as the number of concurrent tasks grows, because it re-attaches handlers to all executing promises in every iteration.
📊 Impact: Reduces execution time for 2000 concurrent tasks from ~2.2s to ~0.13s (a ~17x speedup).
🔬 Measurement: Verified using a temporary benchmark (tests/WorkflowExecutor_bench.test.ts) with 2000 tasks and random delays. Existing tests pass with 100% coverage.


PR created automatically by Jules for task 9498767348345128190 started by @thalesraymond

Replaces `Promise.race(executingPromises)` with a manually managed `signalPromise` in the main execution loop.

This optimization eliminates the O(N^2) overhead associated with repeatedly creating race promises for a large set of executing tasks.

Benchmarks show a significant performance improvement for high-concurrency scenarios:
- Before: ~2258ms for 2000 concurrent tasks (simulated delay)
- After: ~130ms for 2000 concurrent tasks

This change ensures that the `WorkflowExecutor` scales linearly with the number of tasks, rather than quadratically.

Co-authored-by: thalesraymond <32554150+thalesraymond@users.noreply.github.com>
@google-labs-jules
Copy link
Contributor

👋 Jules, reporting for duty! I'm here to lend a hand with this pull request.

When you start a review, I'll add a 👀 emoji to each comment to let you know I've read it. I'll focus on feedback directed at me and will do my best to stay out of conversations between you and other bots or reviewers to keep the noise down.

I'll push a commit with your requested changes shortly after. Please note there might be a delay between these steps, but rest assured I'm on the job!

For more direct control, you can switch me to Reactive Mode. When this mode is on, I will only act on comments where you specifically mention me with @jules. You can find this option in the Pull Request section of your global Jules UI settings. You can always switch back!

New to Jules? Learn more at jules.google/docs.


For security, I will only act on instructions from the user who triggered this task.

@gemini-code-assist
Copy link
Contributor

Summary of Changes

Hello @thalesraymond, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request significantly optimizes the WorkflowExecutor by refactoring its task synchronization mechanism. It moves away from an inefficient Promise.race loop, which suffered from quadratic performance degradation with increasing concurrency, to a signal-based approach. This change dramatically improves the execution speed of high-concurrency workflows, ensuring better scalability and responsiveness.

Highlights

  • Performance Optimization: Replaced the Promise.race loop in WorkflowExecutor with a more efficient signal-based synchronization mechanism.
  • Overhead Reduction: Eliminated the O(N²) overhead caused by Promise.race re-attaching handlers to all executing promises in every iteration, especially with a large number of concurrent tasks.
  • Significant Speedup: Achieved a ~17x speedup, reducing execution time for 2000 concurrent tasks from ~2.2s to ~0.13s.
  • Verification: Changes were verified using a temporary benchmark (tests/WorkflowExecutor_bench.test.ts) and existing tests pass with 100% coverage.
Changelog
  • src/WorkflowExecutor.ts
    • Introduced a new signal mechanism (resolveSignal, signalPromise, notify function) to manage asynchronous task completion more efficiently.
    • Modified the onAbort handler to call notify() when the workflow is cancelled, ensuring the main loop is woken up.
    • Updated the processLoop method signature to accept the new notify function.
    • Replaced await Promise.race(executingPromises) with await signalPromise in the main execution loop, and added logic to reset signalPromise after each wait.
    • Added a call to notify() within the finally block of task execution to signal the completion of a task and wake up the main loop for processing new work.
Activity
  • This pull request was automatically created by Jules for a task initiated by @thalesraymond.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

@sonarqubecloud
Copy link

sonarqubecloud bot commented Feb 6, 2026

@codecov
Copy link

codecov bot commented Feb 6, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

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

Code Review

This pull request introduces a significant performance optimization for high-concurrency workflows by replacing Promise.race with a custom signal-based mechanism. The implementation is clever and effectively addresses the O(N²) overhead issue.

My review includes a couple of suggestions to further improve the code's clarity and maintainability by removing a redundant function call and reducing code duplication. Overall, this is a great improvement.

Comment on lines +57 to +58
let resolveSignal: (() => void) | undefined;
let signalPromise = new Promise<void>((r) => (resolveSignal = r));
Copy link
Contributor

Choose a reason for hiding this comment

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

medium

The logic to create a new signal promise is duplicated here and again on line 97. To improve maintainability and reduce duplication, consider extracting this into a helper function. This also makes the code more self-documenting by giving a name to the operation.

You would then also replace the code on line 97 with signalPromise = createSignalPromise();.

Suggested change
let resolveSignal: (() => void) | undefined;
let signalPromise = new Promise<void>((r) => (resolveSignal = r));
let resolveSignal: (() => void) | undefined;
const createSignalPromise = () => new Promise<void>((r) => (resolveSignal = r));
let signalPromise = createSignalPromise();

Comment on lines 104 to 107
} else {
// After a task finishes, check for new work
this.processLoop(executingPromises, signal);
this.processLoop(executingPromises, notify, signal);
}
Copy link
Contributor

Choose a reason for hiding this comment

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

medium

This else block containing a call to processLoop appears to be redundant. The finally block of each task promise (lines 150-156) already calls processLoop when a task completes to immediately schedule new work. The notify() call then wakes up this main loop.

Since the work scheduling is already handled in the finally block, this second call is unnecessary. Removing this else block will simplify the logic without affecting functionality.

@thalesraymond thalesraymond deleted the bolt/optimize-workflow-executor-9498767348345128190 branch February 6, 2026 01:28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant