Skip to content

refactor: cleanup, dedup, and optimize Rust solver#106

Merged
daharoni merged 1 commit into
mainfrom
refactor/rust-solver-cleanup
Mar 21, 2026
Merged

refactor: cleanup, dedup, and optimize Rust solver#106
daharoni merged 1 commit into
mainfrom
refactor/rust-solver-cleanup

Conversation

@daharoni
Copy link
Copy Markdown
Contributor

Summary

  • Correctness fix: Extract clamp_tau_rise() guard and apply in BandedAR2, fixing a case where banded and FFT engines disagreed for tau_rise ≈ tau_decay
  • 6 deduplication refactors: fft.rs forward/adjoint, banded.rs new/update, baseline EMA, kernel_est adjoint, biexp_fit golden-section, indeca interior_slice
  • 8 performance improvements: fused banded normalize, halved Lipschitz loop, lazy banded update, hoisted loop allocations, cached FenwickTree MSB, combined median+MAD, moved ownership instead of clone
  • 3 new filter-path tests covering HP+LP, HP-only, and LP-only paths in indeca::solve_trace
  • WASM binary shrunk 9KB (416→407 KB)

Files changed (14)

File Changes
kernel.rs Extract clamp_tau_rise(); halve compute_lipschitz loop
banded.rs Add tau guard; collapse update(); fuse normalize pass
fft.rs Extract convolve_impl() shared by forward/adjoint
lib.rs Extract baseline EMA + state_byte_len; lazy banded update
fista.rs Use extracted baseline EMA method
filter.rs Extract forward_fft_and_cache_power(); iterator-zip
baseline.rs Cache MSB in FenwickTree
kernel_est.rs Hoist allocs; extract adjoint_spikes_kernel()
biexp_fit.rs Extract golden_bracket(); sentinel(); has_fast_component(); fix comment
threshold.rs sort_unstable_by+total_cmp; move s_bin; fuse PVE
peak_seed.rs Combined median_and_mad()
indeca.rs Direct filter; interior_slice(); 3 new tests
js_indeca.rs Formatting only (rustfmt)
pkg/calab_solver_bg.wasm Rebuilt (-9KB)

Test plan

  • All 109 Rust tests pass (106 existing + 3 new)
  • WASM builds clean
  • cargo fmt --check passes
  • Verified all existing test assertions still align with refactored code

🤖 Generated with Claude Code

Correctness:
- Extract clamp_tau_rise() guard and apply in BandedAR2, fixing a
  case where banded and FFT engines disagreed for tau_rise ≈ tau_decay

Deduplication:
- fft.rs: extract convolve_impl() shared by forward/adjoint (~85% identical)
- banded.rs: collapse update() into *self = Self::new(...)
- lib.rs+fista.rs: extract compute_raw_baseline()+update_baseline_ema()
- kernel_est.rs: extract adjoint_spikes_kernel() (was in 2 places)
- biexp_fit.rs: extract golden_bracket() for 4 near-identical phases

Performance:
- banded.rs: fuse normalize pass (eliminate O(N) second pass on hot path)
- kernel.rs: compute_lipschitz iterates half frequencies + pre-casts kernel
- lib.rs: skip banded.update() in FFT mode (~4k trig evals per param change)
- kernel_est.rs: hoist z and v_f32 allocations outside loops
- baseline.rs: cache MSB in FenwickTree (avoid recompute per kth() call)
- filter.rs: extract forward_fft_and_cache_power(); iterator-zip loops
- peak_seed.rs: combined median_and_mad() saves 2 allocs + 1 sort per trace
- threshold.rs: move s_bin ownership instead of clone; fuse PVE loops

Cleanup:
- biexp_fit.rs: BiexpResult::sentinel(), has_fast_component(), derive Clone
- biexp_fit.rs: fix stale comment (0.2 → 0.15)
- threshold.rs: sort_unstable_by with total_cmp
- indeca.rs: apply filter directly instead of throwaway 1-iter FISTA
- indeca.rs: extract interior_slice() helper
- lib.rs: extract state_byte_len() for export/load

Tests: 109 pass (3 new filter-path coverage tests for indeca.rs)

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
@daharoni daharoni merged commit 5aa3ca0 into main Mar 21, 2026
2 checks passed
@daharoni daharoni deleted the refactor/rust-solver-cleanup branch March 21, 2026 14:36
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant