⚡️ Speed up method OtelInterceptor._intercept_call by 8%
#22
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.
📄 8% (0.08x) speedup for
OtelInterceptor._intercept_callinchromadb/telemetry/opentelemetry/grpc.py⏱️ Runtime :
170 microseconds→158 microseconds(best of120runs)📝 Explanation and details
The optimization achieves a 7% speedup through three key improvements:
1. Replaced
binascii.hexlify().decode()with.hex()binascii.hexlify(span_id.to_bytes(8, "big")).decode()tospan_id.to_bytes(8, "big").hex().hex()method is a direct, native operation that's faster than the two-step hexlify+decode process2. Eliminated redundant
get_span_context()callsspan.get_span_context()twice; optimized version stores it inspan_ctxvariable3. Minimized
result.code()calls and improved conditional logicresult.code()in acodevariable instead of calling it 3 timesdetailscheck to avoid the redundantand result.details()call whenhasattrfailsclient_call_details.metadata[:]These optimizations are particularly effective for high-frequency gRPC calls where the encoding functions and span context access happen repeatedly. The test results show consistent 6-13% improvements across various scenarios, with the largest gains (13.7%) occurring when handling large trace/span IDs where the encoding optimization has maximum impact.
✅ Correctness verification report:
🌀 Generated Regression Tests and Runtime
To edit these changes
git checkout codeflash/optimize-OtelInterceptor._intercept_call-mh1tgx7eand push.