perf(fft): cache FFT instances#377
Merged
Merged
Conversation
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.
Codecov Report❌ Patch coverage is
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. 🚀 New features to boost your workflow:
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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).