Skip to content
Merged
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
6 changes: 6 additions & 0 deletions .jules/bolt.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,9 @@
**Learning:** When generating a bounded, deduplicated subset (Set) from an array in hot paths, avoid array method chains like `Array.from(new Set([...arr1, ...arr2.map(f)])).slice(0, N)`. This causes several intermediate array allocations that create significant GC pressure during long loops (e.g., historical backtests).
**Action:** Use an empty `Set` along with bounded `for` loops and a results array that implement an early exit (`tickers.length < maxCandidates`) to directly stream unique items. This processes fewer elements and generates zero intermediate arrays.

## 2024-05-15 - DNSE API returns null instead of empty arrays
**Learning:** The DNSE API (entrade.com.vn) returns `{ t: null, c: null }` instead of `{ t: [], c: [] }` when there is no data (e.g. for weekends/holidays). Attempting to read `.length` on `t` throws a TypeError inside `seriesToBars` that can abort the entire backtest runner.
**Action:** Always verify if API arrays from external sources exist (`if (!s.t) return []`) before iterating, even if the TypeScript interface implies they are arrays.
## 2024-05-15 - Fast cache keys using integer math
**Learning:** Calling `new Date().toISOString().slice()` in hot paths (like `cached()` key generation during a backtest loop) adds up significantly (~1ms per thousand calls).
**Action:** When grouping cache keys by day, use fast integer math `Math.floor(unixSecs / 86400)` instead of string-based date allocations to maintain identical semantics while avoiding object instantiation.
5 changes: 4 additions & 1 deletion src/data/cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,10 @@ export function resetCacheStats() {
*/
function namespaced(key: string): string {
if (asOfClock.getStore()?.asOfSec != null || isAsOfOverridden()) {
const day = new Date(nowSec() * 1000).toISOString().slice(0, 10);
// ⚡ Bolt: Optimize cache key generation by replacing slow Date/String
// allocations (new Date(...).toISOString().slice(0, 10)) with integer math,
// grouping by UTC day since epoch to maintain the same invalidation semantics.
const day = Math.floor(nowSec() / 86400);
return `asof=${day}|${key}`;
}
return key;
Expand Down
1 change: 1 addition & 0 deletions src/data/sources/dnsePublic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ async function fetchOhlcs(
}

export function seriesToBars(s: OhlcvSeries): Bar[] {
if (!s.t) return [];
const out: Bar[] = [];
for (let i = 0; i < s.t.length; i++) {
if (s.c[i] == null) continue; // skip empty intraday slots
Expand Down
Loading