Skip to content

Conversation

@codeflash-ai
Copy link

@codeflash-ai codeflash-ai bot commented Oct 24, 2025

📄 23% (0.23x) speedup for _double in src/deepgram/extensions/telemetry/proto_encoder.py

⏱️ Runtime : 13.8 microseconds 11.3 microseconds (best of 81 runs)

📝 Explanation and details

The optimization inlines the _varint function directly into _key, eliminating function call overhead. Since _key generates protobuf wire format keys using (field_number << 3) | wire_type, the resulting values are typically small (most field numbers are < 16, so keys are usually ≤ 0x7F).

Key changes:

  • Replaced _varint((field_number << 3) | wire_type) with inlined varint encoding logic
  • Added fast-path: if value <= 0x7F: return bytes((value,)) for single-byte encoding
  • Eliminated function call overhead for the common case of small field numbers

Why it's faster:

  • Function call elimination: Saves the overhead of calling _varint for every key generation
  • Fast-path optimization: Most protobuf field numbers are small, so keys typically encode as single bytes, avoiding the while loop entirely
  • Reduced stack operations: Direct byte manipulation instead of function parameter passing

The 22% speedup is particularly effective for test cases with small field numbers (like the annotated tests using field_number=1 and field_number=13), where the fast-path single-byte encoding provides maximum benefit. This optimization excels in typical protobuf usage patterns where field numbers stay low.

Correctness verification report:

Test Status
⚙️ Existing Unit Tests 🔘 None Found
🌀 Generated Regression Tests 5 Passed
⏪ Replay Tests 🔘 None Found
🔎 Concolic Coverage Tests 1 Passed
📊 Tests Coverage 100.0%
🌀 Generated Regression Tests and Runtime
import struct # used for binary packing/unpacking # imports import pytest # used for our unit tests from deepgram.extensions.telemetry.proto_encoder import _double # unit tests # --- Basic Test Cases --- def test_double_edge_min_max_float(): # Test with minimum and maximum float values import sys field_number = 13 min_float = -sys.float_info.max max_float = sys.float_info.max codeflash_output = _double(field_number, min_float); result_min = codeflash_output codeflash_output = _double(field_number, max_float); result_max = codeflash_output expected_key = _key(field_number, 1) unpacked_min = struct.unpack("<d", result_min[len(expected_key):])[0] unpacked_max = struct.unpack("<d", result_max[len(expected_key):])[0] #------------------------------------------------ import math import struct # imports import pytest # used for our unit tests from deepgram.extensions.telemetry.proto_encoder import _double # unit tests # ---------------------- # Basic Test Cases # ---------------------- def test_nan_double(): # Test NaN value field_number = 1 value = float('nan') codeflash_output = _double(field_number, value); result = codeflash_output # 4.00μs -> 3.00μs (33.4% faster) # struct.pack will produce a NaN bit pattern, but there are many NaN bit patterns. # Check that the unpacked value is NaN unpacked = struct.unpack("<d", result[-8:])[0] def test_invalid_field_number_type(): # Test with invalid field_number type (should raise TypeError) with pytest.raises(TypeError): _double("not_an_int", 1.0) # 2.01μs -> 1.98μs (1.82% faster) def test_invalid_value_type(): # Test with value that cannot be converted to float (should raise ValueError) with pytest.raises(ValueError): _double(1, "not_a_float") # 3.92μs -> 3.23μs (21.4% faster) #------------------------------------------------ from deepgram.extensions.telemetry.proto_encoder import _double def test__double(): _double(0, 0.0)
🔎 Concolic Coverage Tests and Runtime
Test File::Test Function Original ⏱️ Optimized ⏱️ Speedup
codeflash_concolic_7zeygj7s/tmpmx0pv4o_/test_concolic_coverage.py::test__double 3.89μs 3.05μs 27.5%✅

To edit these changes git checkout codeflash/optimize-_double-mh4j30jd and push.

Codeflash

The optimization **inlines the `_varint` function** directly into `_key`, eliminating function call overhead. Since `_key` generates protobuf wire format keys using `(field_number << 3) | wire_type`, the resulting values are typically small (most field numbers are < 16, so keys are usually ≤ 0x7F). **Key changes:** - Replaced `_varint((field_number << 3) | wire_type)` with inlined varint encoding logic - Added fast-path: `if value <= 0x7F: return bytes((value,))` for single-byte encoding - Eliminated function call overhead for the common case of small field numbers **Why it's faster:** - **Function call elimination**: Saves the overhead of calling `_varint` for every key generation - **Fast-path optimization**: Most protobuf field numbers are small, so keys typically encode as single bytes, avoiding the while loop entirely - **Reduced stack operations**: Direct byte manipulation instead of function parameter passing The 22% speedup is particularly effective for test cases with small field numbers (like the annotated tests using field_number=1 and field_number=13), where the fast-path single-byte encoding provides maximum benefit. This optimization excels in typical protobuf usage patterns where field numbers stay low.
@codeflash-ai codeflash-ai bot requested a review from mashraf-222 October 24, 2025 07:27
@codeflash-ai codeflash-ai bot added ⚡️ codeflash Optimization PR opened by Codeflash AI 🎯 Quality: Medium Optimization Quality according to Codeflash labels Oct 24, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

⚡️ codeflash Optimization PR opened by Codeflash AI 🎯 Quality: Medium Optimization Quality according to Codeflash

1 participant