Skip to content
Closed
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
Expand Up @@ -23,3 +23,6 @@
## 2026-09-19 - Avoid chained array methods before Set initialization
**Learning:** Chaining `.map().filter()` inside a `new Set()` constructor creates multiple intermediate arrays, causing unnecessary memory allocation and garbage collection overhead in hot paths (like ticker normalization during discovery).
**Action:** Replace chained array methods with a direct indexed `for` loop that transforms, validates, and `.add()`s directly to a freshly initialized `Set`.
## 2026-10-03 - [Optimized array-to-Set conversion in hot paths]
**Learning:** Chained array methods like `.map().slice()` followed by array-to-Set conversion create substantial intermediate array allocations and increase GC pressure in tight loops (such as backtesting engines).
**Action:** When extracting bounded, deduplicated subsets, avoid `Array.from(new Set(arr.map(...))).slice()`. Instead, initialize an empty `Set` and `Array` to track uniqueness and use a bounded `for` loop to iteratively append to the array with an early-exit length condition.
29 changes: 25 additions & 4 deletions src/agent/backtestRunner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -347,10 +347,31 @@ 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: Removed .map().slice() array-to-Set conversion chain to avoid allocating large intermediate arrays
const snapshot = await broker.snapshot();
const seen = new Set<string>();
const tickers: string[] = [];

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

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

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