Skip to content

⚡ Bolt: [performance improvement] Vectorize VAD energy calculation#458

Draft
EffortlessSteven wants to merge 1 commit intomainfrom
bolt-vectorize-vad-energy-15592330574394073925
Draft

⚡ Bolt: [performance improvement] Vectorize VAD energy calculation#458
EffortlessSteven wants to merge 1 commit intomainfrom
bolt-vectorize-vad-energy-15592330574394073925

Conversation

@EffortlessSteven
Copy link
Copy Markdown
Member

💡 What: Refactored _detect_speech_frames in transcription/streaming_asr.py to use vectorized numpy operations and removed the explicit python for loop and the unused _calculate_energy helper method. Cleaned up associated tests in tests/test_streaming_asr.py.
🎯 Why: Running python for loops to slice and calculate the RMS energy of numpy arrays frame-by-frame creates a massive bottleneck because it drops out of C-level vectorization. This occurs in a hot path (streaming voice activity detection), meaning every millisecond counts.
📊 Impact: VAD energy calculations now execute at C-level speeds, drastically reducing CPU overhead and latency during streaming transcription.
🔬 Measurement: Verified the algorithmic equivalence by ensuring the entire test suite (uv run pytest tests/test_streaming_asr.py) passes without modifications to the actual VAD logic tests.


PR created automatically by Jules for task 15592330574394073925 started by @EffortlessSteven

This commit replaces the inefficient python for-loop iterating over array slices in `_detect_speech_frames` with a vectorized numpy operation. The 1D audio array is reshaped into a 2D array of frames, allowing the energy calculations and threshold comparisons to be performed at C-level speeds over the entire array simultaneously. The unused helper method `_calculate_energy` and its associated unit tests were removed. This change significantly reduces the execution time of voice activity detection in the hot path without altering the algorithmic logic.
@google-labs-jules
Copy link
Copy Markdown

👋 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 10, 2026

Warning

Rate limit exceeded

@EffortlessSteven has exceeded the limit for the number of commits that can be reviewed per hour. Please wait 19 minutes and 0 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 19 minutes and 0 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: Organization UI

Review profile: ASSERTIVE

Plan: Pro

Run ID: 92482260-5387-4546-9217-f1264c5d0638

📥 Commits

Reviewing files that changed from the base of the PR and between 23d833f and 90cf3a1.

📒 Files selected for processing (3)
  • .jules/bolt.md
  • tests/test_streaming_asr.py
  • transcription/streaming_asr.py
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch bolt-vectorize-vad-energy-15592330574394073925

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.

@github-actions
Copy link
Copy Markdown

Benchmark Results

Commit: 90c945cee1eb927b002bc54218a0173123bcd236
Sample Limit: 5

Baseline Comparison

Track Metric Current Baseline Regression Threshold Status
ASR wer 0.0000% 5.0000% -100.0% 50%
cer 0.0000% 2.0000% -100.0% 50%

Overall: ✅ All metrics within thresholds


Details
  • Workflow run: #436
  • Artifacts: benchmark-results-24221434837
  • Mode: Report-only (regressions do not block merge)

@codecov
Copy link
Copy Markdown

codecov bot commented Apr 10, 2026

Codecov Report

❌ Patch coverage is 88.88889% with 1 line in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
transcription/streaming_asr.py 88.88% 1 Missing ⚠️

📢 Thoughts on this report? Let us know!

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 Voice Activity Detection (VAD) logic by vectorizing frame-wise audio processing in _detect_speech_frames, replacing the iterative _calculate_energy method with efficient numpy operations. The feedback suggests further performance improvements by using .tolist() for converting numpy arrays to Python lists and comparing mean squares directly against a squared threshold to avoid unnecessary square root calculations.

Comment on lines +203 to +211
mean_squares = np.mean(frames**2, axis=1)
# sqrt(mean(audio^2))
energies = np.sqrt(mean_squares)

# Vectorized comparison against threshold
is_speech = energies > self.config.vad_energy_threshold

return speech_frames
# Explicitly cast to list of booleans to satisfy strict typing
return [bool(x) for x in is_speech]
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 vectorizing the energy calculation is a significant improvement, the use of a list comprehension in the return statement introduces a Python-level loop, which partially negates the performance benefits of using numpy. According to PEP 8 and general numpy best practices, is_speech.tolist() is the idiomatic and much more efficient way to convert a numpy array to a Python list. It also correctly handles the conversion of np.bool_ to Python bool, satisfying strict typing requirements. Additionally, you can optimize the calculation by comparing the mean squares directly against the squared threshold, avoiding the np.sqrt operation entirely.

        mean_squares = np.mean(frames**2, axis=1)\n\n        # Vectorized comparison against squared threshold to avoid sqrt\n        is_speech = mean_squares > (self.config.vad_energy_threshold**2)\n\n        # tolist() is more efficient than a list comprehension for converting to Python types\n        return is_speech.tolist()

Comment thread .jules/bolt.md
@@ -0,0 +1,3 @@
## 2024-10-27 - Vectorizing frame-wise audio processing loops
**Learning:** Using Python `for` loops to iterate over and slice numpy arrays drops out of C-level vectorization, creating a massive bottleneck in hot paths like VAD energy calculations.
**Action:** Use `np.reshape` to convert the 1D audio array into a 2D `(num_frames, frame_size)` array, then use `axis=1` operations (e.g., `np.mean(frames**2, axis=1)`) to maintain C-level performance. Ensure boolean numpy arrays are cast properly using list comprehensions (`[bool(x) for x in array]`) to satisfy strict typing.
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

The recommendation to use list comprehensions for casting boolean numpy arrays is suboptimal for performance. array.tolist() is the preferred method for converting numpy arrays to Python lists; it is implemented in C and correctly handles the conversion to Python scalar types (e.g., bool), which satisfies strict typing requirements more efficiently.

Suggested change
**Action:** Use `np.reshape` to convert the 1D audio array into a 2D `(num_frames, frame_size)` array, then use `axis=1` operations (e.g., `np.mean(frames**2, axis=1)`) to maintain C-level performance. Ensure boolean numpy arrays are cast properly using list comprehensions (`[bool(x) for x in array]`) to satisfy strict typing.
**Action:** Use np.reshape to convert the 1D audio array into a 2D (num_frames, frame_size) array, then use axis=1 operations (e.g., np.mean(frames**2, axis=1)) to maintain C-level performance. Use array.tolist() to efficiently convert boolean numpy arrays to a Python list while satisfying strict typing.

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