Skip to content

Comments

Bolt: Optimized Mel Filterbank Matrix Multiplication#4

Open
ysdede wants to merge 1 commit intomainfrom
bolt-mel-filterbank-optimization-16003308894749395639
Open

Bolt: Optimized Mel Filterbank Matrix Multiplication#4
ysdede wants to merge 1 commit intomainfrom
bolt-mel-filterbank-optimization-16003308894749395639

Conversation

@ysdede
Copy link
Owner

@ysdede ysdede commented Feb 12, 2026

What:
Precomputed the start and end indices (_fbStart, _fbEnd) for each row in the Mel filterbank matrix in the MelSpectrogram constructor. Updated computeRawMel to use these bounds in the inner loop instead of iterating over all frequency bins.

Why:
The Mel filterbank is a sparse, triangular matrix. Iterating over all 257 frequency bins (for 512 FFT) for every Mel bin involves many multiplications by zero. By skipping these zeros, we significantly reduce the number of floating-point operations.

Impact:

  • Processing 60s audio: 834ms -> 232ms (~3.6x faster)
  • Realtime factor: 71.9x -> 258.5x

Measurement:
Ran bench.js (created temporarily) which processes 60s of random noise 10 times. Verified correctness with existing tests, including ONNX numerical cross-validation.


PR created automatically by Jules for task 16003308894749395639 started by @ysdede

Summary by CodeRabbit

  • Documentation

  • Added documentation on Mel filterbank sparsity optimization strategies and performance improvements in audio processing.

  • Refactor

  • Optimized Mel spectrogram computation by leveraging sparsity patterns in filterbank matrices, achieving approximately 3.6x performance improvement for audio processing without API changes.

Precomputes the start and end indices of non-zero values in the Mel filterbank matrix to avoid multiplying by zero. This reduces the complexity of the inner loop from O(nFreqBins) to O(filterWidth), resulting in a ~3.6x speedup (834ms -> 232ms for 60s audio).

Co-authored-by: google-labs-jules[bot] <161369871+google-labs-jules[bot]@users.noreply.github.com>
@coderabbitai
Copy link

coderabbitai bot commented Feb 12, 2026

📝 Walkthrough

Walkthrough

Adds documentation and implements a sparsity optimization for Mel filterbank computation. The optimization precomputes the non-zero range boundaries for each Mel filter during initialization, then constrains the inner loop during computation to skip zero coefficients, achieving approximately 3.6x performance improvement for audio processing.

Changes

Cohort / File(s) Summary
Documentation
.jules/bolt.md
New Markdown document explaining the Mel filterbank sparsity optimization strategy and its performance benefits (~3.6x speedup).
Mel Filterbank Optimization
src/mel.js
Introduced precomputed _fbStart and _fbEnd Int32Array fields to store non-zero coefficient ranges for each Mel bin; populated during initialization by scanning the filterbank; modified the dot-product loop in computeRawMel to use bounded iteration instead of scanning all frequency bins.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Poem

🐰 A sparse trick, so clever and clean,
Mel filters now faster than ever seen,
We skip what is zero with mathematical grace,
And race through the spectrum at triple the pace! ✨

🚥 Pre-merge checks | ✅ 4
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title accurately describes the main change: optimizing Mel filterbank matrix multiplication through sparse matrix techniques (precomputing non-zero bounds).
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Merge Conflict Detection ✅ Passed ✅ No merge conflicts detected when merging into main

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing touches
  • 📝 Generate docstrings
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch bolt-mel-filterbank-optimization-16003308894749395639

No actionable comments were generated in the recent review. 🎉

🧹 Recent nitpick comments
src/mel.js (1)

352-357: Hoist _fbStart / _fbEnd out of the hot loop for consistency.

Every other instance property used in the per-frame loop is destructured once on lines 344-345. this._fbStart and this._fbEnd are accessed inside the nFrames × nMels inner loop, adding repeated property lookups.

♻️ Suggested change
-    const { hannWindow: window, melFilterbank: fb, nMels, twiddles: tw, nFft, nFreqBins, hopLength, logZeroGuard } = this;
+    const { hannWindow: window, melFilterbank: fb, nMels, twiddles: tw, nFft, nFreqBins, hopLength, logZeroGuard, _fbStart: fbStart, _fbEnd: fbEnd } = this;
 
     for (let t = 0; t < nFrames; t++) {
       ...
       for (let m = 0; m < nMels; m++) {
         let melVal = 0;
         const fbOff = m * nFreqBins;
-        const start = this._fbStart[m];
-        const end = this._fbEnd[m];
+        const start = fbStart[m];
+        const end = fbEnd[m];
         for (let k = start; k < end; k++) melVal += powerBuf[k] * fb[fbOff + k];
.jules/bolt.md (1)

1-3: Documentation accurately captures the optimization rationale.

Minor nit: the date header says "2024-05-22" while this PR is from February 2026. Worth correcting if this serves as a changelog.


Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@ysdede ysdede changed the title ⚡ Bolt: Optimized Mel Filterbank Matrix Multiplication Bolt: Optimized Mel Filterbank Matrix Multiplication Feb 17, 2026
Repository owner deleted a comment from google-labs-jules bot Feb 20, 2026
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