mirror of
https://github.com/esphome/esphome.git
synced 2026-09-15 17:18:40 +00:00
[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.
This commit is contained in:
@@ -1907,17 +1907,27 @@ class RepeatedTypeInfo(TypeInfo):
|
||||
size_expr = f"{name}->size()" if self._use_pointer else f"{name}.size()"
|
||||
o += f" size += {size_expr} * {bytes_per_element};\n"
|
||||
else:
|
||||
# Other types need the actual value
|
||||
# Check if inner type produces a constant size (doesn't depend on value)
|
||||
inner_size = self._ti.get_size_calculation("it", True)
|
||||
if "it" not in inner_size:
|
||||
# Constant size per element — use multiply instead of loop
|
||||
# Extract the constant from "size += N;"
|
||||
const_val = (
|
||||
inner_size.strip().removeprefix("size += ").removesuffix(";")
|
||||
)
|
||||
size_expr = f"{name}->size()" if self._use_pointer else f"{name}.size()"
|
||||
o += f" size += {size_expr} * {const_val};\n"
|
||||
# Special handling for const char* elements
|
||||
if self._use_pointer and "const char" in self._container_no_template:
|
||||
elif self._use_pointer and "const char" in self._container_no_template:
|
||||
field_id_size = self.calculate_field_id_size()
|
||||
o += f" for (const char *it : {container_ref}) {{\n"
|
||||
o += f" size += ProtoSize::calc_length_force({field_id_size}, strlen(it));\n"
|
||||
o += " }\n"
|
||||
else:
|
||||
auto_ref = "" if self._ti_is_bool else "&"
|
||||
o += f" for (const auto {auto_ref}it : {container_ref}) {{\n"
|
||||
o += f" {self._ti.get_size_calculation('it', True)}\n"
|
||||
o += " }\n"
|
||||
o += f" {inner_size}\n"
|
||||
o += " }\n"
|
||||
|
||||
o += "}"
|
||||
return o
|
||||
|
||||
Reference in New Issue
Block a user