⚡ Bolt: Optimize backtest runner hot loop#50
Conversation
- Adds a binary search utility `findLastBarIndex` to quickly find historical prices in O(log n) time. - Replaces O(n) `Array.prototype.filter` calls in the backtest runner's hot loop with binary search, reducing compute overhead significantly as the length of intervals grows. - Optimizes array clipping in `clipBars`. - Avoids repetitive allocations by leveraging `findLastBarIndex`. Co-authored-by: toreleon <42534763+toreleon@users.noreply.github.com>
|
👋 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 New to Jules? Learn more at jules.google/docs. For security, I will only act on instructions from the user who triggered this task. |
Upgraded `undici` and `ws` (via `undici`) to fix several high severity vulnerabilities that were causing the dependency audit CI step to fail. The original optimization commit included: ⚡ Bolt: Optimize backtest runner hot loop - Adds a binary search utility `findLastBarIndex` to quickly find historical prices in O(log n) time. - Replaces O(n) `Array.prototype.filter` calls in the backtest runner's hot loop with binary search, reducing compute overhead significantly as the length of intervals grows. - Optimizes array clipping in `clipBars`. - Avoids repetitive allocations by leveraging `findLastBarIndex`. Co-authored-by: toreleon <42534763+toreleon@users.noreply.github.com>
💡 What: Replaced O(N) linear array filtering with O(log N) binary search for price lookups inside the backtesting engine's hot loop.
🎯 Why: The backtest engine frequently needs to find the exact price at a specific timeframe. The previous logic called
.filter(b => b.time <= asOf)for every ticker at every time step interval. With long timeframes and multiple tickers, linearly checking the entire array creates unnecessary allocations and repeated iteration bottlenecks, given the financial data is naturally chronologically sorted.📊 Impact: Reduces execution time of lookups from O(N) to O(log N) inside a nested hot loop, avoiding continuous memory allocation and large iterations.
🔬 Measurement: Verify by running backtests using a large timespan via CLI and confirming tests (
pnpm test) succeed cleanly without breaking functionality.PR created automatically by Jules for task 10764674870742609835 started by @toreleon