Skip to content

⚡ Bolt: [performance improvement] Replace multiple reduce calls with single loop - #184

Draft
toreleon wants to merge 1 commit into
masterfrom
bolt-optimize-reduce-loops-17193101723706905739
Draft

toreleon wants to merge 1 commit into
masterfrom
bolt-optimize-reduce-loops-17193101723706905739

Conversation

@toreleon

Copy link
Copy Markdown
Owner

💡 What: Replaced sequential .reduce() passes over arrays like turnRows and equityRows with a single for loop in src/agent/backtestRunner.ts. Also replaced an object-accumulating .reduce() in src/tools/portfolio.ts with a for loop that updates local variables directly.

🎯 Why: Running multiple .reduce() passes traverses the same array multiple times (O(3N)), which adds significant iteration overhead on large arrays (like backtest turns). Similarly, accumulating into an object within a .reduce() causes unnecessary function instantiation and garbage collection overhead compared to a basic for loop with scalar mutations.

📊 Impact: Turns O(3N) multi-pass array iterations into O(N) operations in backtestRunner.ts. Reduces garbage collection pressure in portfolio.ts and backtestRunner.ts by avoiding object allocations and callback creation overhead during reductions.

🔬 Measurement: Run the test suite to ensure no regressions using pnpm test. Run a backtest using the TUI /backtest command to observe successful execution with lower memory allocation overhead.


PR created automatically by Jules for task 17193101723706905739 started by @toreleon

Refactored `src/agent/backtestRunner.ts` and `src/tools/portfolio.ts` to replace multiple sequential `.reduce()` calls and object-accumulating `.reduce()` calls with single, indexed `for` loops. This avoids multi-pass array iterations, lowers garbage collection overhead, and prevents unnecessary state accumulation into objects on hot paths like backtest summaries.

Co-authored-by: toreleon <42534763+toreleon@users.noreply.github.com>
@google-labs-jules

Copy link
Copy Markdown
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.

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