Remove dead code: 64-bit protobuf types never used in 7 years (#9471)

This commit is contained in:
J. Nick Koston 2025-07-15 15:12:12 -10:00 committed by GitHub
parent 6486147da1
commit 856cb182fc
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 42 additions and 67 deletions

View File

@ -175,23 +175,7 @@ class Proto32Bit {
const uint32_t value_; const uint32_t value_;
}; };
class Proto64Bit { // NOTE: Proto64Bit class removed - wire type 1 (64-bit fixed) not supported
public:
explicit Proto64Bit(uint64_t value) : value_(value) {}
uint64_t as_fixed64() const { return this->value_; }
int64_t as_sfixed64() const { return static_cast<int64_t>(this->value_); }
double as_double() const {
union {
uint64_t raw;
double value;
} s{};
s.raw = this->value_;
return s.value;
}
protected:
const uint64_t value_;
};
class ProtoWriteBuffer { class ProtoWriteBuffer {
public: public:
@ -258,20 +242,10 @@ class ProtoWriteBuffer {
this->write((value >> 16) & 0xFF); this->write((value >> 16) & 0xFF);
this->write((value >> 24) & 0xFF); this->write((value >> 24) & 0xFF);
} }
void encode_fixed64(uint32_t field_id, uint64_t value, bool force = false) { // NOTE: Wire type 1 (64-bit fixed: double, fixed64, sfixed64) is intentionally
if (value == 0 && !force) // not supported to reduce overhead on embedded systems. All ESPHome devices are
return; // 32-bit microcontrollers where 64-bit operations are expensive. If 64-bit support
// is needed in the future, the necessary encoding/decoding functions must be added.
this->encode_field_raw(field_id, 1); // type 1: 64-bit fixed64
this->write((value >> 0) & 0xFF);
this->write((value >> 8) & 0xFF);
this->write((value >> 16) & 0xFF);
this->write((value >> 24) & 0xFF);
this->write((value >> 32) & 0xFF);
this->write((value >> 40) & 0xFF);
this->write((value >> 48) & 0xFF);
this->write((value >> 56) & 0xFF);
}
void encode_float(uint32_t field_id, float value, bool force = false) { void encode_float(uint32_t field_id, float value, bool force = false) {
if (value == 0.0f && !force) if (value == 0.0f && !force)
return; return;
@ -337,7 +311,7 @@ class ProtoMessage {
virtual bool decode_varint(uint32_t field_id, ProtoVarInt value) { return false; } virtual bool decode_varint(uint32_t field_id, ProtoVarInt value) { return false; }
virtual bool decode_length(uint32_t field_id, ProtoLengthDelimited value) { return false; } virtual bool decode_length(uint32_t field_id, ProtoLengthDelimited value) { return false; }
virtual bool decode_32bit(uint32_t field_id, Proto32Bit value) { return false; } virtual bool decode_32bit(uint32_t field_id, Proto32Bit value) { return false; }
virtual bool decode_64bit(uint32_t field_id, Proto64Bit value) { return false; } // NOTE: decode_64bit removed - wire type 1 not supported
}; };
class ProtoSize { class ProtoSize {
@ -662,33 +636,8 @@ class ProtoSize {
total_size += field_id_size + varint(value); total_size += field_id_size + varint(value);
} }
/** // NOTE: sint64 support functions (add_sint64_field, add_sint64_field_repeated) removed
* @brief Calculates and adds the size of a sint64 field to the total message size // sint64 type is not supported by ESPHome API to reduce overhead on embedded systems
*
* Sint64 fields use ZigZag encoding, which is more efficient for negative values.
*/
static inline void add_sint64_field(uint32_t &total_size, uint32_t field_id_size, int64_t value) {
// Skip calculation if value is zero
if (value == 0) {
return; // No need to update total_size
}
// ZigZag encoding for sint64: (n << 1) ^ (n >> 63)
uint64_t zigzag = (static_cast<uint64_t>(value) << 1) ^ (static_cast<uint64_t>(value >> 63));
total_size += field_id_size + varint(zigzag);
}
/**
* @brief Calculates and adds the size of a sint64 field to the total message size (repeated field version)
*
* Sint64 fields use ZigZag encoding, which is more efficient for negative values.
*/
static inline void add_sint64_field_repeated(uint32_t &total_size, uint32_t field_id_size, int64_t value) {
// Always calculate size for repeated fields
// ZigZag encoding for sint64: (n << 1) ^ (n >> 63)
uint64_t zigzag = (static_cast<uint64_t>(value) << 1) ^ (static_cast<uint64_t>(value >> 63));
total_size += field_id_size + varint(zigzag);
}
/** /**
* @brief Calculates and adds the size of a string/bytes field to the total message size * @brief Calculates and adds the size of a string/bytes field to the total message size

View File

@ -313,6 +313,37 @@ class TypeInfo(ABC):
TYPE_INFO: dict[int, TypeInfo] = {} TYPE_INFO: dict[int, TypeInfo] = {}
# Unsupported 64-bit types that would add overhead for embedded systems
# TYPE_DOUBLE = 1, TYPE_FIXED64 = 6, TYPE_SFIXED64 = 16, TYPE_SINT64 = 18
UNSUPPORTED_TYPES = {1: "double", 6: "fixed64", 16: "sfixed64", 18: "sint64"}
def validate_field_type(field_type: int, field_name: str = "") -> None:
"""Validate that the field type is supported by ESPHome API.
Raises ValueError for unsupported 64-bit types.
"""
if field_type in UNSUPPORTED_TYPES:
type_name = UNSUPPORTED_TYPES[field_type]
field_info = f" (field: {field_name})" if field_name else ""
raise ValueError(
f"64-bit type '{type_name}'{field_info} is not supported by ESPHome API. "
"These types add significant overhead for embedded systems. "
"If you need 64-bit support, please add the necessary encoding/decoding "
"functions to proto.h/proto.cpp first."
)
def get_type_info_for_field(field: descriptor.FieldDescriptorProto) -> TypeInfo:
"""Get the appropriate TypeInfo for a field, handling repeated fields.
Also validates that the field type is supported.
"""
if field.label == 3: # repeated
return RepeatedTypeInfo(field)
validate_field_type(field.type, field.name)
return TYPE_INFO[field.type](field)
def register_type(name: int): def register_type(name: int):
"""Decorator to register a type with a name and number.""" """Decorator to register a type with a name and number."""
@ -738,6 +769,7 @@ class SInt64Type(TypeInfo):
class RepeatedTypeInfo(TypeInfo): class RepeatedTypeInfo(TypeInfo):
def __init__(self, field: descriptor.FieldDescriptorProto) -> None: def __init__(self, field: descriptor.FieldDescriptorProto) -> None:
super().__init__(field) super().__init__(field)
validate_field_type(field.type, field.name)
self._ti: TypeInfo = TYPE_INFO[field.type](field) self._ti: TypeInfo = TYPE_INFO[field.type](field)
@property @property
@ -1025,10 +1057,7 @@ def calculate_message_estimated_size(desc: descriptor.DescriptorProto) -> int:
total_size = 0 total_size = 0
for field in desc.field: for field in desc.field:
if field.label == 3: # repeated ti = get_type_info_for_field(field)
ti = RepeatedTypeInfo(field)
else:
ti = TYPE_INFO[field.type](field)
# Add estimated size for this field # Add estimated size for this field
total_size += ti.get_estimated_size() total_size += ti.get_estimated_size()
@ -1334,10 +1363,7 @@ def build_base_class(
# For base classes, we only declare the fields but don't handle encode/decode # For base classes, we only declare the fields but don't handle encode/decode
# The derived classes will handle encoding/decoding with their specific field numbers # The derived classes will handle encoding/decoding with their specific field numbers
for field in common_fields: for field in common_fields:
if field.label == 3: # repeated ti = get_type_info_for_field(field)
ti = RepeatedTypeInfo(field)
else:
ti = TYPE_INFO[field.type](field)
# Only add field declarations, not encode/decode logic # Only add field declarations, not encode/decode logic
protected_content.extend(ti.protected_content) protected_content.extend(ti.protected_content)