⚡️ Speed up function _should_be_included
by 103%
#43
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_included
insentry_sdk/tracing_utils.py
⏱️ Runtime :
112 milliseconds
→55.0 milliseconds
(best of80
runs)📝 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-mg9lx66s
and push.