⚡️ Speed up function _int64 by 18%#20
Open
codeflash-ai[bot] wants to merge 1 commit into
Open
Conversation
The optimized code achieves a **17% speedup** through two key optimizations targeting the common case of small integer values in protobuf encoding: **1. Single-byte varint preallocation**: Added `_SINGLE_BYTE_VARINTS` tuple containing pre-computed `bytes` objects for values 0-127. This eliminates the overhead of `bytearray()` allocation, bit manipulation loops, and `bytes()` conversion for small values - which are extremely common in protobuf field keys and small integers. **2. Inlined key computation in `_key()`**: Instead of calling `_varint()`, the function now directly computes the key value and uses the fast path for single-byte results, avoiding function call overhead. **Why this works**: Protobuf field numbers are typically small (1-15 are most common), and the wire type is always 0-5, making the combined key value `(field_number << 3) | wire_type` usually less than 128. The test results show this optimization is most effective for small field numbers and values: - `test_int64_basic_zero()`: 58.6% faster - `test_int64_basic_field_number_zero()`: 62.8% faster - `test_basic_zero_value()`: 75.4% faster The optimization provides diminishing returns for larger values (like `test_basic_larger_positive_value()` showing 17% slower) since they still require the full varint encoding loop, but the overall benefit comes from the high frequency of small values in typical protobuf usage patterns.
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.
📄 18% (0.18x) speedup for
_int64insrc/deepgram/extensions/telemetry/proto_encoder.py⏱️ Runtime :
68.0 microseconds→57.8 microseconds(best of186runs)📝 Explanation and details
The optimized code achieves a 17% speedup through two key optimizations targeting the common case of small integer values in protobuf encoding:
1. Single-byte varint preallocation: Added
_SINGLE_BYTE_VARINTStuple containing pre-computedbytesobjects for values 0-127. This eliminates the overhead ofbytearray()allocation, bit manipulation loops, andbytes()conversion for small values - which are extremely common in protobuf field keys and small integers.2. Inlined key computation in
_key(): Instead of calling_varint(), the function now directly computes the key value and uses the fast path for single-byte results, avoiding function call overhead.Why this works: Protobuf field numbers are typically small (1-15 are most common), and the wire type is always 0-5, making the combined key value
(field_number << 3) | wire_typeusually less than 128. The test results show this optimization is most effective for small field numbers and values:test_int64_basic_zero(): 58.6% fastertest_int64_basic_field_number_zero(): 62.8% fastertest_basic_zero_value(): 75.4% fasterThe optimization provides diminishing returns for larger values (like
test_basic_larger_positive_value()showing 17% slower) since they still require the full varint encoding loop, but the overall benefit comes from the high frequency of small values in typical protobuf usage patterns.✅ Correctness verification report:
🌀 Generated Regression Tests and Runtime
🔎 Concolic Coverage Tests and Runtime
codeflash_concolic_7zeygj7s/tmpbo5k38g4/test_concolic_coverage.py::test__int64To edit these changes
git checkout codeflash/optimize-_int64-mh4izvm6and push.