⚡️ Speed up function _get_span_name
by 5%
#48
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.
📄 5% (0.05x) speedup for
_get_span_name
insentry_sdk/tracing_utils.py
⏱️ Runtime :
2.18 milliseconds
→2.07 milliseconds
(best of100
runs)📝 Explanation and details
The optimization achieves a 5% speedup by eliminating inefficient loops and reducing variable assignments. Here are the key changes:
1. Loop Elimination for AI_CHAT Template
for
loop to iterate through("model", "model_name")
keys, callingkwargs.get()
andisinstance()
for each iteration2. Early Returns
elif
chains with a sharedspan_name
variable that gets reassignedreturn span_name
lookup3. Direct Value Validation
kwargs.get(key) and isinstance(kwargs[key], str)
in loopmodel is not None and isinstance(model, str)
Performance Benefits by Test Case:
The optimization is particularly effective for AI_CHAT templates with large kwargs dictionaries, which are common in ML/AI tracing scenarios.
✅ Correctness verification report:
🌀 Generated Regression Tests and Runtime
To edit these changes
git checkout codeflash/optimize-_get_span_name-mg9nnwxm
and push.