1
0
mirror of https://github.com/esphome/esphome.git synced 2025-09-12 00:02:21 +01:00

Optimize API performance and flash usage by eliminating runtime message size lookup

This commit is contained in:
J. Nick Koston
2025-07-11 10:01:11 -10:00
parent bef20b60d0
commit a3806e4de2
7 changed files with 350 additions and 461 deletions

View File

@@ -987,13 +987,24 @@ def build_message_type(
# Add MESSAGE_TYPE method if this is a service message
if message_id is not None:
# Validate that message_id fits in uint8_t
if message_id > 255:
raise ValueError(
f"Message ID {message_id} for {desc.name} exceeds uint8_t maximum (255)"
)
# Add static constexpr for message type
public_content.append(f"static constexpr uint16_t MESSAGE_TYPE = {message_id};")
public_content.append(f"static constexpr uint8_t MESSAGE_TYPE = {message_id};")
# Add estimated size constant
estimated_size = calculate_message_estimated_size(desc)
# Validate that estimated_size fits in uint8_t
if estimated_size > 255:
raise ValueError(
f"Estimated size {estimated_size} for {desc.name} exceeds uint8_t maximum (255)"
)
public_content.append(
f"static constexpr uint16_t ESTIMATED_SIZE = {estimated_size};"
f"static constexpr uint8_t ESTIMATED_SIZE = {estimated_size};"
)
# Add message_name method inline in header