⚡️ Speed up function set_transaction_name by 14%
#17
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.
📄 14% (0.14x) speedup for
set_transaction_nameinsentry_sdk/api.py⏱️ Runtime :
203 microseconds→177 microseconds(best of107runs)📝 Explanation and details
The optimization caches the
Scope.get_current_scopeclass method lookup in a module-level variable_scope_get_current_scope, eliminating repeated attribute access overhead.Key changes:
_scope_get_current_scope = Scope.get_current_scopeat module level to cache the method referenceget_current_scope()andset_transaction_name()to call the cached reference instead of performing attribute lookup each timeWhy this speeds up the code:
In Python, attribute access (
Scope.get_current_scope) involves dictionary lookups and method resolution that happen on every call. By caching the method reference once at module import time, we eliminate this overhead for each function invocation. This is particularly effective for frequently called functions like these.Performance impact by test case:
The optimization is most effective for workloads with frequent transaction name operations, as seen in the test results where repeated calls show the highest speedup percentages.
✅ Correctness verification report:
🌀 Generated Regression Tests and Runtime
To edit these changes
git checkout codeflash/optimize-set_transaction_name-mg976jx2and push.