⚡️ Speed up function in_operation
by 42%
#2
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.
📄 42% (0.42x) speedup for
in_operation
intemporalio/nexus/_operation_context.py
⏱️ Runtime :
334 microseconds
→236 microseconds
(best of280
runs)📝 Explanation and details
The optimization inlines the
_try_temporal_context()
function call directly intoin_operation()
to eliminate function call overhead.Key changes:
in_operation()
function now directly executes the context variable lookups and validation logic instead of calling_try_temporal_context()
is not None
check to the return statement to maintain the boolean return type_try_temporal_context()
function is preserved unchanged for other potential callersWhy this speeds up execution:
in_operation()
spent 100% of its time calling_try_temporal_context()
, so removing this indirection directly improves performancePerformance characteristics based on tests:
in_operation()
appears to be called frequently in performance-critical paths, the cumulative benefit of removing function call overhead is substantial✅ Correctness verification report:
🌀 Generated Regression Tests and Runtime
To edit these changes
git checkout codeflash/optimize-in_operation-mgib0ny1
and push.