Repository navigation
Description
Source: benchmarks/OPTIMIZATION_ROADMAP.md rank 2. Estimator PCA, operation fit, substrate python-bound, score 74.1, disposition rewrite first, observed Flow speedup 5.55×.
Source-level bottleneck audit
The current pca_fit implementation has several avoidable costs before changing the mathematical algorithm at all:
- Mean calculation allocates a temporary
f32[X.rows]column for every feature, copies the whole column throughmatrix_at, callsarray_mean, then frees it. Compute column sums directly from row-majorX.datainstead. - Covariance construction evaluates every
(j1, j2)pair even though the covariance matrix is symmetric. Compute one triangle and mirror it, cutting the dominant feature-pair accumulation work roughly in half. - Covariance and power-iteration loops route each element through
matrix_at/matrix_set; use contiguous row-major indexing in these internal hot loops while retaining the public Matrix representation. - Every one of the 100 power iterations allocates and frees
new_vec. Allocate a reusable work vector once per component and swap/copy it without heap traffic inside the iteration loop. - The Rayleigh quotient currently allocates another temporary vector after power iteration. Reuse the same work buffer.
These changes should preserve the current covariance + power-iteration + deflation semantics exactly. Do not replace the eigensolver in the first optimization pass; that would confound algorithmic parity with allocation/cache improvements.
Correctness invariants
The canonical Iris PCA state is already tightly instrumented and must remain within its existing contract: total explained-variance-ratio score, both explained-variance ratios, singular values, reconstruction MSE, and both components after sign alignment. PR #331 also exposes these as persistent learned-state diagnostics, so any state regression will remain visible independently of runtime.
Benchmark acceptance
Add a focused old-vs-new pca_fit A/B regression on deterministic matrices covering both tall/narrow and wider shapes. Verify the fitted mean, explained variance, explained-variance ratio and sign-aligned components before timing. Then require the canonical 19-row parity suite and scaled self-baseline gate to remain green. Update roadmap evidence with measured fit latency before closing.
The first implementation pass should therefore be allocation/cache/symmetry only; a later pass can evaluate BLAS/eigensolver replacement separately if profiling still attributes meaningful time to covariance/eigendecomposition.
Activity