⚡️ Speed up function get_otel_attribute by 28%
#163
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.
📄 28% (0.28x) speedup for
get_otel_attributeinmlflow/tracing/utils/__init__.py⏱️ Runtime :
729 microseconds→568 microseconds(best of68runs)📝 Explanation and details
The optimization adds a string type check with selective JSON parsing that significantly reduces the number of expensive
json.loads()calls.Key Changes:
json.loads()if the string appears to be JSON-formatted (starts/ends with quotes, brackets, or braces)Why This Is Faster:
json.loads()is computationally expensive, requiring string parsing, tokenization, and object constructionjson.loads()calls (32 plain strings avoided out of 60 total string attributes in the profiler)attribute_value[0] == '"') are orders of magnitude faster than JSON parsingjson.loads()from 922ms to 633ms (31% reduction)Performance Patterns from Tests:
Impact on Hot Path Usage:
Based on function references,
get_otel_attribute()is called in critical tracing paths:mlflow.start_span()- used in trace creation workflowsmlflow.start_span_no_context()- manual span managementThe 28% overall speedup will directly benefit these tracing operations, especially when spans contain many non-JSON string attributes, which appears common in OpenTelemetry attribute storage patterns.
✅ Correctness verification report:
🌀 Generated Regression Tests and Runtime
To edit these changes
git checkout codeflash/optimize-get_otel_attribute-mhwy4jyuand push.