⚡️ Speed up function set_tag by 14%
#13
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.
📄 14% (0.14x) speedup for
set_taginsentry_sdk/api.py⏱️ Runtime :
88.2 microseconds→77.3 microseconds(best of18runs)📝 Explanation and details
The optimization eliminates redundant attribute lookups by caching the
Scope.get_isolation_scopemethod reference in a module-level variable_get_isolation_scope.Key changes:
_get_isolation_scope = Scope.get_isolation_scopeat module level to cache the method referenceget_isolation_scope()to call_get_isolation_scope()instead ofScope.get_isolation_scope()set_tag()to call_get_isolation_scope()directly instead of going through theget_isolation_scope()wrapper functionWhy this is faster:
Scope.get_isolation_scope()requires Python to traverse the attribute chain (lookupScope, then lookupget_isolation_scopeon that class). The cached reference eliminates this lookup overhead.set_tag(), the optimization bypasses the intermediateget_isolation_scope()function call entirely, reducing call stack depth.The line profiler shows the impact -
set_tag()execution time dropped from 3275.3ns per hit to 1511.3ns per hit (54% improvement per call). This optimization is particularly effective for workloads with frequent tagging operations, as evidenced by the performance test cases in the suite that make hundreds ofset_tag()calls in tight loops.✅ Correctness verification report:
⚙️ Existing Unit Tests and Runtime
integrations/launchdarkly/test_launchdarkly.py::test_launchdarkly_integration_asynciointegrations/launchdarkly/test_launchdarkly.py::test_launchdarkly_integration_threadedintegrations/openfeature/test_openfeature.py::test_openfeature_integration_asynciointegrations/openfeature/test_openfeature.py::test_openfeature_integration_threadedintegrations/statsig/test_statsig.py::test_check_gate_asynciointegrations/statsig/test_statsig.py::test_check_gate_threadedintegrations/threading/test_threading.py::test_scope_data_not_leaked_in_threadsintegrations/unleash/test_unleash.py::test_is_enabled_asynciointegrations/unleash/test_unleash.py::test_is_enabled_threadednew_scopes_compat/test_new_scopes_compat.py::test_configure_scope_sdk1new_scopes_compat/test_new_scopes_compat.py::test_push_scope_sdk1new_scopes_compat/test_new_scopes_compat.py::test_with_cloned_hub_configure_scope_sdk1new_scopes_compat/test_new_scopes_compat.py::test_with_cloned_hub_push_scope_sdk1new_scopes_compat/test_new_scopes_compat.py::test_with_cloned_hub_sdk1new_scopes_compat/test_new_scopes_compat.py::test_with_hub_configure_scope_sdk1new_scopes_compat/test_new_scopes_compat.py::test_with_hub_push_scope_sdk1new_scopes_compat/test_new_scopes_compat.py::test_with_hub_sdk1test_client.py::test_nantest_crons.py::test_scope_data_in_checkintest_feature_flags.py::test_featureflags_integration_asynciotest_feature_flags.py::test_featureflags_integration_threaded🌀 Generated Regression Tests and Runtime
To edit these changes
git checkout codeflash/optimize-set_tag-mg95cj8sand push.