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
@@ -0,0 +1,3 @@
## 2025-02-09 - Time-Series O(log n) Clipping Optimization
**Learning:** The codebase fetches historical OHLCV time-series data which is strictly ordered by time (chronological). The `clipBars` function used `Array.prototype.filter` to restrict future bars in backtesting mode, which is an O(n) operation that inspects every element in a potentially long array.
**Action:** Created and used `findLastBarIndex(bars, targetTime)`, a binary search algorithm, to reduce the clipping complexity from O(n) to O(log n) followed by an array slice. This pattern should be considered whenever dealing with chronologically sorted arrays in the codebase.
6 changes: 5 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,11 @@
"pnpm": {
"onlyBuiltDependencies": [
"better-sqlite3"
]
],
"overrides": {
"undici": ">=7.28.0 <8.0.0",
"ws": ">=8.21.0"
}
},
"packageManager": "pnpm@10.33.2+sha512.a90faf6feeab71ad6c6e57f94e0fe1a12f5dcc22cd754db40ae9593eb6a3e0b6b12e3540218bb37ae083404b1f2ce6db2a4121e979829b4aff94b99f49da1cf8"
}
30 changes: 14 additions & 16 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

28 changes: 27 additions & 1 deletion src/data/sources/dnsePublic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,14 +61,40 @@ export function seriesToBars(s: OhlcvSeries): Bar[] {
return out;
}

/**
* O(log n) binary search to find the last bar at or before a target time.
* Assumes the bars array is chronologically sorted by time.
*/
export function findLastBarIndex(bars: Bar[], targetTime: number): number {
let low = 0;
let high = bars.length - 1;
let ans = -1;

while (low <= high) {
const mid = Math.floor((low + high) / 2);
if (bars[mid].time <= targetTime) {
ans = mid;
low = mid + 1; // Look for a later time that is still <= targetTime
} else {
high = mid - 1;
}
}
return ans;
}

function clipBars(bars: Bar[]): Bar[] {
// Clip when an as-of clock is active (ALS or module override). When neither
// is set, fall through unchanged — DNSE only returns historical data anyway.
const hasOverride =
asOfClock.getStore()?.asOfSec != null || isAsOfOverridden();
if (!hasOverride) return bars;
const asOf = nowSec();
return bars.filter((b) => b.time <= asOf);

// Performance optimization: use O(log n) binary search instead of O(n) filter
// since the time-series data is already chronologically sorted.
const lastIdx = findLastBarIndex(bars, asOf);
if (lastIdx === -1) return [];
return bars.slice(0, lastIdx + 1);
}

export async function getStockOhlcv(
Expand Down
Loading