⚡️ Speed up function has_tracing_enabled by 15%
#41
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.
📄 15% (0.15x) speedup for
has_tracing_enabledinsentry_sdk/tracing_utils.py⏱️ Runtime :
64.4 microseconds→56.0 microseconds(best of65runs)📝 Explanation and details
The optimized code improves performance by restructuring the conditional logic to enable early returns and reduce unnecessary operations:
Key Optimizations:
Early return for
enable_tracing=False: Instead of evaluating the entire boolean expression, the code immediately returnsFalsewhenenable_tracingis explicitlyFalse. This eliminates the need to check the tracing keys (traces_sample_rateandtraces_sampler) in cases where tracing is disabled.Removed redundant
bool()wrapper: The original code wrapped the entire expression inbool(), which adds function call overhead. The optimized version returns boolean values directly from the conditional expressions.Flattened conditional structure: The optimized code separates the
enable_tracingcheck from the tracing keys check, making the logic flow more linear and avoiding complex nested boolean expressions.Performance Impact:
The 14% speedup is most pronounced when
enable_tracing=False(20-29% faster in those test cases), as the function can exit early without performing additional dictionary lookups. Cases with missing or non-Falseenable_tracingvalues also benefit (8-24% faster) from the cleaner boolean logic and removal of thebool()wrapper.Best suited for: Applications where tracing is frequently disabled (
enable_tracing=False) or where the function is called frequently with various option configurations, as the early return pattern and reduced function call overhead provide consistent performance gains across different scenarios.✅ Correctness verification report:
⚙️ Existing Unit Tests and Runtime
test_basics.py::test_option_enable_tracing🌀 Generated Regression Tests and Runtime
To edit these changes
git checkout codeflash/optimize-has_tracing_enabled-mg9lhdgcand push.