Skip to content

⚡ Bolt: Replace O(N) array filtering with O(log N) binary search for time-series data#72

Draft
toreleon wants to merge 3 commits into
masterfrom
bolt-optimize-array-filter-14981410524126896598
Draft

⚡ Bolt: Replace O(N) array filtering with O(log N) binary search for time-series data#72
toreleon wants to merge 3 commits into
masterfrom
bolt-optimize-array-filter-14981410524126896598

Conversation

@toreleon

@toreleon toreleon commented Jul 6, 2026

Copy link
Copy Markdown
Owner

💡 What: Introduced a reusable findLastBarIndex function to perform O(log N) binary search lookups on chronologically sorted Bar arrays, replacing Array.prototype.filter() operations in dnsePublic.ts and backtestRunner.ts.

🎯 Why: During backtesting, the application iterates over thousands of bars, and calling .filter() repeatedly on the full array inside the interval loop causes massive O(N) CPU overhead and unnecessary array allocations, degrading performance and increasing memory usage.

📊 Impact: Substantially speeds up backtesting executions by converting O(N * intervals) scanning + allocating to O(log N * intervals) lookups. Reduces garbage collection pressure by avoiding large array instantiations on every interval loop.

🔬 Measurement: Run a large backtest before and after this change. Notice reduced time per turn and a smaller memory footprint in the CLI output metrics. Checked that pnpm test and pnpm typecheck still pass to verify accuracy.


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

…eries lookups

Replaced `array.filter(b => b.time <= asOf)` in `backtestRunner.ts` and `dnsePublic.ts` with a custom `findLastBarIndex` binary search, which takes advantage of the fact that market data arrays are already chronologically sorted. This drastically reduces CPU overhead and memory allocations inside hot loops like the backtest runner.

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.

google-labs-jules Bot and others added 2 commits July 6, 2026 15:30
…eries lookups & fix dependencies vulnerabilities

Replaced `array.filter(b => b.time <= asOf)` in `backtestRunner.ts` and `dnsePublic.ts` with a custom `findLastBarIndex` binary search, which takes advantage of the fact that market data arrays are already chronologically sorted. This drastically reduces CPU overhead and memory allocations inside hot loops like the backtest runner.
Also bumped `ws` to 8.21.0 and `undici` to 8.7.0 to fix security vulnerabilities flagged by `pnpm audit`.

Co-authored-by: toreleon <42534763+toreleon@users.noreply.github.com>
…eries lookups & fix dependencies vulnerabilities

Replaced `array.filter(b => b.time <= asOf)` in `backtestRunner.ts` and `dnsePublic.ts` with a custom `findLastBarIndex` binary search, which takes advantage of the fact that market data arrays are already chronologically sorted. This drastically reduces CPU overhead and memory allocations inside hot loops like the backtest runner.
Also bumped `ws` to 8.21.0 and `undici` to 6.27.0 to fix security vulnerabilities flagged by `pnpm audit`.

Co-authored-by: toreleon <42534763+toreleon@users.noreply.github.com>
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