⚡️ Speed up function _should_be_included by 103%#43
Open
codeflash-ai[bot] wants to merge 1 commit intomasterfrom
Open
⚡️ Speed up function _should_be_included by 103%#43codeflash-ai[bot] wants to merge 1 commit intomasterfrom
_should_be_included by 103%#43codeflash-ai[bot] wants to merge 1 commit intomasterfrom
Conversation
The optimized code achieves a 103% speedup through two key optimizations: **1. Precompiled Regex Pattern** The regex pattern `r"[\\/](?:dist|site)-packages[\\/]"` is compiled once at module load time as `_DIST_SITE_PACKAGES_RE`, eliminating the overhead of recompiling the regex on every call to `_is_external_source()`. This provides consistent ~30-40% improvements for external source detection cases. **2. Optimized `_module_in_list()` Function** - **Set-based exact matching**: Converts the list to a set for O(1) exact lookups instead of O(n) linear search - **Tuple-based prefix matching**: Creates a tuple of prefixes (e.g., `"myapp."`) and uses `str.startswith(tuple)`, which is C-optimized **Performance Impact by Test Case Type:** - **Large list scenarios**: Dramatic improvements (200%+ speedup) when matching items in lists with 1000+ entries due to set lookup efficiency - **Basic operations**: Moderate improvements (10-45% faster) for typical use cases with small lists - **Prefix matching**: Some slowdown (5-15%) for submodule cases due to tuple creation overhead, but this is offset by gains in other scenarios - **External source detection**: Consistent 30-40% improvements from precompiled regex The optimization trades a small upfront cost (set/tuple creation) for significant gains when dealing with larger lists or repeated calls, making it particularly effective for real-world tracing scenarios where these functions are called frequently.
misrasaurabh1
approved these changes
Oct 2, 2025
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.
📄 103% (1.03x) speedup for
_should_be_includedinsentry_sdk/tracing_utils.py⏱️ Runtime :
112 milliseconds→55.0 milliseconds(best of80runs)📝 Explanation and details
The optimized code achieves a 103% speedup through two key optimizations:
1. Precompiled Regex Pattern
The regex pattern
r"[\\/](?:dist|site)-packages[\\/]"is compiled once at module load time as_DIST_SITE_PACKAGES_RE, eliminating the overhead of recompiling the regex on every call to_is_external_source(). This provides consistent ~30-40% improvements for external source detection cases.2. Optimized
_module_in_list()Function"myapp.") and usesstr.startswith(tuple), which is C-optimizedPerformance Impact by Test Case Type:
The optimization trades a small upfront cost (set/tuple creation) for significant gains when dealing with larger lists or repeated calls, making it particularly effective for real-world tracing scenarios where these functions are called frequently.
✅ Correctness verification report:
⚙️ Existing Unit Tests and Runtime
test_tracing_utils.py::test_should_be_included🌀 Generated Regression Tests and Runtime
To edit these changes
git checkout codeflash/optimize-_should_be_included-mg9lx66sand push.