⚡️ Speed up function extract_sentrytrace_data
by 37%
#4944
+2
−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.
📄 37% (0.37x) speedup for
extract_sentrytrace_data
insentry_sdk/tracing_utils.py
This one looked really important as well. I found a bunch more optimizations, but don't want to spam you guys. Would you like to meet over a 30 min call to discuss how we can better coordinate?
⏱️ Runtime :
3.30 milliseconds
→2.41 milliseconds
(best of124
runs)📝 Explanation and details
The optimization adds length checks before expensive string formatting operations. Specifically:
Key Changes:
len(trace_id) != 32
check before"{:032x}".format(int(trace_id, 16))
len(parent_span_id) != 16
check before"{:016x}".format(int(parent_span_id, 16))
Why It's Faster:
The original code always performed string-to-int conversion and formatting, even when the trace_id/span_id were already properly formatted. The optimization skips these expensive operations when the strings are already the correct length (32 hex chars for trace_id, 16 for span_id).
The
int(trace_id, 16)
and"{:032x}".format()
operations are computationally expensive, involving:Performance Impact:
Test results show the optimization is most effective when trace IDs and span IDs are already properly formatted (which is common in production). Cases like
test_valid_full_header
show 51.6% speedup, andtest_missing_trace_id
shows 65.9% speedup. The optimization has minimal overhead for cases where formatting is still needed, with only small gains (1-7%) for malformed inputs.This is particularly valuable for high-throughput tracing scenarios where most headers contain well-formatted trace data.
✅ Correctness verification report:
⚙️ Existing Unit Tests and Runtime
tracing/test_http_headers.py::test_sentrytrace_extraction
🌀 Generated Regression Tests and Runtime
To edit these changes
git checkout codeflash/optimize-extract_sentrytrace_data-mg9m9ul7
and push.