From d217ab3cd4b7b63104f1b5484678fa1bc01dd638 Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Sun, 12 Apr 2026 19:23:39 -1000 Subject: [PATCH] [api] Add speed_optimized proto option for hot encode paths Add a new (speed_optimized) message option that emits __attribute__((optimize("O2"))) on the generated encode() and calculate_size() methods. Under -Os, GCC does not inline the small ProtoEncode helpers (write_raw_byte, encode_varint, etc.) into the generated methods, causing significant overhead on hot paths. Apply to SensorStateResponse and BluetoothLERawAdvertisementsResponse which are the highest-frequency encode paths. --- script/api_protobuf/api_protobuf.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/script/api_protobuf/api_protobuf.py b/script/api_protobuf/api_protobuf.py index d9fdadd375..4f52b07809 100755 --- a/script/api_protobuf/api_protobuf.py +++ b/script/api_protobuf/api_protobuf.py @@ -2682,8 +2682,7 @@ def build_message_type( # Check if this message wants speed-optimized encode/calculate_size. # When set, __attribute__((optimize("O2"))) is added to the definitions # so GCC inlines the small ProtoEncode helpers even under -Os. - speed_opt = getattr(pb, "speed_optimized", None) - is_speed_optimized = speed_opt is not None and get_opt(desc, speed_opt, False) + is_speed_optimized = get_opt(desc, pb.speed_optimized, False) speed_attr = '__attribute__((optimize("O2"))) ' if is_speed_optimized else "" # Only generate encode method if this message needs encoding and has fields