⚡️ Speed up function _encode_span_id by 30%
#20
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.
📄 30% (0.30x) speedup for
_encode_span_idinchromadb/telemetry/opentelemetry/grpc.py⏱️ Runtime :
896 microseconds→688 microseconds(best of175runs)📝 Explanation and details
The optimization replaces the two-step process of
binascii.hexlify().decode()with the direct.hex()method on bytes objects.Key changes:
binascii.hexlify()call which creates an intermediate bytes object.decode()operation.hex()method directly on the bytes fromto_bytes()Why it's faster:
The
.hex()method on bytes objects is implemented in C and directly produces a string, avoiding the intermediate bytes object creation and the subsequent decode step. This eliminates function call overhead and memory allocation/deallocation for the intermediate result.Performance characteristics:
The optimization shows consistent 20-35% speedup across all test cases, with particularly strong performance on:
The speedup is most pronounced in scenarios with many sequential calls, making this optimization especially valuable for telemetry systems that process high volumes of span IDs.
✅ Correctness verification report:
🌀 Generated Regression Tests and Runtime
To edit these changes
git checkout codeflash/optimize-_encode_span_id-mh1t8dwiand push.