⚡️ Speed up function set_tags by 6%
#14
Open
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.
📄 6% (0.06x) speedup for
set_tagsinsentry_sdk/api.py⏱️ Runtime :
29.9 microseconds→28.1 microseconds(best of54runs)📝 Explanation and details
The optimization eliminates a redundant function call by directly calling
Scope.get_isolation_scope()instead of going through the intermediateget_isolation_scope()wrapper function inset_tags().Key Change:
return get_isolation_scope().set_tags(tags)(calls wrapper function first)scope = Scope.get_isolation_scope()followed byscope.set_tags(tags)(direct call)Why It's Faster:
The original version creates an unnecessary function call overhead. When
set_tags()callsget_isolation_scope(), Python has to:get_isolation_scope()Scope.get_isolation_scope()set_tags()on itThe optimized version eliminates step 1 by calling
Scope.get_isolation_scope()directly, reducing the call stack depth and function call overhead.Performance Impact:
The line profiler shows the optimization reduces total time in
set_tags()from 1.74ms to 1.55ms. The 6% speedup is consistent across test cases, with the annotated test showing improvement from 14.3μs to 13.5μs.This optimization is particularly effective for high-frequency logging scenarios where
set_tags()is called repeatedly, as it eliminates function call overhead on every invocation.✅ Correctness verification report:
⚙️ Existing Unit Tests and Runtime
test_api.py::test_set_tags🌀 Generated Regression Tests and Runtime
To edit these changes
git checkout codeflash/optimize-set_tags-mg95ey0wand push.