Commit Graph

146 Commits

Author SHA1 Message Date
J. Nick Koston 136eabebb4 Merge branch 'api-enum-auto-max-value' into integration
# Conflicts:
#	esphome/components/api/api_pb2.cpp
#	script/api_protobuf/api_protobuf.py
2026-04-06 14:07:38 -10:00
J. Nick Koston 7c671cd0e6 Merge branch 'api-proto-max-length' into integration
# Conflicts:
#	esphome/components/api/api_connection.cpp
#	esphome/components/api/api_pb2.cpp
#	esphome/components/api/proto.h
#	esphome/core/application.h
#	esphome/core/component.cpp
#	script/api_protobuf/api_protobuf.py
2026-04-06 14:02:14 -10:00
J. Nick Koston 87622c9a74 [api] Add max_data_length proto option and optimize entity string encoding
Add a max_data_length field option to api_options.proto for string/bytes
fields. When max_data_length < 128 and force = true, the code generator
uses encode_short_string_force() — a single call that writes the tag
byte, 1-byte length varint, and raw string data with no branching. Size
calculation simplifies from calc_length(1, size) to 2 + size.

Annotate entity fields across all 25 ListEntities*Response messages:
- name and object_id: max_data_length = 120, force = true (50 fields)
- icon: max_data_length = 63 (25 fields)

The 120 and 63 values match NAME_MAX_LENGTH and ICON_MAX_LENGTH in
esphome/core/config.py, validated at config time.
2026-04-06 13:28:50 -10:00
J. Nick Koston 37258de6b1 [api] Auto-derive max_value for enum fields in protobuf codegen
Scan all enum definitions in api.proto at codegen time and automatically
populate max_value for enum-typed fields. This eliminates the need for
manual (max_value) annotations on every enum field.

When max_value < 128, the generated calculate_size uses constant-size
arithmetic instead of a function call:
- `calc_uint32(1, static_cast<uint32_t>(field))` → `field ? 2 : 0`

