⚡️ Speed up function create_span_decorator by 6%
#47
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
create_span_decoratorinsentry_sdk/tracing_utils.py⏱️ Runtime :
11.2 microseconds→10.6 microseconds(best of17runs)📝 Explanation and details
The optimized code achieves a 5% speedup by eliminating redundant computations that were happening on every function call. The key optimizations are:
Pre-computed Values: Instead of calling
qualname_from_function(f),inspect.iscoroutinefunction(f), andinspect.signature(f)repeatedly during each decorated function invocation, these are computed once during decorator creation and cached asqualname,iscoroutine, andsignature.Partial Function for Span Names: The original code reconstructed span names on every call using
_get_span_name(template, function_name, kwargs). The optimized version usesfunctools.partialto pre-bind the static arguments (templateandfunction_name), creatingspan_name_partialthat only needs the dynamickwargsparameter.Optimized Attribute Handling: The original code used
attributes or {}which creates a new empty dict on every call whenattributesis None. The optimized version uses an explicitif attributes:check, only callingspan.update_data({})when needed.Shared Logic Extraction: The common span validation logic is extracted into
_common_before(), reducing code duplication between sync and async wrappers.These optimizations are particularly effective for high-frequency decorated functions where the decorator overhead becomes noticeable. The 5% improvement comes from avoiding repeated string operations, function calls, and object allocations that were happening on every invocation of the decorated function.
✅ Correctness verification report:
⚙️ Existing Unit Tests and Runtime
🌀 Generated Regression Tests and Runtime
To edit these changes
git checkout codeflash/optimize-create_span_decorator-mg9n6ltoand push.