Skip to content

Performance: Optimize FFT loops via interchange and local variable caching#160

Open
ysdede wants to merge 1 commit into
masterfrom
perf-fft-loop-interchange-9104419570519025766
Open

Performance: Optimize FFT loops via interchange and local variable caching#160
ysdede wants to merge 1 commit into
masterfrom
perf-fft-loop-interchange-9104419570519025766

Conversation

@ysdede
Copy link
Copy Markdown
Owner

@ysdede ysdede commented Apr 18, 2026

What changed
In src/mel.js, specifically the fft function for stages len=16..N, we performed loop interchange to swap the k and i loops so that the k loop is executed outside. Additionally, inside the inner i loop, we explicitly cache the elements accessed from the TypedArrays (re and im) into local variables.

Why it was needed
Profiling the FFT execution in V8 indicated a bottleneck inside the nested loop due to frequent and repeated property/index lookups into the tw.cos, tw.sin arrays, as well as the main re and im buffers. The twIdx depends purely on k and step, so hoisting it outside the inner loop reduces overhead.

Impact
Running a benchmark of 50,000 FFT computations over a 512-point random array in V8 showed processing times reduced from ~904ms to ~876ms. This is roughly a 3% performance improvement on the core transformation logic without altering any behavior or outputs.

How to verify

  1. Check the source logic to confirm it preserves mathematical equivalence (since indices generated in stages are strictly disjoint).
  2. Run npm install vitest && npm run test to guarantee all tests (especially mel_feature_cache.test.mjs and related integration tests) continue to pass.

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

Summary by Sourcery

Optimize FFT inner loops in mel.js to reduce TypedArray and twiddle-factor access overhead while preserving existing behavior.

Enhancements:

  • Reorder FFT loops to iterate over twiddle indices in the outer loop and data segments in the inner loop for better cache and access patterns.
  • Cache frequently accessed real/imaginary buffer values into local variables within the FFT inner loop to reduce repeated TypedArray lookups.

Documentation:

  • Document the FFT loop interchange and local-variable caching optimization in the performance notes (bolt.md), including observed V8 speedup and recommended pattern.

…ching

- Perform loop interchange in remaining stages (len=16..N) of `fft` to hoist array lookups for twiddle factors (`wCos`, `wSin`) out of the innermost loop.
- Cache TypedArray lookups (`re[q]`, `im[q]`, `re[p]`, `im[p]`) into local variables inside the innermost loop to avoid redundant memory reads.
- These changes yield roughly a 3% speedup in V8 without manual loop unrolling, and behavior remains completely identical.
@google-labs-jules
Copy link
Copy Markdown
Contributor

👋 Jules, reporting for duty! I'm here to lend a hand with this pull request.

When you start a review, I'll add a 👀 emoji to each comment to let you know I've read it. I'll focus on feedback directed at me and will do my best to stay out of conversations between you and other bots or reviewers to keep the noise down.

I'll push a commit with your requested changes shortly after. Please note there might be a delay between these steps, but rest assured I'm on the job!

For more direct control, you can switch me to Reactive Mode. When this mode is on, I will only act on comments where you specifically mention me with @jules. You can find this option in the Pull Request section of your global Jules UI settings. You can always switch back!

New to Jules? Learn more at jules.google/docs.


For security, I will only act on instructions from the user who triggered this task.

@coderabbitai
Copy link
Copy Markdown

coderabbitai Bot commented Apr 18, 2026

Warning

Rate limit exceeded

@ysdede has exceeded the limit for the number of commits that can be reviewed per hour. Please wait 42 minutes and 12 seconds before requesting another review.

Your organization is not enrolled in usage-based pricing. Contact your admin to enable usage-based pricing to continue reviews beyond the rate limit, or try again in 42 minutes and 12 seconds.

⌛ How to resolve this issue?

After the wait time has elapsed, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

We recommend that you space out your commits to avoid hitting the rate limit.

🚦 How do rate limits work?

CodeRabbit enforces hourly rate limits for each developer per organization.

Our paid plans have higher rate limits than the trial, open-source and free plans. In all cases, we re-allow further reviews after a brief timeout.

Please see our FAQ for further information.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

Run ID: cdfe2a21-1c5f-45bb-8846-0edae3caeff1

📥 Commits

Reviewing files that changed from the base of the PR and between 262e1f9 and fa96e91.

📒 Files selected for processing (2)
  • .jules/bolt.md
  • src/mel.js
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch perf-fft-loop-interchange-9104419570519025766

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.

Copy link
Copy Markdown

@sourcery-ai sourcery-ai Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey - I've reviewed your changes and they look great!


Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

Copy link
Copy Markdown

@gemini-code-assist gemini-code-assist Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request optimizes the FFT algorithm in src/mel.js by interchanging the inner loops to hoist twiddle factor lookups and caching TypedArray accesses into local variables to reduce overhead. Documentation in .jules/bolt.md was also updated to reflect these performance improvements. The review feedback suggests a further optimization to hoist the tw.cos and tw.sin property accesses outside the k loop to minimize property lookup overhead in the V8 engine.

Comment thread src/mel.js
Comment on lines +342 to +346
// Optimization: Swap inner loops (k and i) to hoist twiddle array lookups out of the innermost loop.
for (let k = 0; k < halfLen; k++) {
const twIdx = k * step;
const wCos = tw.cos[twIdx];
const wSin = tw.sin[twIdx];
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

While the loop interchange successfully hoists the twiddle index lookups out of the innermost i loop, the property accesses tw.cos and tw.sin are still performed in every iteration of the k loop. Hoisting these array references outside the k loop (but inside the len loop) will further reduce property lookup overhead in V8, which aligns with the performance goals of this PR.

    // Optimization: Swap inner loops (k and i) to hoist twiddle array lookups out of the innermost loop.
    const twCos = tw.cos;
    const twSin = tw.sin;
    for (let k = 0; k < halfLen; k++) {
      const twIdx = k * step;
      const wCos = twCos[twIdx];
      const wSin = twSin[twIdx];

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