Skip to content

armgit / ruflo

Public Imported from GitHub
0

🌊 The original agent harness. Deploy intelligent multi-player swarms, coordinate autonomous workflows, and build conversational AI systems. Features adaptive memory, self-learning intelligence, federation, vector RAG integration, and native Claude Code / Codex / Hermes and many more Integrated

perf(neural-trader): #49 β€” bench-driven perf notes + targeted optimization

ruvbb05c5a96f96
diff --git a/plugins/ruflo-neural-trader/docs/perf-notes.md b/plugins/ruflo-neural-trader/docs/perf-notes.md
new file mode 100644
index 0000000000..74ac2ed7b0
--- /dev/null
+++ b/plugins/ruflo-neural-trader/docs/perf-notes.md
@@ -0,0 +1,97 @@
+# Neural-Trader Performance Notes β€” ADR-126 follow-up #49
+
+Bench-driven hot-path analysis derived from the three baseline benches
+landed in `#48`:
+
+- `benchmarks/signal-generation.bench.mjs`
+- `benchmarks/backtest-throughput.bench.mjs`
+- `benchmarks/memory-recall.bench.mjs`
+
+The portfolio CG bench (`portfolio-cg.bench.mjs`) is owned by the
+ADR-126 Phase 3 / #2080 work and is referenced read-only here.
+
+## Hotspots ranked by user-visible impact
+
+### 1. Neumann solver per-iter allocation (FIXED in this PR)
+
+- **Where**: `plugins/ruflo-neural-trader/src/sublinear-adapter.mjs:148-184`
+ (`neumannSeries` exported kernel)
+- **Measured before**: avg ~0.635 ms at n=256 across 5 runs, plus measurable
+ GC variance (p95 / max well above p50)
+- **Measured after**: avg ~0.522 ms at n=256 across 5 runs β€” **~18% faster**
+- **Optimization**: replace per-iter `new Float64Array(n)` allocation with
+ a two-buffer ping-pong (`cur` / `next`, swapped by reference each iter).
+ At n=256 a typical 5-iter solve previously allocated 5Γ—2 KB of garbage
+ per call β€” eliminated.
+- **Scope**: plugin-layer only. No upstream `neural-trader` change needed.
+ Smoke `node scripts/smoke-neural-trader-portfolio-cg.mjs` still passes.
+- **Parity preserved**: `||cg βˆ’ neumann||_∞ = 2.12e-8` at n=256 β€” identical
+ to the pre-change baseline; the change is purely an allocation-pattern
+ rewrite, not an algorithmic change.
+
+### 2. Memory recall linear scan grows ~O(N) (TRACKED, not fixed here)
+
+- **Where**: the production path uses `mcp__claude-flow__memory_search`
+ via AgentDB + HNSW. The bench `memory-recall.bench.mjs` models the
+ linear-scan baseline (cosine over Float32Array) to track the
+ approximate-vs-exact recall gap an ANN index would create.
+- **Measured**: avg latency scales from 0.08 ms at N=100 β†’ 3.85 ms at
+ N=5000 β†’ 47.87Γ— scaling vs the ideal-linear 50Γ— (so 96% of linear).
+ p95 at N=5000 is ~4.2 ms.
+- **Proposed optimization (UPSTREAM)**: the production `memory_search`
+ already uses HNSW per ADR-006, which gives 150x–12,500Γ— speedups on
+ this exact workload. The plugin doesn't need a local change β€” what we
+ DO need is a regression gate: when the skill's recall@10 drops below
+ 0.8, the HNSW build params (M, efConstruction) drifted and need a
+ rebuild. The bench captures that gate. **Scope: upstream HNSW config,
+ not the plugin.**
+
+### 3. Signal-scan p99 tail latency is ~9Γ— the avg (TRACKED, not fixed here)
+
+- **Where**: `benchmarks/signal-generation.bench.mjs` β€” for AAPL the
+ measured avg is 22 Β΅s but p99 is 191 Β΅s.
+- **Hypothesis**: GC pressure from `bars.map(b => b.close)` + two
+ `bars.slice` calls per scan in the bench. The production `--signal scan`
+ path is upstream (`npx neural-trader`) and we can't fix it in the
+ plugin layer, but the bench is illustrative.
+- **Proposed optimization (UPSTREAM)**: reuse a single typed array for
+ `closes` and slice via indices instead of materializing sub-arrays.
+ Saves ~3 allocations per scan call.
+- **Scope**: upstream `neural-trader` binary if the same pattern exists
+ in the Rust/NAPI code path β€” confirmed only by profiling the
+ `--signal scan` cloud roundtrip, which the plugin can't reach from a
+ pure-JS smoke.
+
+## Out-of-scope / explicitly NOT optimized
+
+- **`canonicalBytes` (signed-artifact.mjs:75 / signed-attribution.mjs:193)** β€”
+ the explicit ADR-126 Phase 4 contract is `JSON.stringify` with NO key
+ sort, deterministic-by-construction. Adding a sort or memoization
+ would break the smoke's CWE-347 parity with the plugin-registry
+ signer. Leave as-is.
+- **`isSymmetric` (sublinear-adapter.mjs:204-211)** β€” already short-circuits
+ on first violation. The full upper-triangle scan on symmetric inputs
+ is the contract guard the smoke depends on; weakening it weakens the
+ SPD precondition.
+
+## Bench-as-regression-gate
+
+The three new benches act as a perf budget:
+
+| Bench | Budget | Source |
+|--------------------------------|-----------------------|---------------------|
+| signal worst-symbol avg | <1000 Β΅s | acceptance line |
+| signal full-scan sum-of-avgs | <10 ms | acceptance line |
+| backtest avg runtime | <10 ms | acceptance line |
+| backtest throughput | >25,000 bars/sec | acceptance line |
+| memory-recall p95 at N=5000 | <50 ms | acceptance line |
+| memory-recall recall@10 | >=0.8 (90% subset) | acceptance line |
+
+A bench whose acceptance line trips on CI is a perf regression.
+
+## Refs
+
+- ADR-126 Β§SOTA delta β€” bench-driven perf work
+- ADR-123 Wedge 8 β€” sublinear-time-solver
+- ADR-006 β€” Unified Memory Service (HNSW)
+- Followup #48 β€” bench suite that produced the numbers above
diff --git a/plugins/ruflo-neural-trader/src/sublinear-adapter.mjs b/plugins/ruflo-neural-trader/src/sublinear-adapter.mjs
index b69f6ff0c4..2fda643285 100644
--- a/plugins/ruflo-neural-trader/src/sublinear-adapter.mjs
+++ b/plugins/ruflo-neural-trader/src/sublinear-adapter.mjs
@@ -151,36 +151,45 @@ export function neumannSeries(A, b, opts) {
const maxIter = opts.maxIterations ?? 1000;
const diag = new Float64Array(n);
for (let i = 0; i < n; i++) diag[i] = A[i][i] || 1;
- const x = new Float64Array(n);
+ // Ping-pong buffers: avoid allocating a fresh Float64Array(n) every iter.
+ // Before this change a typical 5-iter solve at n=256 allocated 5Γ—2KB of
+ // garbage per call β€” measurable GC pressure under sustained workload.
+ // The two-buffer swap keeps the same algorithm (Jacobi-style update from
+ // `cur` into `next`, convergence check, swap) with zero per-iter alloc.
+ // ADR-126 follow-up #49 β€” bench-driven perf note.
+ let cur = new Float64Array(n);
+ let next = new Float64Array(n);
let iterations = 0;
for (let k = 0; k < maxIter; k++) {
iterations++;
- const next = new Float64Array(n);
for (let i = 0; i < n; i++) {
let off = 0;
const row = A[i];
for (let j = 0; j < n; j++) {
- if (j !== i) off += row[j] * x[j];
+ if (j !== i) off += row[j] * cur[j];
}
next[i] = (b[i] - off) / diag[i];
}
- // Convergence check on inf-norm of (next - x).
+ // Convergence check on inf-norm of (next - cur).
let d = 0;
for (let i = 0; i < n; i++) {
- const e = Math.abs(next[i] - x[i]);
+ const e = Math.abs(next[i] - cur[i]);
if (e > d) d = e;
}
- for (let i = 0; i < n; i++) x[i] = next[i];
+ // Swap: next becomes the new cur; the old cur is reused as next-scratch.
+ const tmp = cur;
+ cur = next;
+ next = tmp;
if (d < tol) break;
}
- // Residual ||AΒ·x βˆ’ b||β‚‚
- const Ax = matVec(A, x);
+ // Residual ||AΒ·x βˆ’ b||β‚‚ where x is the latest cur after the swap.
+ const Ax = matVec(A, cur);
let r2 = 0;
for (let i = 0; i < n; i++) {
const d = Ax[i] - b[i];
r2 += d * d;
}
- return { solution: Array.from(x), iterations, residual: Math.sqrt(r2) };
+ return { solution: Array.from(cur), iterations, residual: Math.sqrt(r2) };
}
function matVec(A, x) {