mirror of
https://github.com/esphome/esphome.git
synced 2026-09-14 00:28:39 +00:00
Eliminate sub_size variable, accumulate directly into size
This commit is contained in:
@@ -2353,12 +2353,11 @@ uint32_t BluetoothLERawAdvertisementsResponse::calculate_size() const {
|
||||
uint32_t size = 0;
|
||||
for (uint16_t i = 0; i < this->advertisements_len; i++) {
|
||||
auto &sub_msg = this->advertisements[i];
|
||||
uint32_t sub_size = 0;
|
||||
sub_size += ProtoSize::calc_uint64_force(1, sub_msg.address);
|
||||
sub_size += ProtoSize::calc_sint32_force(1, sub_msg.rssi);
|
||||
sub_size += sub_msg.address_type ? 2 : 0;
|
||||
sub_size += 2 + sub_msg.data_len;
|
||||
size += 2 + sub_size;
|
||||
size += 2;
|
||||
size += ProtoSize::calc_uint64_force(1, sub_msg.address);
|
||||
size += ProtoSize::calc_sint32_force(1, sub_msg.rssi);
|
||||
size += sub_msg.address_type ? 2 : 0;
|
||||
size += 2 + sub_msg.data_len;
|
||||
}
|
||||
return size;
|
||||
}
|
||||
|
||||
@@ -1628,7 +1628,8 @@ def _generate_inline_size_block(
|
||||
|
||||
lines = []
|
||||
lines.append(f"auto &sub_msg = {element};")
|
||||
lines.append("uint32_t sub_size = 0;")
|
||||
# 1 byte tag + 1 byte length (guaranteed < 128 by validation)
|
||||
lines.append("size += 2;")
|
||||
|
||||
for field in sub_desc.field:
|
||||
if field.options.deprecated:
|
||||
@@ -1636,14 +1637,10 @@ def _generate_inline_size_block(
|
||||
ti = create_field_type_info(field, needs_decode=False, needs_encode=True)
|
||||
force = get_field_opt(field, pb.force, False)
|
||||
size_line = ti.get_size_calculation(f"sub_msg.{ti.field_name}", force)
|
||||
# Replace "size +=" with "sub_size +=" for the local accumulator
|
||||
size_line = size_line.replace("size +=", "sub_size +=")
|
||||
# Replace hardcoded this-> references (e.g., FixedArrayBytesType uses this->field_len)
|
||||
size_line = size_line.replace("this->", "sub_msg.")
|
||||
lines.extend(size_line.split("\n"))
|
||||
|
||||
# 1 byte tag + 1 byte length (guaranteed < 128) + body
|
||||
lines.append("size += 2 + sub_size;")
|
||||
return "\n".join(lines)
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user