Skip to content

⚡ Bolt: O(n) filter to O(log n) binary search optimization in time series lookups#73

Draft
toreleon wants to merge 3 commits into
masterfrom
perf/binary-search-filter-4866413359383505278
Draft

⚡ Bolt: O(n) filter to O(log n) binary search optimization in time series lookups#73
toreleon wants to merge 3 commits into
masterfrom
perf/binary-search-filter-4866413359383505278

Conversation

@toreleon

@toreleon toreleon commented Jul 7, 2026

Copy link
Copy Markdown
Owner

💡 What: Implemented a reusable findLastBarIndex binary search function in src/data/sources/dnsePublic.ts and refactored clipBars, vnindexAt, and priceOverride to use it.

🎯 Why: During backtests, the runBacktestSession function iterates over many intervals. Within each iteration, it was calling Array.prototype.filter on large historical arrays (vnindex and bars[sym]) to slice out data up to the current timestamp. This incurred an O(n) overhead (and unnecessary array allocations) per turn. Since these series are chronologically sorted, this lookup is a perfect candidate for O(log n) binary search.

📊 Impact: Massive reduction in time complexity inside the hottest paths of the backtest runner. A standalone node test of Array.filter vs custom binary search against arrays of size 100,000 for 10,000 iterations ran ~1400x faster (~19.3 seconds vs ~14 ms). Less garbage collection pausing due to reduced array mutation.

🔬 Measurement: Run a local simulation or long range backtest and observe the time to complete the loop logic; additionally verify with pnpm test and pnpm typecheck to ensure the logic behaves identically.


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

Replaces O(n) Array.prototype.filter calls inside high-frequency backtest loops with an O(log n) binary search utility, significantly reducing compute time during simulations.

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 7, 2026 16:05
Updated undici, cheerio, and added ws to resolve dependency audit vulnerabilities causing CI failures while keeping existing optimizations.

Co-authored-by: toreleon <42534763+toreleon@users.noreply.github.com>
…g types

Updated undici (to 7.28.0), cheerio, and added ws explicitly to resolve dependency audit vulnerabilities causing CI failures, while ensuring tests (`CacheStorage`) continue passing by avoiding undici 8.x.

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