⚡️ Speed up function add_span_attributes by 337% - #10
Open
codeflash-ai[bot] wants to merge 1 commit into
Open
Conversation
The optimization achieves a **337% speedup** by implementing early return and improved null checking patterns that eliminate unnecessary function calls in the common case where OpenTelemetry is disabled. **Key optimizations applied:** 1. **Early return with explicit null check**: Changed `if not tracer:` to `if tracer is None:` followed by immediate return. This uses explicit identity comparison which is slightly faster than truthiness evaluation. 2. **Eliminated unnecessary function calls**: In the original code, even when `tracer` was None, the function still called `trace.get_current_span()` and `span.is_recording()`. The optimized version completely skips these expensive operations when telemetry is disabled. 3. **Improved span null checking**: Changed `if span and span.is_recording():` to `if span is not None and span.is_recording():` with a comment explaining the optimization to avoid the `is_recording()` call when span is None. **Why this leads to significant speedup:** The line profiler shows that `trace.get_current_span()` was consuming **74.2% of the original execution time** (209,476ns out of 282,318ns total). By adding the early return when `tracer is None`, this expensive call is completely eliminated in the most common scenario where OpenTelemetry is not configured or available. **Impact on existing workloads:** Based on the function reference in `titiler/core/middleware.py`, this function is called **on every HTTP request** in the middleware pipeline. The telemetry data shows rich request metadata being captured, making this a critical hot path. A 337% speedup here directly translates to reduced request latency across the entire application. **Test case performance:** The annotated tests show consistent **300-650% speedups** across all scenarios, with the largest gains in edge cases like "span is None" (587% faster) and "attributes with non-str keys" (646% faster). This indicates the optimization is particularly effective when OpenTelemetry spans are not actively recording, which is often the case in production environments where telemetry may be selectively enabled.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
📄 337% (3.37x) speedup for
add_span_attributesinsrc/titiler/core/titiler/core/telemetry.py⏱️ Runtime :
50.2 microseconds→11.5 microseconds(best of250runs)📝 Explanation and details
The optimization achieves a 337% speedup by implementing early return and improved null checking patterns that eliminate unnecessary function calls in the common case where OpenTelemetry is disabled.
Key optimizations applied:
Early return with explicit null check: Changed
if not tracer:toif tracer is None:followed by immediate return. This uses explicit identity comparison which is slightly faster than truthiness evaluation.Eliminated unnecessary function calls: In the original code, even when
tracerwas None, the function still calledtrace.get_current_span()andspan.is_recording(). The optimized version completely skips these expensive operations when telemetry is disabled.Improved span null checking: Changed
if span and span.is_recording():toif span is not None and span.is_recording():with a comment explaining the optimization to avoid theis_recording()call when span is None.Why this leads to significant speedup:
The line profiler shows that
trace.get_current_span()was consuming 74.2% of the original execution time (209,476ns out of 282,318ns total). By adding the early return whentracer is None, this expensive call is completely eliminated in the most common scenario where OpenTelemetry is not configured or available.Impact on existing workloads:
Based on the function reference in
titiler/core/middleware.py, this function is called on every HTTP request in the middleware pipeline. The telemetry data shows rich request metadata being captured, making this a critical hot path. A 337% speedup here directly translates to reduced request latency across the entire application.Test case performance:
The annotated tests show consistent 300-650% speedups across all scenarios, with the largest gains in edge cases like "span is None" (587% faster) and "attributes with non-str keys" (646% faster). This indicates the optimization is particularly effective when OpenTelemetry spans are not actively recording, which is often the case in production environments where telemetry may be selectively enabled.
✅ Correctness verification report:
🌀 Generated Regression Tests and Runtime
🔎 Concolic Coverage Tests and Runtime
To edit these changes
git checkout codeflash/optimize-add_span_attributes-mifm78xsand push.