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
Expand Up @@ -41,3 +41,6 @@
## 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.
## 2026-10-18 - Single-Pass Loop Consolidation
**Learning:** In backtest generation and portfolio/account summarization (`src/agent/backtestRunner.ts`, `src/broker/fhsc.ts`), multiple sequential `.reduce()` operations on the same large arrays (`turnRows`, `equityRows`, `paymentAccounts`) were causing redundant $O(n)$ iteration cycles and GC overhead. Replacing them with single-pass `for...of` loops successfully aggregated multiple metrics in one pass, yielding a clear algorithmic improvement without severely impacting readability. However, merely swapping a single `.reduce()` with a `for` loop on small arrays constitutes a micro-optimization that degrades declarative readability with no measurable real-world speedup.
**Action:** When extracting multiple aggregates from an array, combine them into a single `for...of` loop. Do not blindly replace single `.reduce()` calls unless they are in proven hot paths or chained with other functional passes.
30 changes: 23 additions & 7 deletions src/agent/backtestRunner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -465,13 +465,29 @@ export async function runBacktestSession(
const totalReturn = (finalMtm / opts.initialCash - 1) * 100;
const benchReturn = (finalBench / opts.initialCash - 1) * 100;
let runningPeak = opts.initialCash;
const maxDD = equityRows.reduce((mn, r) => {
runningPeak = Math.max(runningPeak, r.mtm_vnd);
return Math.min(mn, r.mtm_vnd / runningPeak - 1);
}, 0);
const totalCost = turnRows.reduce((s, r) => s + (r.cost_usd ?? 0), 0);
const totalIn = turnRows.reduce((s, r) => s + (r.in_tokens ?? 0), 0);
const totalOut = turnRows.reduce((s, r) => s + (r.out_tokens ?? 0), 0);

// ⚡ Bolt: Consolidated multiple .reduce() passes over turn rows and equity rows
// into single-pass for...of loops to significantly reduce GC overhead
// and iteration cycles in the backtest reporting hot path.
let maxDD = 0;
for (const r of equityRows) {
if (r.mtm_vnd > runningPeak) {
runningPeak = r.mtm_vnd;
}
const dd = r.mtm_vnd / runningPeak - 1;
if (dd < maxDD) {
maxDD = dd;
}
}

let totalCost = 0;
let totalIn = 0;
let totalOut = 0;
for (const r of turnRows) {
totalCost += r.cost_usd ?? 0;
totalIn += r.in_tokens ?? 0;
totalOut += r.out_tokens ?? 0;
}

const summary: SummaryPayload = {
runId,
Expand Down
15 changes: 10 additions & 5 deletions src/broker/fhsc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -653,11 +653,16 @@ export class FHSCBroker implements Broker {
const account = accountResult.status === "fulfilled" ? accountResult.value : ({} as FhscSubAccount);
const portfolio = portfolioResults.flatMap((result) => (result.status === "fulfilled" ? result.value : []));
const positions = this.normalizePositions(portfolio);
const paymentCash = paymentAccounts.reduce(
(sum, account) => sum + numberOf(account.balance ?? account.total_balance),
0,
);
const blockedBalance = paymentAccounts.reduce((sum, account) => sum + numberOf(account.blocked_balance), 0);

// ⚡ Bolt: Consolidated two separate .reduce() passes into a single for...of loop
// to improve execution speed when aggregating sub-account balances.
let paymentCash = 0;
let blockedBalance = 0;
for (const pa of paymentAccounts) {
paymentCash += numberOf(pa.balance ?? pa.total_balance);
blockedBalance += numberOf(pa.blocked_balance);
}

return {
broker: NAME,
cashVnd:
Expand Down
Loading