⚡️ Speed up function _bool by 13%
          #19
        
          
      
                
     Open
            
            
          
  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.