-
Couldn't load subscription status.
- Fork 567
⚡️ Speed up function extract_sentrytrace_data by 37%
#4944
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
⚡️ Speed up function extract_sentrytrace_data by 37%
#4944
Conversation
The optimization adds length checks before expensive string formatting operations. Specifically:
**Key Changes:**
- Added `len(trace_id) != 32` check before `"{:032x}".format(int(trace_id, 16))`
- Added `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:
- Hexadecimal string parsing
- Integer conversion
- String formatting with zero-padding
**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, and `test_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.
| if trace_id and len(trace_id) != 32: | ||
| trace_id = "{:032x}".format(int(trace_id, 16)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If the string formatting is really redundant after the regex match, the string formatting logic can be removed.
There's no point checking len(trace_id) != 32, because to reach this point the regex has matched, so the string must be 32 characters long.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
agreed, can just remove it
📄 37% (0.37x) speedup for
extract_sentrytrace_datainsentry_sdk/tracing_utils.pyThis 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 of124runs)📝 Explanation and details
The optimization adds length checks before expensive string formatting operations. Specifically:
Key Changes:
len(trace_id) != 32check before"{:032x}".format(int(trace_id, 16))len(parent_span_id) != 16check 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_headershow 51.6% speedup, andtest_missing_trace_idshows 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-mg9m9ul7and push.