From d2b2dfca99119ada608a9317a34303d1f62030fd Mon Sep 17 00:00:00 2001 From: "codeflash-ai[bot]" <148906541+codeflash-ai[bot]@users.noreply.github.com> Date: Fri, 24 Oct 2025 07:24:44 +0000 Subject: [PATCH] Optimize _int64 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. --- .../extensions/telemetry/proto_encoder.py | 39 ++++++++++++------- 1 file changed, 26 insertions(+), 13 deletions(-) diff --git a/src/deepgram/extensions/telemetry/proto_encoder.py b/src/deepgram/extensions/telemetry/proto_encoder.py index a085ed0e..cd148256 100644 --- a/src/deepgram/extensions/telemetry/proto_encoder.py +++ b/src/deepgram/extensions/telemetry/proto_encoder.py @@ -6,13 +6,20 @@ import typing from typing import Dict, List +_SINGLE_BYTE_VARINTS = tuple(bytes([i]) for i in range(128)) + # --- Protobuf wire helpers (proto3) --- + def _varint(value: int) -> bytes: if value < 0: # For this usage we only encode non-negative values value &= (1 << 64) - 1 + # Use fast path for single-byte values (common for small integers and field keys) + if value < 0x80: + return _SINGLE_BYTE_VARINTS[value] + # Typical multi-byte varint encoding out = bytearray() while value > 0x7F: out.append((value & 0x7F) | 0x80) @@ -22,7 +29,12 @@ def _varint(value: int) -> bytes: def _key(field_number: int, wire_type: int) -> bytes: - return _varint((field_number << 3) | wire_type) + # Inline the (field_number << 3) | wire_type computation to avoid function call overhead + value = (field_number << 3) | wire_type + if value < 0x80: + return _SINGLE_BYTE_VARINTS[value] + else: + return _varint(value) def _len_delimited(field_number: int, payload: bytes) -> bytes: @@ -83,6 +95,7 @@ def _map_str_double(field_number: int, items: typing.Mapping[str, float] | None) # --- Schema-specific encoders (deepgram.dxtelemetry.v1) --- + def _encode_telemetry_context(ctx: typing.Mapping[str, typing.Any]) -> bytes: # Map SDK context keys to proto fields package_name = ctx.get("sdk_name") or ctx.get("package_name") or "python-sdk" @@ -123,7 +136,7 @@ def _encode_telemetry_context(ctx: typing.Mapping[str, typing.Any]) -> bytes: msg += _string(11, installation_id) if project_id: msg += _string(12, project_id) - + # Include extras as additional context attributes (field 13) extras = ctx.get("extras", {}) if extras: @@ -133,11 +146,13 @@ def _encode_telemetry_context(ctx: typing.Mapping[str, typing.Any]) -> bytes: if value is not None: extras_map[str(key)] = str(value) msg += _map_str_str(13, extras_map) - + return bytes(msg) -def _encode_telemetry_event(name: str, ts: float, attributes: Dict[str, str] | None, metrics: Dict[str, float] | None) -> bytes: +def _encode_telemetry_event( + name: str, ts: float, attributes: Dict[str, str] | None, metrics: Dict[str, float] | None +) -> bytes: msg = bytearray() msg += _string(1, name) msg += _len_delimited(2, _timestamp_message(ts)) @@ -253,7 +268,7 @@ def _normalize_events(events: List[dict]) -> List[bytes]: # Note: URL is never logged for privacy "connection_type": "websocket", } - + # Add detailed error information to attributes if e.get("error_type"): attrs["error_type"] = str(e["error_type"]) @@ -265,7 +280,7 @@ def _normalize_events(events: List[dict]) -> List[bytes]: attrs["timeout_occurred"] = str(e["timeout_occurred"]) if e.get("duration_ms"): attrs["duration_ms"] = str(e["duration_ms"]) - + # Add WebSocket handshake failure details if e.get("handshake_status_code"): attrs["handshake_status_code"] = str(e["handshake_status_code"]) @@ -278,27 +293,27 @@ def _normalize_events(events: List[dict]) -> List[bytes]: handshake_headers = e["handshake_response_headers"] for header_name, header_value in handshake_headers.items(): # Prefix with 'handshake_' to distinguish from request headers - safe_header_name = header_name.lower().replace('-', '_') + safe_header_name = header_name.lower().replace("-", "_") attrs[f"handshake_{safe_header_name}"] = str(header_value) - + # Add connection parameters if available if e.get("connection_params"): for key, value in e["connection_params"].items(): if value is not None: attrs[f"connection_{key}"] = str(value) - + # Add request_id if present for server-side correlation request_id = e.get("request_id") if request_id: attrs["request_id"] = str(request_id) - + # Include ALL extras in the attributes for comprehensive telemetry extras = e.get("extras", {}) if extras: for key, value in extras.items(): if value is not None and key not in attrs: attrs[str(key)] = str(value) - + rec = _encode_error_event( err_type=str(e.get("error_type", e.get("error", "Error"))), message=str(e.get("error_message", e.get("message", ""))), @@ -375,5 +390,3 @@ def encode_telemetry_batch_iter(events: List[dict], context: typing.Mapping[str, yield _len_delimited(1, _encode_telemetry_context(context)) for rec in _normalize_events(events): yield rec - -