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
4 changes: 4 additions & 0 deletions .jules/bolt.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,7 @@
**Learning:** In `src/tools/technical.ts`, the `indicatorsTool` used chained array methods like `.map()` and `.slice().map()` to extract closes and construct the resulting bars array. This created intermediate array allocations causing significant garbage collection overhead during hot-path technical indicator computation.
**Action:** Replaced the chained array methods with a single, highly optimized indexed `for` loop to reduce memory allocations and avoid GC pressure.

## 2026-10-04 - Optimize hot loop array to Set extraction
**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.

24 changes: 20 additions & 4 deletions src/agent/backtestRunner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -347,10 +347,26 @@ export async function runBacktestSession(
universe: BACKTEST_DISCOVERY_UNIVERSE,
limit: maxCandidates,
});
const held = (await broker.snapshot()).positions.map((p) => p.ticker);
const tickers = Array.from(
new Set([...held, ...discovery.candidates.map((c) => c.ticker)]),
).slice(0, maxCandidates);
// ⚡ Bolt: Use bounded Set and loop to avoid array allocations in hot loop
const snap = await broker.snapshot();
const seen = new Set<string>();
const tickers: string[] = [];

for (let i = 0; i < snap.positions.length && tickers.length < maxCandidates; i++) {
const t = snap.positions[i]!.ticker;
if (!seen.has(t)) {
seen.add(t);
tickers.push(t);
}
}

for (let i = 0; i < discovery.candidates.length && tickers.length < maxCandidates; i++) {
const t = discovery.candidates[i]!.ticker;
if (!seen.has(t)) {
seen.add(t);
tickers.push(t);
}
}

const decisions: FinalDecision[] = [];
for (const ticker of tickers) {
Expand Down
Loading