⚡️ Speed up method Transcript.get_by_id_async by 819% - #3
Open
codeflash-ai[bot] wants to merge 1 commit into
Open
⚡️ Speed up method Transcript.get_by_id_async by 819%#3codeflash-ai[bot] wants to merge 1 commit into
Transcript.get_by_id_async by 819%#3codeflash-ai[bot] wants to merge 1 commit into
Conversation
The key optimization is **moving the ThreadPoolExecutor from instance-level to module-level**. In the original code, every `Transcript` object created its own `ThreadPoolExecutor` in `__init__()`, which is extremely expensive. Each executor spawns multiple threads and allocates significant resources. When creating many `Transcript` instances (as suggested by the test patterns), this leads to: - Thread pool creation overhead for each instance - Memory bloat from multiple thread pools - OS-level thread context switching penalties The optimized version uses a single shared `_executor` at module level. All `Transcript` instances now share the same thread pool, eliminating the per-instance creation cost while maintaining identical async behavior. **Performance impact**: The line profiler shows `wait_for_completion_async()` improved from 568μs per hit to 63μs per hit (~9x faster), and the overall runtime improved from 3.03ms to 330μs (818% speedup). This optimization is particularly effective for workloads that create multiple `Transcript` objects or call `get_by_id_async()` frequently, as each call creates a new instance. The shared executor maintains thread reuse efficiency while dramatically reducing initialization overhead.
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.
📄 819% (8.19x) speedup for
Transcript.get_by_id_asyncinassemblyai/transcriber.py⏱️ Runtime :
3.03 milliseconds→330 microseconds(best of8runs)📝 Explanation and details
The key optimization is moving the ThreadPoolExecutor from instance-level to module-level.
In the original code, every
Transcriptobject created its ownThreadPoolExecutorin__init__(), which is extremely expensive. Each executor spawns multiple threads and allocates significant resources. When creating manyTranscriptinstances (as suggested by the test patterns), this leads to:The optimized version uses a single shared
_executorat module level. AllTranscriptinstances now share the same thread pool, eliminating the per-instance creation cost while maintaining identical async behavior.Performance impact: The line profiler shows
wait_for_completion_async()improved from 568μs per hit to 63μs per hit (~9x faster), and the overall runtime improved from 3.03ms to 330μs (818% speedup).This optimization is particularly effective for workloads that create multiple
Transcriptobjects or callget_by_id_async()frequently, as each call creates a new instance. The shared executor maintains thread reuse efficiency while dramatically reducing initialization overhead.✅ Correctness verification report:
🌀 Generated Regression Tests and Runtime
To edit these changes
git checkout codeflash/optimize-Transcript.get_by_id_async-mgv02utjand push.