Skip to content

perf(fft): cache FFT instances#377

Merged
lpatiny merged 2 commits into
mainfrom
perf/reimfft-cache-cap
Jun 22, 2026
Merged

perf(fft): cache FFT instances#377
lpatiny merged 2 commits into
mainfrom
perf/reimfft-cache-cap

Conversation

@lpatiny

@lpatiny lpatiny commented Jun 22, 2026

Copy link
Copy Markdown
Member

Bound the per-size FFT cache so transforming many different sizes cannot
grow it without limit: once it reaches 10 entries it is cleared
completely rather than tracking insertion order, since distinct sizes
are rare and the common single-size workload never hits the cap.

Rework the benchmark to seed input deterministically with ml-xsadd and
add an explicit before/after comparison for the 64k FFT, confirming the
cached instance (~1.16 ms) is ~2.3x faster than rebuilding it per call
(~2.72 ms).

lpatiny added 2 commits June 22, 2026 19:40
Bound the per-size FFT cache so transforming many different sizes cannot
grow it without limit: once it reaches 10 entries it is cleared
completely rather than tracking insertion order, since distinct sizes
are rare and the common single-size workload never hits the cap.

Rework the benchmark to seed input deterministically with ml-xsadd and
add an explicit before/after comparison for the 64k FFT, confirming the
cached instance (~1.16 ms) is ~2.3x faster than rebuilding it per call
(~2.72 ms).
Extract the per-size FFT instance cache into a shared module
(utils/fftCache.ts) backed by an encapsulated singleton, and route every
fft.js call site through it: reimFFT, reimArrayFFT, reimMatrixFFT,
reimMatrixFFTByColumns, matrixHilbertTransform and xHilbertTransform.
Previously only reimFFT cached its instance and the others rebuilt the
size-dependent lookup tables on every call; now `new FFT(size)` happens
in exactly one place and all functions reuse the cached instances.

Add public `clearFFTCache()` to release the cached instances and
`setFFTCacheMaxSize()` to tune the bound (default 10, clear-when-full).
Keep `getFFT` internal so fft.js does not leak into the public API.

Add a deterministic xHilbertTransform benchmark (≈1.88x faster, output
bit-identical) alongside the existing reimFFT one.
@lpatiny lpatiny changed the title perf(fft): cache FFT sizes up to 10 perf(fft): cache FFT instances Jun 22, 2026
@codecov

codecov Bot commented Jun 22, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 93.75000% with 2 lines in your changes missing coverage. Please review.
✅ Project coverage is 97.12%. Comparing base (c5468e2) to head (55699cf).

Files with missing lines Patch % Lines
src/utils/fftCache.ts 92.30% 2 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main     #377      +/-   ##
==========================================
- Coverage   97.15%   97.12%   -0.04%     
==========================================
  Files         206      207       +1     
  Lines        4115     4141      +26     
  Branches     1031     1036       +5     
==========================================
+ Hits         3998     4022      +24     
- Misses        113      115       +2     
  Partials        4        4              

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@lpatiny lpatiny merged commit 9d84adf into main Jun 22, 2026
8 of 10 checks passed
@lpatiny lpatiny deleted the perf/reimfft-cache-cap branch June 22, 2026 20:06
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