⚡️ Speed up function _bool by 13%#19
Open
codeflash-ai[bot] wants to merge 1 commit into
Open
Conversation
The optimized code achieves a 13% speedup through two key optimizations in the protobuf encoding functions:
**1. Fast path for single-byte varints in `_varint()`:**
- Added an early return `if value <= 0x7F: return bytes((value,))` to avoid creating a bytearray and performing loops for small integers (≤127)
- This optimization is highly effective since protobuf field keys are typically small numbers that fit in a single byte
**2. Replaced bytearray with list + local method reference:**
- Changed from `bytearray()` to `[]` and cached `out.append` as a local variable `append`
- This eliminates the overhead of bytearray operations and method lookups in the loop
**3. Direct byte literals in `_bool()`:**
- Replaced `_varint(1 if value else 0)` with `(b'\x01' if value else b'\x00')`
- Eliminates function call overhead for encoding boolean values since they always result in single-byte values
The test results show consistent 8-27% improvements across all cases, with particularly strong gains for:
- Small field numbers (single-byte keys): 15-27% faster
- False values: Often 15-25% faster due to avoiding the varint call entirely
- Empty containers: Up to 42% faster for falsy values like `[]` and `{}`
These optimizations are especially beneficial for typical protobuf usage where field numbers are small and boolean values are common.
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.
📄 13% (0.13x) speedup for
_boolinsrc/deepgram/extensions/telemetry/proto_encoder.py⏱️ Runtime :
83.8 microseconds→73.8 microseconds(best of148runs)📝 Explanation and details
The optimized code achieves a 13% speedup through two key optimizations in the protobuf encoding functions:
1. Fast path for single-byte varints in
_varint():if value <= 0x7F: return bytes((value,))to avoid creating a bytearray and performing loops for small integers (≤127)2. Replaced bytearray with list + local method reference:
bytearray()to[]and cachedout.appendas a local variableappend3. Direct byte literals in
_bool():_varint(1 if value else 0)with(b'\x01' if value else b'\x00')The test results show consistent 8-27% improvements across all cases, with particularly strong gains for:
[]and{}These optimizations are especially beneficial for typical protobuf usage where field numbers are small and boolean values are common.
✅ Correctness verification report:
🌀 Generated Regression Tests and Runtime
🔎 Concolic Coverage Tests and Runtime
codeflash_concolic_7zeygj7s/tmpze2qsx6z/test_concolic_coverage.py::test__boolcodeflash_concolic_7zeygj7s/tmpze2qsx6z/test_concolic_coverage.py::test__bool_2To edit these changes
git checkout codeflash/optimize-_bool-mh4iu1zxand push.