⚡️ Speed up function generate_mlflow_trace_id_from_otel_trace_id by 43%
#164
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.
📄 43% (0.43x) speedup for
generate_mlflow_trace_id_from_otel_trace_idinmlflow/tracing/utils/__init__.py⏱️ Runtime :
664 microseconds→463 microseconds(best of82runs)📝 Explanation and details
The optimization achieves a 43% speedup by replacing the OpenTelemetry library call with a native Python f-string format and removing unnecessary caching overhead.
Key optimizations:
Removed
@lru_cache(maxsize=1): The LRU cache was counterproductive because:trace_api.format_trace_id) is already very fastReplaced
trace_api.format_trace_id()withf"{trace_id:032x}":Performance impact analysis:
encode_trace_idruns in 414ns vs 2595ns for the original cached versionmlflow/entities/span.pyin span creation from OpenTelemetry protobuf data, indicating it's in a hot path during trace ingestionWorkload benefits:
Based on the function reference, this optimization will significantly improve performance during high-volume trace processing where spans are frequently created from OpenTelemetry protocol data. The elimination of locking overhead makes it particularly beneficial for multi-threaded trace ingestion scenarios.
✅ Correctness verification report:
🌀 Generated Regression Tests and Runtime
To edit these changes
git checkout codeflash/optimize-generate_mlflow_trace_id_from_otel_trace_id-mhwymo5rand push.