mirror of
https://github.com/esphome/esphome.git
synced 2026-02-23 09:48:24 -07:00
[api] Split ProtoVarInt::parse into 32-bit and 64-bit phases
On 32-bit platforms (ESP32 Xtensa), 64-bit shifts in varint parsing compile to __ashldi3 library calls. Since the vast majority of protobuf varint fields (message types, sizes, enum values, sensor readings) fit in 4 bytes, the 64-bit arithmetic is unnecessary overhead on the common path. Split parse() into two phases: - Bytes 0-3: uint32_t loop with native 32-bit shifts (0, 7, 14, 21) - Bytes 4-9: noinline parse_wide_() with uint64_t, only for BLE addresses and other 64-bit fields The code generator auto-detects which proto messages use int64/uint64/ sint64 fields and emits USE_API_VARINT64 conditionally. On non-BLE configs, parse_wide_() and the 64-bit accessors (as_uint64, as_int64, as_sint64) are compiled out entirely. Saves ~40 bytes flash on non-BLE configs. Benchmark shows 25-50% faster parsing for 1-4 byte varints (the common case).
This commit is contained in:
@@ -1905,6 +1905,34 @@ def build_type_usage_map(
|
||||
)
|
||||
|
||||
|
||||
def get_varint64_ifdef(
|
||||
file_desc: descriptor.FileDescriptorProto,
|
||||
message_ifdef_map: dict[str, str | None],
|
||||
) -> tuple[bool, str | None]:
|
||||
"""Check if 64-bit varint fields exist and get their common ifdef guard.
|
||||
|
||||
Returns:
|
||||
(has_varint64, ifdef_guard) - has_varint64 is True if any fields exist,
|
||||
ifdef_guard is the common guard or None if unconditional.
|
||||
"""
|
||||
varint64_types = {
|
||||
FieldDescriptorProto.TYPE_INT64,
|
||||
FieldDescriptorProto.TYPE_UINT64,
|
||||
FieldDescriptorProto.TYPE_SINT64,
|
||||
}
|
||||
ifdefs: set[str | None] = {
|
||||
message_ifdef_map.get(msg.name)
|
||||
for msg in file_desc.message_type
|
||||
if not msg.options.deprecated
|
||||
for field in msg.field
|
||||
if not field.options.deprecated and field.type in varint64_types
|
||||
}
|
||||
if not ifdefs:
|
||||
return False, None
|
||||
ifdefs.discard(None)
|
||||
return True, ifdefs.pop() if len(ifdefs) == 1 else None
|
||||
|
||||
|
||||
def build_enum_type(desc, enum_ifdef_map) -> tuple[str, str, str]:
|
||||
"""Builds the enum type.
|
||||
|
||||
@@ -2559,11 +2587,28 @@ def main() -> None:
|
||||
|
||||
file = d.file[0]
|
||||
|
||||
# Build dynamic ifdef mappings early so we can emit USE_API_VARINT64 before includes
|
||||
enum_ifdef_map, message_ifdef_map, message_source_map, used_messages = (
|
||||
build_type_usage_map(file)
|
||||
)
|
||||
|
||||
# Find the ifdef guard for 64-bit varint fields (int64/uint64/sint64).
|
||||
# Emitted before proto.h so parse_wide_() and 64-bit accessors are available.
|
||||
has_varint64, varint64_guard = get_varint64_ifdef(file, message_ifdef_map)
|
||||
|
||||
content = FILE_HEADER
|
||||
content += """\
|
||||
#pragma once
|
||||
|
||||
#include "esphome/core/defines.h"
|
||||
"""
|
||||
if has_varint64:
|
||||
content += "\n".join(
|
||||
wrap_with_ifdef(["#define USE_API_VARINT64"], varint64_guard)
|
||||
)
|
||||
content += "\n"
|
||||
|
||||
content += """\
|
||||
#include "esphome/core/string_ref.h"
|
||||
|
||||
#include "proto.h"
|
||||
@@ -2694,11 +2739,6 @@ static void dump_bytes_field(DumpBuffer &out, const char *field_name, const uint
|
||||
|
||||
content += "namespace enums {\n\n"
|
||||
|
||||
# Build dynamic ifdef mappings for both enums and messages
|
||||
enum_ifdef_map, message_ifdef_map, message_source_map, used_messages = (
|
||||
build_type_usage_map(file)
|
||||
)
|
||||
|
||||
# Simple grouping of enums by ifdef
|
||||
current_ifdef = None
|
||||
|
||||
|
||||
Reference in New Issue
Block a user