Performance: Optimize FFT stage inner loops via loop interchange and variable hoisting#159
Performance: Optimize FFT stage inner loops via loop interchange and variable hoisting#159ysdede wants to merge 1 commit into
Conversation
…y lookups and caching typed array accesses.
|
👋 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 New to Jules? Learn more at jules.google/docs. For security, I will only act on instructions from the user who triggered this task. |
|
Warning Rate limit exceeded
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 32 minutes and 49 seconds. ⌛ How to resolve this issue?After the wait time has elapsed, a review can be triggered using the 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 configurationConfiguration used: Path: .coderabbit.yaml Review profile: CHILL Plan: Pro Run ID: 📒 Files selected for processing (2)
✨ Finishing Touches🧪 Generate unit tests (beta)
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. Comment |
There was a problem hiding this comment.
Code Review
This pull request optimizes the FFT implementation in src/mel.js by interchanging nested loops to hoist twiddle factor lookups and caching TypedArray accesses into local variables to reduce overhead. These performance improvements are also documented in .jules/bolt.md. Feedback suggests further micro-optimizations, including hoisting the tw.cos and tw.sin property lookups outside the loops and refactoring the inner loop to use p as the iterator to eliminate redundant additions.
| for (let k = 0; k < halfLen; k++) { | ||
| const twIdx = k * step; | ||
| const wCos = tw.cos[twIdx]; | ||
| const wSin = tw.sin[twIdx]; | ||
| for (let i = 0; i < N; i += len) { | ||
| const p = i + k; | ||
| const q = p + halfLen; | ||
| const tRe = re[q] * wCos - im[q] * wSin; | ||
| const tIm = re[q] * wSin + im[q] * wCos; | ||
| re[q] = re[p] - tRe; | ||
| im[q] = im[p] - tIm; | ||
| re[p] += tRe; | ||
| im[p] += tIm; | ||
| const req = re[q]; | ||
| const imq = im[q]; | ||
| const tRe = req * wCos - imq * wSin; | ||
| const tIm = req * wSin + imq * wCos; | ||
| const rep = re[p]; | ||
| const imp = im[p]; | ||
| re[q] = rep - tRe; | ||
| im[q] = imp - tIm; | ||
| re[p] = rep + tRe; | ||
| im[p] = imp + tIm; | ||
| } | ||
| } |
There was a problem hiding this comment.
To further optimize this performance-critical loop, you can hoist the tw.cos and tw.sin array references out of the k loop to avoid repeated property lookups on the tw object. Additionally, using p as the loop variable directly eliminates the redundant i + k addition in the innermost loop, which is a common micro-optimization in FFT implementations.
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];
for (let p = k; p < N; p += len) {
const q = p + halfLen;
const req = re[q];
const imq = im[q];
const tRe = req * wCos - imq * wSin;
const tIm = req * wSin + imq * wCos;
const rep = re[p];
const imp = im[p];
re[q] = rep - tRe;
im[q] = imp - tIm;
re[p] = rep + tRe;
im[p] = imp + tIm;
}
}
What changed
In
src/mel.js, within the nested loops for the Fast Fourier Transform (FFT) stages, theiandkloops were swapped (loop interchange) to hoistwCosandwSintwiddle factor computations out of the innermost loop. Additionally, array reads and writes for thereandimFloat32Arrays inside the hot butterfly loop were cached into local variables. A.jules/bolt.mdjournal entry was appended to capture this learning.Why it was needed
V8 Javascript engines struggle to properly optimize deeply nested, tight numerical loops when there are constant indirect lookup costs (
tw.cos[twIdx]) occurring pointlessly inside the primary loop for operations that only vary with an outer variable. Profiling identified thefftstage butterfly loop as heavily bound by TypedArray element access.Impact
Benchmark simulations comparing the nested baseline structure against the swapped loop and local var cache reduced execution times significantly. Over 5000 executions of a dummy size-4096 spectrum, time spent fell from ~1192 ms to ~833 ms, a roughly ~30% improvement in this highly specific routine.
How to verify
npm testPR created automatically by Jules for task 12804144867430842509 started by @ysdede
Summary by Sourcery
Optimize FFT stage computation loops to reduce TypedArray access overhead and improve performance.
Enhancements:
Documentation: