Add conditional GIL release based on data size threshold#11
Merged
shauneccles merged 9 commits intomainfrom Nov 27, 2025
Merged
Add conditional GIL release based on data size threshold#11shauneccles merged 9 commits intomainfrom
shauneccles merged 9 commits intomainfrom
Conversation
Co-authored-by: shauneccles <21007065+shauneccles@users.noreply.github.com>
Introduces GIL_RELEASE_THRESHOLD_FRAMES (1000) to only release the GIL when the data size is large enough that resampling work dominates the GIL release/acquire overhead (~1-5 µs). This improves single-threaded performance for small data sizes while maintaining multi-threading benefits for large data sizes. - Resampler.process(): conditional GIL release - CallbackResampler.read(): conditional GIL release - resample(): conditional GIL release - Added tests for conditional GIL release behavior - Updated .gitignore to exclude compiled extensions Co-authored-by: shauneccles <21007065+shauneccles@users.noreply.github.com>
Use lambdas to extract the common resampling logic, making the conditional GIL release pattern more maintainable. Co-authored-by: shauneccles <21007065+shauneccles@users.noreply.github.com>
Co-authored-by: shauneccles <21007065+shauneccles@users.noreply.github.com>
Co-authored-by: shauneccles <21007065+shauneccles@users.noreply.github.com>
Copilot
AI
changed the title
[WIP] Investigate GIL release impact on single threading performance
Add conditional GIL release based on data size threshold
Nov 25, 2025
Member
|
@copilot Look deeper - is there a better way to handle this, or to resolve or otherwise manage this issue. |
Instead of using only a fixed threshold, users can now explicitly control GIL release behavior via a new release_gil parameter: - None or "auto" (default): Release GIL only for large data (>= 1000 frames) - True: Always release GIL (best for multi-threaded applications) - False: Never release GIL (best for single-threaded, small data) This provides better control compared to the previous threshold-only approach: - Single-threaded applications can use release_gil=False to avoid overhead - Multi-threaded applications can use release_gil=True for maximum parallelism - Default "auto" mode balances both use cases Co-authored-by: shauneccles <21007065+shauneccles@users.noreply.github.com>
Co-authored-by: shauneccles <21007065+shauneccles@users.noreply.github.com>
Author
Investigated deeper and implemented a better solution in commit 3f0df5f. Instead of only using a fixed threshold, users now have explicit control via a # Default "auto" mode (threshold-based)
samplerate.resample(data, ratio)
# Force GIL release for multi-threaded apps
samplerate.resample(data, ratio, release_gil=True)
# Disable GIL release for single-threaded, small data
samplerate.resample(data, ratio, release_gil=False)This approach:
|
Member
|
@copilot Update the documentation with this new parameter |
Added a new "Multi-threading and GIL Control" section explaining: - The release_gil parameter and its three modes (None/auto, True, False) - When to use each mode for optimal performance - Examples for resample(), Resampler.process(), and CallbackResampler.read() Co-authored-by: shauneccles <21007065+shauneccles@users.noreply.github.com>
Author
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.
release_gilparameter for explicit controlSummary
This PR addresses the issue where releasing the GIL during resampling operations added overhead that impaired single-threaded performance for small data sizes.
Root Cause
The GIL release/acquire overhead (~1-5 µs) was significant for small data sizes where the actual resampling work only took 20-100 µs.
Solution
Added a new
release_gilparameter to all resampling methods that gives users explicit control:Results
Original prompt
💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.