For repeated enum fields with constant element size, the per-element
loop is replaced with a multiply: `size += count * bytes_per_element`.
2026-04-06 13:19:37 -10:00
J. Nick Koston 2b5ee69eb2 [api] Speed up protobuf encode 17-20% with register-optimized write path (#15290)
Co-authored-by: pre-commit-ci-lite[bot] <117423508+pre-commit-ci-lite[bot]@users.noreply.github.com>
2026-04-06 12:42:18 -10:00
J. Nick Koston 4099064207 Use write_short_string for non-forced strings with max_data_length < 128
Add empty check wrapper for non-forced short strings instead of falling
through to the general encode_string path.
2026-04-06 08:43:24 -10:00
J. Nick Koston 33285dfe27 Fix merge issues: write_short_string to static ProtoEncode, add debug arg
- Convert ProtoEncode::write_short_string to static method with pos param
- Fix codegen to emit ProtoEncode::write_short_string(pos, ...)
- Add PROTO_ENCODE_DEBUG_INIT to encode_fn call in encode_to_buffer

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-04-06 08:33:13 -10:00
J. Nick Koston 7122fb6f88 Merge remote-tracking branch 'upstream/api-sint32-short-varint' into integration
Fix double static_cast in enum encode precomputed tag path by
passing raw field ref instead of pre-cast value_expr.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-04-06 08:31:05 -10:00
J. Nick Koston 8fc7cdbe28 Merge remote-tracking branch 'upstream/api-enum-auto-max-value' into integration 2026-04-05 19:04:51 -10:00
J. Nick Koston fef4ff4a82 [api] Remove encode_small_varint — no real benefit over encode_uint32 2026-04-05 19:04:01 -10:00
J. Nick Koston 03d64317c7 Merge remote-tracking branch 'upstream/api-enum-auto-max-value' into integration 2026-04-05 18:58:09 -10:00
J. Nick Koston d5b5a6a4a7 [api] Don't use encode_small_varint for forced fields (must encode zero) 2026-04-05 18:57:04 -10:00
J. Nick Koston 307f436229 [api] Move zero-check into encode_small_varint to simplify call sites 2026-04-05 18:56:10 -10:00
J. Nick Koston 883a300785 [api] Add encode_small_varint helper for single-byte tag+value encoding
Replace two separate write_raw_byte calls with a single
encode_small_varint(tag, value) call that writes both bytes
with one bounds check. Used for enum fields with max < 128.
2026-04-05 18:53:48 -10:00
J. Nick Koston 5a6330e366 [api] Remove redundant uint32_t cast in enum raw byte writes 2026-04-05 18:51:14 -10:00
J. Nick Koston 5c178eab63 [api] Optimize encode for non-forced enum fields with single-byte tags
When an enum field has max_value < 128 and a single-byte tag, emit
inline write_raw_byte calls instead of calling encode_uint32, which
avoids encode_field_raw and encode_varint_raw function call overhead.
2026-04-05 18:50:23 -10:00
J. Nick Koston 3f7e636f7a Merge remote-tracking branch 'upstream/api-enum-auto-max-value' into integration 2026-04-05 18:40:32 -10:00
J. Nick Koston acd2fc3711 [api] Use multiply instead of loop for constant-size repeated enums
When a repeated enum field has a constant size per element (max < 128),
emit size * constant instead of iterating. This fixes unused variable
warnings from clang-tidy and generates more efficient code.
2026-04-05 18:29:01 -10:00
J. Nick Koston 0fc3664441 [api] Auto-derive max_value for enum fields in protobuf codegen
The protobuf code generator already parses all enum definitions and
knows every enum's maximum value. Use this to automatically apply the
max_value < 128 optimization to all enum fields, eliminating the need
for manual annotation.

Since every enum in api.proto has max < 128, all 58 enum field size
calculations now use constant arithmetic instead of calling varint
size functions.

Builds on #15424 which introduced the max_value optimization.
2026-04-05 18:16:14 -10:00
J. Nick Koston 1de94c1a84 [api] Add max_value proto option for constant-size varint codegen (#15424) 2026-04-05 18:02:06 -10:00
J. Nick Koston ef41caa347 Merge branch 'api-proto-max-length' into integration 2026-04-03 14:16:20 -10:00
J. Nick Koston 80502e8c39 [api] Rename encode_short_string to write_short_string 2026-04-03 14:15:19 -10:00
J. Nick Koston fd68e9a827 [api] Rename encode_raw_short_string to encode_short_string 2026-04-03 14:01:14 -10:00
J. Nick Koston 2d13def0ee [api] Add encode_raw_short_string for forced string fields with max_data_length
Replace 3-call sequence (write_raw_byte + write_raw_byte + encode_raw)
with single inlined encode_raw_short_string call for forced string
fields with max_data_length < 128. The compiler can hoist the pos
pointer across all three writes in one function boundary.
2026-04-03 14:00:01 -10:00
J. Nick Koston 675400cc31 [api] Use .empty() for string zero check in single-byte varint size 2026-04-03 13:51:25 -10:00
J. Nick Koston e174e579ed [api] Add max_data_length proto option and optimize entity name/object_id
Add max_data_length field option for string/bytes fields. When
max_data_length < 128, the codegen emits constant-size length varint
calculations and direct byte writes.

Annotate all entity name and object_id fields with
(max_data_length) = 120 and (force) = true across all 25
ListEntities*Response messages (50 fields).

Generated code changes:
- calculate_size: `calc_length(1, size)` -> `2 + size` (constant)
- encode: `encode_string(N, ref)` -> `write_raw_byte(tag) + write_raw_byte(len) + encode_raw(data, len)`

Eliminates 2 function calls per field per entity list response,
removes zero-check branches, and removes varint size computation.
2026-04-03 13:33:51 -10:00
J. Nick Koston 85d9f36028 Merge remote-tracking branch 'upstream/api-proto-max-value-codegen' into integration 2026-04-03 12:48:23 -10:00
J. Nick Koston 9ece286a26 [api] Simplify _get_single_byte_varint_size 2026-04-03 12:32:32 -10:00
J. Nick Koston fa80caf0ff [api] Fall back to RAW_ENCODE_MAP when SMALL_MAP has no entry 2026-04-03 12:31:44 -10:00
J. Nick Koston f3238a857e [api] Select encode map before lookup 2026-04-03 12:31:01 -10:00
J. Nick Koston 02b424388e [api] Use RAW_ENCODE_SMALL_MAP for max_value encode optimization
Replace inline encode_func check with a lookup table, matching the
existing RAW_ENCODE_MAP pattern.
2026-04-03 12:28:51 -10:00
J. Nick Koston 363d811cc1 [api] Use _get_simple_size_calculation for FixedArrayBytesType fallback
Replace inline calc_length/calc_length_force formatting with the
existing helper method.
2026-04-03 12:27:55 -10:00
J. Nick Koston 1e68a90ac9 [api] Extract _get_single_byte_varint_size helper in codegen
Refactor the constant-size varint pattern into a reusable helper
method on the TypeInfo base class, used by both UInt32Type (max_value)
and FixedArrayBytesType (fixed_array_size < 128).
2026-04-03 12:26:51 -10:00
J. Nick Koston e35aa729f3 [api] Add max_value proto option for constant-size varint codegen
Add a max_value field option to api_options.proto that tells the code
generator the maximum value a field can have. When max_value < 128,
the generated calculate_size() uses constant arithmetic instead of
calling varint size functions, and encode() uses direct byte writes
instead of varint encoding.

Also optimize FixedArrayBytesType: when fixed_array_size < 128, the
length varint is always 1 byte, so calculate_size() uses constant
arithmetic and encode() uses write_raw_byte for the length.

Applied to BluetoothLERawAdvertisement.address_type (max_value=4).

Measured on ESP32 (upstairsdesk89proxy):
- BluetoothLERawAdvertisement::calculate_size: 88 → 71 bytes (-19%)
- BluetoothLERawAdvertisement::encode: 199 → 179 bytes (-10%)
- Total BLE proxy hot path: 1807 → 1770 bytes (-37 bytes)
2026-04-03 12:21:37 -10:00
J. Nick Koston 52897fd067 [api] Add 2-byte inline fast path for sint32 varint encode/size
Add encode_varint_raw_short() and ProtoSize::varint_short() that
inline both the 1-byte and 2-byte varint paths, falling back to
the noinline slow path for 3+ bytes.

Use these for sint32 fields (zigzag encoding), where values like
RSSI (-100 to 0) produce zigzag values that are 1-2 bytes. This
avoids a function call for the common case without bloating the
generic encode_varint_raw fast path.
2026-03-28 17:27:40 -10:00
J. Nick Koston 793813790a [api] Precompute tag bytes for forced varint and length-delimited fields (#15067) 2026-03-24 01:52:39 +00:00
J. Nick Koston 382de7ca90 [api] Store dump strings in PROGMEM to save RAM on ESP8266 (#14982) 2026-03-23 13:40:53 -10:00
J. Nick Koston 36d2e58b11 [api] Make ProtoDecodableMessage::decode() non-virtual (#15076) 2026-03-23 08:23:08 -10:00
J. Nick Koston f4097d5a95 [api] Devirtualize API command dispatch (#15044) 2026-03-23 19:57:40 +13:00
J. Nick Koston fbe3e7d99c [api] Emit raw tag+value writes for forced fixed32 key fields (#15051) 2026-03-22 15:28:46 -10:00
J. Nick Koston 05d285ba86 [api] Fix heap-buffer-overflow in protobuf message dump for StringRef (#14721) 2026-03-12 07:16:53 -10:00
J. Nick Koston 6e468936ec [api] Inline ProtoVarInt::parse fast path and return consumed in struct (#14638)
Co-authored-by: Claude Opus 4.6 <noreply@anthropic.com>
2026-03-10 09:10:55 -10:00
J. Nick Koston be6c3c52ac [api] Add force proto field option to skip zero checks on hot path (#14610)
Co-authored-by: Claude Opus 4.6 <noreply@anthropic.com>
2026-03-07 18:59:13 -10:00
J. Nick Koston 77f2c371b2 [api] Single-pass protobuf encode for BLE proxy advertisements (#14575) 2026-03-07 07:26:34 -10:00
J. Nick Koston 42dbb51022 [api] Devirtualize protobuf encode/calculate_size (#14449)
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
2026-03-06 19:03:54 +00:00
J. Nick Koston f68a3ed15d [api] Remove virtual destructor from ProtoMessage (#14393) 2026-03-01 18:09:00 -10:00
J. Nick Koston 8bb577de64 [api] Split ProtoVarInt::parse into 32-bit and 64-bit phases (#14039) 2026-02-25 12:23:13 -06:00
J. Nick Koston f77da803c9 [api] Write protobuf encode output to pre-sized buffer directly (#14018) 2026-02-20 21:39:18 -06:00
J. Nick Koston 6c6da8a3cd [api] Skip class generation for empty SOURCE_CLIENT protobuf messages (#13880) 2026-02-09 18:45:24 +00:00
J. Nick Koston c658d7b57f [api] Merge auth check into base read_message, eliminate APIServerConnection (#13873) 2026-02-09 12:02:02 -06:00