Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .jules/bolt.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
## 2024-05-14 - Time-Series O(log n) Search Pattern
**Learning:** `src/agent/backtestRunner.ts` makes heavy use of `.filter((b) => b.time <= asOf)` to get the latest close price inside loop bounds, changing potentially O(1) loop lookups or O(log n) lookups into repetitive O(n) filtering over large arrays.
**Action:** Expose `findLastBarIndex` in `src/data/sources/dnsePublic.ts` and use binary search or track indices explicitly in tight loops.
## 2024-05-14 - Time-Series O(log n) Search Pattern
**Learning:** `src/data/sources/dnsePublic.ts` previously used a `.filter((b) => b.time <= asOf)` to clip historical arrays. This turned what should be a fast slice operation into an O(n) scan over entire time-series data.
**Action:** Used the already-available `findLastBarIndex` (which implements binary search) and `.slice()` to truncate the arrays in `O(log n)` time instead, massively speeding up lookups for arrays representing long timelines.
5 changes: 4 additions & 1 deletion src/data/sources/dnsePublic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,10 @@ function clipBars(bars: Bar[]): Bar[] {
asOfClock.getStore()?.asOfSec != null || isAsOfOverridden();
if (!hasOverride) return bars;
const asOf = nowSec();
return bars.filter((b) => b.time <= asOf);

// ⚑ Bolt: Optimize O(n) filter to O(log n) binary search for large time-series arrays
const idx = findLastBarIndex(bars, asOf);
return idx === -1 ? [] : bars.slice(0, idx + 1);
}

export async function getStockOhlcv(
Expand Down
Loading