Skip to content

⚡ Bolt: Replace O(n) backtest array filtering with O(log n) binary search#63

Draft
toreleon wants to merge 2 commits into
masterfrom
bolt-binary-search-opt-7119260057156216071
Draft

⚡ Bolt: Replace O(n) backtest array filtering with O(log n) binary search#63
toreleon wants to merge 2 commits into
masterfrom
bolt-binary-search-opt-7119260057156216071

Conversation

@toreleon

Copy link
Copy Markdown
Owner

💡 What:
Added a binary search utility function findLastBarIndex and replaced occurrences of Array.prototype.filter(b => b.time <= asOf) with it when querying historical prices inside the backtest runner loop. Also updated clipBars to use .slice() combined with the binary search index.

🎯 Why:
During backtesting simulations, the functions priceOverride and vnindexAt evaluate closures at every tick (interval turn) in a loop for multiple tickers. Previously, both were using an O(n) Array.prototype.filter() over long, chronologically-sorted price arrays simply to find the most recent bar prior to asOf. This constantly allocated large intermediate arrays and re-evaluated the entire dataset, severely degrading performance. Because market timeseries data is strictly chronologically ordered, we can leverage O(log n) binary search bounds-checking instead.

📊 Impact:
Replaces an O(n) filter and array allocation with an O(log n) integer search and direct indexing lookup on hot paths. This should yield measurable CPU time improvements and reduced memory churn when backtesting across long date ranges with many 30m interval turns.

🔬 Measurement:
Run a heavy backtest session (/backtest from the TUI spanning several months) and compare execution times or memory profiles. Alternatively, execute unit tests via pnpm test to verify correctness is preserved.


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

…st lookups

Replaces O(n) `Array.prototype.filter()` array allocations with an O(log n)
binary search utility `findLastBarIndex` when looking up historical time
series data in the hot loop of 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.

…st lookups

Replaces O(n) `Array.prototype.filter()` array allocations with an O(log n)
binary search utility `findLastBarIndex` when looking up historical time
series data in the hot loop of the backtest runner.

Also includes package upgrades for `undici` and `ws` to resolve
high-severity vulnerability audit failures.

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