address lint comment

This commit is contained in:
J. Nick Koston 2025-07-11 14:36:38 -10:00
parent 95786ce269
commit 6f5f378857
No known key found for this signature in database
3 changed files with 48 additions and 26 deletions

View File

@ -2200,9 +2200,7 @@ void ExecuteServiceArgument::calculate_size(uint32_t &total_size) const {
} }
} }
if (!this->float_array.empty()) { if (!this->float_array.empty()) {
for (const auto &it : this->float_array) { total_size += this->float_array.size() * 5;
ProtoSize::add_fixed_field_repeated<4>(total_size, 1);
}
} }
if (!this->string_array.empty()) { if (!this->string_array.empty()) {
for (const auto &it : this->string_array) { for (const auto &it : this->string_array) {

View File

@ -233,18 +233,6 @@ class ProtoSize {
total_size += field_id_size + NumBytes; total_size += field_id_size + NumBytes;
} }
/**
* @brief Calculates and adds the size of a fixed field to the total message size (repeated field version)
*
* @tparam NumBytes The number of bytes for this fixed field (4 or 8)
*/
template<uint32_t NumBytes>
static inline void add_fixed_field_repeated(uint32_t &total_size, uint32_t field_id_size) {
// Always calculate size for repeated fields
// Fixed fields always take exactly NumBytes
total_size += field_id_size + NumBytes;
}
/** /**
* @brief Calculates and adds the size of an enum field to the total message size * @brief Calculates and adds the size of an enum field to the total message size
* *

View File

@ -277,15 +277,13 @@ class TypeInfo(ABC):
zero_check: Expression to check for zero value (e.g., "!= 0.0f") zero_check: Expression to check for zero value (e.g., "!= 0.0f")
""" """
field_id_size = self.calculate_field_id_size() field_id_size = self.calculate_field_id_size()
method = ( # Fixed-size repeated fields are handled differently in RepeatedTypeInfo
f"add_fixed_field_repeated<{num_bytes}>" # so we should never get force=True here
if force assert not force, (
else f"add_fixed_field<{num_bytes}>" "Fixed-size repeated fields should be handled by RepeatedTypeInfo"
) )
if force: method = f"add_fixed_field<{num_bytes}>"
return f"ProtoSize::{method}(total_size, {field_id_size});" return f"ProtoSize::{method}(total_size, {field_id_size}, {name} {zero_check});"
else:
return f"ProtoSize::{method}(total_size, {field_id_size}, {name} {zero_check});"
@abstractmethod @abstractmethod
def get_size_calculation(self, name: str, force: bool = False) -> str: def get_size_calculation(self, name: str, force: bool = False) -> str:
@ -296,6 +294,14 @@ class TypeInfo(ABC):
force: Whether to force encoding the field even if it has a default value force: Whether to force encoding the field even if it has a default value
""" """
def get_fixed_size_bytes(self) -> int | None:
"""Get the number of bytes for fixed-size fields (float, double, fixed32, etc).
Returns:
The number of bytes (4 or 8) for fixed-size fields, None for variable-size fields.
"""
return None
@abstractmethod @abstractmethod
def get_estimated_size(self) -> int: def get_estimated_size(self) -> int:
"""Get estimated size in bytes for this field with typical values. """Get estimated size in bytes for this field with typical values.
@ -335,6 +341,9 @@ class DoubleType(TypeInfo):
def get_size_calculation(self, name: str, force: bool = False) -> str: def get_size_calculation(self, name: str, force: bool = False) -> str:
return self._get_fixed_size_calculation(name, force, 8, "!= 0.0") return self._get_fixed_size_calculation(name, force, 8, "!= 0.0")
def get_fixed_size_bytes(self) -> int:
return 8
def get_estimated_size(self) -> int: def get_estimated_size(self) -> int:
return self.calculate_field_id_size() + 8 # field ID + 8 bytes for double return self.calculate_field_id_size() + 8 # field ID + 8 bytes for double
@ -355,6 +364,9 @@ class FloatType(TypeInfo):
def get_size_calculation(self, name: str, force: bool = False) -> str: def get_size_calculation(self, name: str, force: bool = False) -> str:
return self._get_fixed_size_calculation(name, force, 4, "!= 0.0f") return self._get_fixed_size_calculation(name, force, 4, "!= 0.0f")
def get_fixed_size_bytes(self) -> int:
return 4
def get_estimated_size(self) -> int: def get_estimated_size(self) -> int:
return self.calculate_field_id_size() + 4 # field ID + 4 bytes for float return self.calculate_field_id_size() + 4 # field ID + 4 bytes for float
@ -435,6 +447,9 @@ class Fixed64Type(TypeInfo):
def get_size_calculation(self, name: str, force: bool = False) -> str: def get_size_calculation(self, name: str, force: bool = False) -> str:
return self._get_fixed_size_calculation(name, force, 8, "!= 0") return self._get_fixed_size_calculation(name, force, 8, "!= 0")
def get_fixed_size_bytes(self) -> int:
return 8
def get_estimated_size(self) -> int: def get_estimated_size(self) -> int:
return self.calculate_field_id_size() + 8 # field ID + 8 bytes fixed return self.calculate_field_id_size() + 8 # field ID + 8 bytes fixed
@ -455,6 +470,9 @@ class Fixed32Type(TypeInfo):
def get_size_calculation(self, name: str, force: bool = False) -> str: def get_size_calculation(self, name: str, force: bool = False) -> str:
return self._get_fixed_size_calculation(name, force, 4, "!= 0") return self._get_fixed_size_calculation(name, force, 4, "!= 0")
def get_fixed_size_bytes(self) -> int:
return 4
def get_estimated_size(self) -> int: def get_estimated_size(self) -> int:
return self.calculate_field_id_size() + 4 # field ID + 4 bytes fixed return self.calculate_field_id_size() + 4 # field ID + 4 bytes fixed
@ -628,6 +646,9 @@ class SFixed32Type(TypeInfo):
def get_size_calculation(self, name: str, force: bool = False) -> str: def get_size_calculation(self, name: str, force: bool = False) -> str:
return self._get_fixed_size_calculation(name, force, 4, "!= 0") return self._get_fixed_size_calculation(name, force, 4, "!= 0")
def get_fixed_size_bytes(self) -> int:
return 4
def get_estimated_size(self) -> int: def get_estimated_size(self) -> int:
return self.calculate_field_id_size() + 4 # field ID + 4 bytes fixed return self.calculate_field_id_size() + 4 # field ID + 4 bytes fixed
@ -648,6 +669,9 @@ class SFixed64Type(TypeInfo):
def get_size_calculation(self, name: str, force: bool = False) -> str: def get_size_calculation(self, name: str, force: bool = False) -> str:
return self._get_fixed_size_calculation(name, force, 8, "!= 0") return self._get_fixed_size_calculation(name, force, 8, "!= 0")
def get_fixed_size_bytes(self) -> int:
return 8
def get_estimated_size(self) -> int: def get_estimated_size(self) -> int:
return self.calculate_field_id_size() + 8 # field ID + 8 bytes fixed return self.calculate_field_id_size() + 8 # field ID + 8 bytes fixed
@ -801,11 +825,23 @@ class RepeatedTypeInfo(TypeInfo):
field_id_size = self._ti.calculate_field_id_size() field_id_size = self._ti.calculate_field_id_size()
o = f"ProtoSize::add_repeated_message(total_size, {field_id_size}, {name});" o = f"ProtoSize::add_repeated_message(total_size, {field_id_size}, {name});"
return o return o
# For other repeated types, use the underlying type's size calculation with force=True # For other repeated types, use the underlying type's size calculation with force=True
o = f"if (!{name}.empty()) {{\n" o = f"if (!{name}.empty()) {{\n"
o += f" for (const auto {'' if self._ti_is_bool else '&'}it : {name}) {{\n"
o += f" {self._ti.get_size_calculation('it', True)}\n" # Check if this is a fixed-size type by seeing if it has a fixed byte count
o += " }\n" num_bytes = self._ti.get_fixed_size_bytes()
if num_bytes is not None:
# Fixed types have constant size per element, so we can multiply
field_id_size = self._ti.calculate_field_id_size()
# Pre-calculate the total bytes per element
bytes_per_element = field_id_size + num_bytes
o += f" total_size += {name}.size() * {bytes_per_element};\n"
else:
# Other types need the actual value
o += f" for (const auto {'' if self._ti_is_bool else '&'}it : {name}) {{\n"
o += f" {self._ti.get_size_calculation('it', True)}\n"
o += " }\n"
o += "}" o += "}"
return o return o