Compare commits

...

14 Commits

Author SHA1 Message Date
J. Nick Koston
8219f15333 Merge branch 'dev' into less_than_64_do_not_calc 2025-05-28 19:51:56 -05:00
J. Nick Koston
3aac6c0c05 Merge branch 'dev' into less_than_64_do_not_calc 2025-05-28 18:16:54 -05:00
J. Nick Koston
7cb7aa7932 Merge branch 'dev' into less_than_64_do_not_calc 2025-05-27 01:31:31 -05:00
J. Nick Koston
0dc49d5016 Merge branch 'dev' into less_than_64_do_not_calc 2025-05-22 18:02:09 -05:00
J. Nick Koston
cc33881b45 Merge branch 'dev' into less_than_64_do_not_calc 2025-05-19 02:33:32 -04:00
J. Nick Koston
2a0e5d49d5 Merge branch 'dev' into less_than_64_do_not_calc 2025-05-18 20:11:08 -04:00
J. Nick Koston
4038bbd817 tweak 2025-05-16 15:27:59 -04:00
J. Nick Koston
9ce8fd8860 tweak 2025-05-16 15:27:02 -04:00
J. Nick Koston
5450b51300 tweak 2025-05-16 15:25:27 -04:00
J. Nick Koston
342a662a68 preen 2025-05-16 15:23:02 -04:00
J. Nick Koston
8ff20141ed preen 2025-05-16 15:22:53 -04:00
J. Nick Koston
52805122e7 preen 2025-05-16 15:22:26 -04:00
J. Nick Koston
3107e2f85e tweak 2025-05-16 14:06:54 -04:00
J. Nick Koston
8f152c5a0a Reduce protobuf message size calculation overhead
About 45% of these can be pre-calculated to the maximum size. We might
go a few bytes over but its a bit faster than having to calculate it out.
2025-05-16 13:59:44 -04:00
2 changed files with 112 additions and 126 deletions

View File

@@ -782,7 +782,7 @@ bool ConnectResponse::decode_varint(uint32_t field_id, ProtoVarInt value) {
}
void ConnectResponse::encode(ProtoWriteBuffer buffer) const { buffer.encode_bool(1, this->invalid_password); }
void ConnectResponse::calculate_size(uint32_t &total_size) const {
ProtoSize::add_bool_field(total_size, 1, this->invalid_password, false);
total_size += 2; // Pre-calculated maximum size
}
#ifdef HAS_PROTO_MESSAGE_DUMP
void ConnectResponse::dump_to(std::string &out) const {
@@ -1200,9 +1200,7 @@ void BinarySensorStateResponse::encode(ProtoWriteBuffer buffer) const {
buffer.encode_bool(3, this->missing_state);
}
void BinarySensorStateResponse::calculate_size(uint32_t &total_size) const {
ProtoSize::add_fixed_field<4>(total_size, 1, this->key != 0, false);
ProtoSize::add_bool_field(total_size, 1, this->state, false);
ProtoSize::add_bool_field(total_size, 1, this->missing_state, false);
total_size += 9; // Pre-calculated maximum size
}
#ifdef HAS_PROTO_MESSAGE_DUMP
void BinarySensorStateResponse::dump_to(std::string &out) const {
@@ -1412,11 +1410,7 @@ void CoverStateResponse::encode(ProtoWriteBuffer buffer) const {
buffer.encode_enum<enums::CoverOperation>(5, this->current_operation);
}
void CoverStateResponse::calculate_size(uint32_t &total_size) const {
ProtoSize::add_fixed_field<4>(total_size, 1, this->key != 0, false);
ProtoSize::add_enum_field(total_size, 1, static_cast<uint32_t>(this->legacy_state), false);
ProtoSize::add_fixed_field<4>(total_size, 1, this->position != 0.0f, false);
ProtoSize::add_fixed_field<4>(total_size, 1, this->tilt != 0.0f, false);
ProtoSize::add_enum_field(total_size, 1, static_cast<uint32_t>(this->current_operation), false);
total_size += 27; // Pre-calculated maximum size
}
#ifdef HAS_PROTO_MESSAGE_DUMP
void CoverStateResponse::dump_to(std::string &out) const {
@@ -1502,14 +1496,7 @@ void CoverCommandRequest::encode(ProtoWriteBuffer buffer) const {
buffer.encode_bool(8, this->stop);
}
void CoverCommandRequest::calculate_size(uint32_t &total_size) const {
ProtoSize::add_fixed_field<4>(total_size, 1, this->key != 0, false);
ProtoSize::add_bool_field(total_size, 1, this->has_legacy_command, false);
ProtoSize::add_enum_field(total_size, 1, static_cast<uint32_t>(this->legacy_command), false);
ProtoSize::add_bool_field(total_size, 1, this->has_position, false);
ProtoSize::add_fixed_field<4>(total_size, 1, this->position != 0.0f, false);
ProtoSize::add_bool_field(total_size, 1, this->has_tilt, false);
ProtoSize::add_fixed_field<4>(total_size, 1, this->tilt != 0.0f, false);
ProtoSize::add_bool_field(total_size, 1, this->stop, false);
total_size += 29; // Pre-calculated maximum size
}
#ifdef HAS_PROTO_MESSAGE_DUMP
void CoverCommandRequest::dump_to(std::string &out) const {
@@ -2835,9 +2822,7 @@ void SensorStateResponse::encode(ProtoWriteBuffer buffer) const {
buffer.encode_bool(3, this->missing_state);
}
void SensorStateResponse::calculate_size(uint32_t &total_size) const {
ProtoSize::add_fixed_field<4>(total_size, 1, this->key != 0, false);
ProtoSize::add_fixed_field<4>(total_size, 1, this->state != 0.0f, false);
ProtoSize::add_bool_field(total_size, 1, this->missing_state, false);
total_size += 12; // Pre-calculated maximum size
}
#ifdef HAS_PROTO_MESSAGE_DUMP
void SensorStateResponse::dump_to(std::string &out) const {
@@ -3003,8 +2988,7 @@ void SwitchStateResponse::encode(ProtoWriteBuffer buffer) const {
buffer.encode_bool(2, this->state);
}
void SwitchStateResponse::calculate_size(uint32_t &total_size) const {
ProtoSize::add_fixed_field<4>(total_size, 1, this->key != 0, false);
ProtoSize::add_bool_field(total_size, 1, this->state, false);
total_size += 7; // Pre-calculated maximum size
}
#ifdef HAS_PROTO_MESSAGE_DUMP
void SwitchStateResponse::dump_to(std::string &out) const {
@@ -3046,8 +3030,7 @@ void SwitchCommandRequest::encode(ProtoWriteBuffer buffer) const {
buffer.encode_bool(2, this->state);
}
void SwitchCommandRequest::calculate_size(uint32_t &total_size) const {
ProtoSize::add_fixed_field<4>(total_size, 1, this->key != 0, false);
ProtoSize::add_bool_field(total_size, 1, this->state, false);
total_size += 7; // Pre-calculated maximum size
}
#ifdef HAS_PROTO_MESSAGE_DUMP
void SwitchCommandRequest::dump_to(std::string &out) const {
@@ -3251,8 +3234,7 @@ void SubscribeLogsRequest::encode(ProtoWriteBuffer buffer) const {
buffer.encode_bool(2, this->dump_config);
}
void SubscribeLogsRequest::calculate_size(uint32_t &total_size) const {
ProtoSize::add_enum_field(total_size, 1, static_cast<uint32_t>(this->level), false);
ProtoSize::add_bool_field(total_size, 1, this->dump_config, false);
total_size += 8; // Pre-calculated maximum size
}
#ifdef HAS_PROTO_MESSAGE_DUMP
void SubscribeLogsRequest::dump_to(std::string &out) const {
@@ -3356,7 +3338,7 @@ bool NoiseEncryptionSetKeyResponse::decode_varint(uint32_t field_id, ProtoVarInt
}
void NoiseEncryptionSetKeyResponse::encode(ProtoWriteBuffer buffer) const { buffer.encode_bool(1, this->success); }
void NoiseEncryptionSetKeyResponse::calculate_size(uint32_t &total_size) const {
ProtoSize::add_bool_field(total_size, 1, this->success, false);
total_size += 2; // Pre-calculated maximum size
}
#ifdef HAS_PROTO_MESSAGE_DUMP
void NoiseEncryptionSetKeyResponse::dump_to(std::string &out) const {
@@ -3617,7 +3599,7 @@ bool GetTimeResponse::decode_32bit(uint32_t field_id, Proto32Bit value) {
}
void GetTimeResponse::encode(ProtoWriteBuffer buffer) const { buffer.encode_fixed32(1, this->epoch_seconds); }
void GetTimeResponse::calculate_size(uint32_t &total_size) const {
ProtoSize::add_fixed_field<4>(total_size, 1, this->epoch_seconds != 0, false);
total_size += 5; // Pre-calculated maximum size
}
#ifdef HAS_PROTO_MESSAGE_DUMP
void GetTimeResponse::dump_to(std::string &out) const {
@@ -4108,8 +4090,7 @@ void CameraImageRequest::encode(ProtoWriteBuffer buffer) const {
buffer.encode_bool(2, this->stream);
}
void CameraImageRequest::calculate_size(uint32_t &total_size) const {
ProtoSize::add_bool_field(total_size, 1, this->single, false);
ProtoSize::add_bool_field(total_size, 1, this->stream, false);
total_size += 4; // Pre-calculated maximum size
}
#ifdef HAS_PROTO_MESSAGE_DUMP
void CameraImageRequest::dump_to(std::string &out) const {
@@ -5098,9 +5079,7 @@ void NumberStateResponse::encode(ProtoWriteBuffer buffer) const {
buffer.encode_bool(3, this->missing_state);
}
void NumberStateResponse::calculate_size(uint32_t &total_size) const {
ProtoSize::add_fixed_field<4>(total_size, 1, this->key != 0, false);
ProtoSize::add_fixed_field<4>(total_size, 1, this->state != 0.0f, false);
ProtoSize::add_bool_field(total_size, 1, this->missing_state, false);
total_size += 12; // Pre-calculated maximum size
}
#ifdef HAS_PROTO_MESSAGE_DUMP
void NumberStateResponse::dump_to(std::string &out) const {
@@ -5141,8 +5120,7 @@ void NumberCommandRequest::encode(ProtoWriteBuffer buffer) const {
buffer.encode_float(2, this->state);
}
void NumberCommandRequest::calculate_size(uint32_t &total_size) const {
ProtoSize::add_fixed_field<4>(total_size, 1, this->key != 0, false);
ProtoSize::add_fixed_field<4>(total_size, 1, this->state != 0.0f, false);
total_size += 10; // Pre-calculated maximum size
}
#ifdef HAS_PROTO_MESSAGE_DUMP
void NumberCommandRequest::dump_to(std::string &out) const {
@@ -5541,8 +5519,7 @@ void SirenStateResponse::encode(ProtoWriteBuffer buffer) const {
buffer.encode_bool(2, this->state);
}
void SirenStateResponse::calculate_size(uint32_t &total_size) const {
ProtoSize::add_fixed_field<4>(total_size, 1, this->key != 0, false);
ProtoSize::add_bool_field(total_size, 1, this->state, false);
total_size += 7; // Pre-calculated maximum size
}
#ifdef HAS_PROTO_MESSAGE_DUMP
void SirenStateResponse::dump_to(std::string &out) const {
@@ -5844,8 +5821,7 @@ void LockStateResponse::encode(ProtoWriteBuffer buffer) const {
buffer.encode_enum<enums::LockState>(2, this->state);
}
void LockStateResponse::calculate_size(uint32_t &total_size) const {
ProtoSize::add_fixed_field<4>(total_size, 1, this->key != 0, false);
ProtoSize::add_enum_field(total_size, 1, static_cast<uint32_t>(this->state), false);
total_size += 11; // Pre-calculated maximum size
}
#ifdef HAS_PROTO_MESSAGE_DUMP
void LockStateResponse::dump_to(std::string &out) const {
@@ -6052,7 +6028,7 @@ bool ButtonCommandRequest::decode_32bit(uint32_t field_id, Proto32Bit value) {
}
void ButtonCommandRequest::encode(ProtoWriteBuffer buffer) const { buffer.encode_fixed32(1, this->key); }
void ButtonCommandRequest::calculate_size(uint32_t &total_size) const {
ProtoSize::add_fixed_field<4>(total_size, 1, this->key != 0, false);
total_size += 5; // Pre-calculated maximum size
}
#ifdef HAS_PROTO_MESSAGE_DUMP
void ButtonCommandRequest::dump_to(std::string &out) const {
@@ -6298,10 +6274,7 @@ void MediaPlayerStateResponse::encode(ProtoWriteBuffer buffer) const {
buffer.encode_bool(4, this->muted);
}
void MediaPlayerStateResponse::calculate_size(uint32_t &total_size) const {
ProtoSize::add_fixed_field<4>(total_size, 1, this->key != 0, false);
ProtoSize::add_enum_field(total_size, 1, static_cast<uint32_t>(this->state), false);
ProtoSize::add_fixed_field<4>(total_size, 1, this->volume != 0.0f, false);
ProtoSize::add_bool_field(total_size, 1, this->muted, false);
total_size += 18; // Pre-calculated maximum size
}
#ifdef HAS_PROTO_MESSAGE_DUMP
void MediaPlayerStateResponse::dump_to(std::string &out) const {
@@ -6461,7 +6434,7 @@ void SubscribeBluetoothLEAdvertisementsRequest::encode(ProtoWriteBuffer buffer)
buffer.encode_uint32(1, this->flags);
}
void SubscribeBluetoothLEAdvertisementsRequest::calculate_size(uint32_t &total_size) const {
ProtoSize::add_uint32_field(total_size, 1, this->flags, false);
total_size += 6; // Pre-calculated maximum size
}
#ifdef HAS_PROTO_MESSAGE_DUMP
void SubscribeBluetoothLEAdvertisementsRequest::dump_to(std::string &out) const {
@@ -6770,10 +6743,7 @@ void BluetoothDeviceRequest::encode(ProtoWriteBuffer buffer) const {
buffer.encode_uint32(4, this->address_type);
}
void BluetoothDeviceRequest::calculate_size(uint32_t &total_size) const {
ProtoSize::add_uint64_field(total_size, 1, this->address, false);
ProtoSize::add_enum_field(total_size, 1, static_cast<uint32_t>(this->request_type), false);
ProtoSize::add_bool_field(total_size, 1, this->has_address_type, false);
ProtoSize::add_uint32_field(total_size, 1, this->address_type, false);
total_size += 25; // Pre-calculated maximum size
}
#ifdef HAS_PROTO_MESSAGE_DUMP
void BluetoothDeviceRequest::dump_to(std::string &out) const {
@@ -6828,10 +6798,7 @@ void BluetoothDeviceConnectionResponse::encode(ProtoWriteBuffer buffer) const {
buffer.encode_int32(4, this->error);
}
void BluetoothDeviceConnectionResponse::calculate_size(uint32_t &total_size) const {
ProtoSize::add_uint64_field(total_size, 1, this->address, false);
ProtoSize::add_bool_field(total_size, 1, this->connected, false);
ProtoSize::add_uint32_field(total_size, 1, this->mtu, false);
ProtoSize::add_int32_field(total_size, 1, this->error, false);
total_size += 25; // Pre-calculated maximum size
}
#ifdef HAS_PROTO_MESSAGE_DUMP
void BluetoothDeviceConnectionResponse::dump_to(std::string &out) const {
@@ -6870,7 +6837,7 @@ bool BluetoothGATTGetServicesRequest::decode_varint(uint32_t field_id, ProtoVarI
}
void BluetoothGATTGetServicesRequest::encode(ProtoWriteBuffer buffer) const { buffer.encode_uint64(1, this->address); }
void BluetoothGATTGetServicesRequest::calculate_size(uint32_t &total_size) const {
ProtoSize::add_uint64_field(total_size, 1, this->address, false);
total_size += 11; // Pre-calculated maximum size
}
#ifdef HAS_PROTO_MESSAGE_DUMP
void BluetoothGATTGetServicesRequest::dump_to(std::string &out) const {
@@ -7133,7 +7100,7 @@ void BluetoothGATTGetServicesDoneResponse::encode(ProtoWriteBuffer buffer) const
buffer.encode_uint64(1, this->address);
}
void BluetoothGATTGetServicesDoneResponse::calculate_size(uint32_t &total_size) const {
ProtoSize::add_uint64_field(total_size, 1, this->address, false);
total_size += 11; // Pre-calculated maximum size
}
#ifdef HAS_PROTO_MESSAGE_DUMP
void BluetoothGATTGetServicesDoneResponse::dump_to(std::string &out) const {
@@ -7165,8 +7132,7 @@ void BluetoothGATTReadRequest::encode(ProtoWriteBuffer buffer) const {
buffer.encode_uint32(2, this->handle);
}
void BluetoothGATTReadRequest::calculate_size(uint32_t &total_size) const {
ProtoSize::add_uint64_field(total_size, 1, this->address, false);
ProtoSize::add_uint32_field(total_size, 1, this->handle, false);
total_size += 17; // Pre-calculated maximum size
}
#ifdef HAS_PROTO_MESSAGE_DUMP
void BluetoothGATTReadRequest::dump_to(std::string &out) const {
@@ -7321,8 +7287,7 @@ void BluetoothGATTReadDescriptorRequest::encode(ProtoWriteBuffer buffer) const {
buffer.encode_uint32(2, this->handle);
}
void BluetoothGATTReadDescriptorRequest::calculate_size(uint32_t &total_size) const {
ProtoSize::add_uint64_field(total_size, 1, this->address, false);
ProtoSize::add_uint32_field(total_size, 1, this->handle, false);
total_size += 17; // Pre-calculated maximum size
}
#ifdef HAS_PROTO_MESSAGE_DUMP
void BluetoothGATTReadDescriptorRequest::dump_to(std::string &out) const {
@@ -7418,9 +7383,7 @@ void BluetoothGATTNotifyRequest::encode(ProtoWriteBuffer buffer) const {
buffer.encode_bool(3, this->enable);
}
void BluetoothGATTNotifyRequest::calculate_size(uint32_t &total_size) const {
ProtoSize::add_uint64_field(total_size, 1, this->address, false);
ProtoSize::add_uint32_field(total_size, 1, this->handle, false);
ProtoSize::add_bool_field(total_size, 1, this->enable, false);
total_size += 19; // Pre-calculated maximum size
}
#ifdef HAS_PROTO_MESSAGE_DUMP
void BluetoothGATTNotifyRequest::dump_to(std::string &out) const {
@@ -7584,9 +7547,7 @@ void BluetoothGATTErrorResponse::encode(ProtoWriteBuffer buffer) const {
buffer.encode_int32(3, this->error);
}
void BluetoothGATTErrorResponse::calculate_size(uint32_t &total_size) const {
ProtoSize::add_uint64_field(total_size, 1, this->address, false);
ProtoSize::add_uint32_field(total_size, 1, this->handle, false);
ProtoSize::add_int32_field(total_size, 1, this->error, false);
total_size += 23; // Pre-calculated maximum size
}
#ifdef HAS_PROTO_MESSAGE_DUMP
void BluetoothGATTErrorResponse::dump_to(std::string &out) const {
@@ -7628,8 +7589,7 @@ void BluetoothGATTWriteResponse::encode(ProtoWriteBuffer buffer) const {
buffer.encode_uint32(2, this->handle);
}
void BluetoothGATTWriteResponse::calculate_size(uint32_t &total_size) const {
ProtoSize::add_uint64_field(total_size, 1, this->address, false);
ProtoSize::add_uint32_field(total_size, 1, this->handle, false);
total_size += 17; // Pre-calculated maximum size
}
#ifdef HAS_PROTO_MESSAGE_DUMP
void BluetoothGATTWriteResponse::dump_to(std::string &out) const {
@@ -7666,8 +7626,7 @@ void BluetoothGATTNotifyResponse::encode(ProtoWriteBuffer buffer) const {
buffer.encode_uint32(2, this->handle);
}
void BluetoothGATTNotifyResponse::calculate_size(uint32_t &total_size) const {
ProtoSize::add_uint64_field(total_size, 1, this->address, false);
ProtoSize::add_uint32_field(total_size, 1, this->handle, false);
total_size += 17; // Pre-calculated maximum size
}
#ifdef HAS_PROTO_MESSAGE_DUMP
void BluetoothGATTNotifyResponse::dump_to(std::string &out) const {
@@ -7709,9 +7668,7 @@ void BluetoothDevicePairingResponse::encode(ProtoWriteBuffer buffer) const {
buffer.encode_int32(3, this->error);
}
void BluetoothDevicePairingResponse::calculate_size(uint32_t &total_size) const {
ProtoSize::add_uint64_field(total_size, 1, this->address, false);
ProtoSize::add_bool_field(total_size, 1, this->paired, false);
ProtoSize::add_int32_field(total_size, 1, this->error, false);
total_size += 19; // Pre-calculated maximum size
}
#ifdef HAS_PROTO_MESSAGE_DUMP
void BluetoothDevicePairingResponse::dump_to(std::string &out) const {
@@ -7757,9 +7714,7 @@ void BluetoothDeviceUnpairingResponse::encode(ProtoWriteBuffer buffer) const {
buffer.encode_int32(3, this->error);
}
void BluetoothDeviceUnpairingResponse::calculate_size(uint32_t &total_size) const {
ProtoSize::add_uint64_field(total_size, 1, this->address, false);
ProtoSize::add_bool_field(total_size, 1, this->success, false);
ProtoSize::add_int32_field(total_size, 1, this->error, false);
total_size += 19; // Pre-calculated maximum size
}
#ifdef HAS_PROTO_MESSAGE_DUMP
void BluetoothDeviceUnpairingResponse::dump_to(std::string &out) const {
@@ -7812,9 +7767,7 @@ void BluetoothDeviceClearCacheResponse::encode(ProtoWriteBuffer buffer) const {
buffer.encode_int32(3, this->error);
}
void BluetoothDeviceClearCacheResponse::calculate_size(uint32_t &total_size) const {
ProtoSize::add_uint64_field(total_size, 1, this->address, false);
ProtoSize::add_bool_field(total_size, 1, this->success, false);
ProtoSize::add_int32_field(total_size, 1, this->error, false);
total_size += 19; // Pre-calculated maximum size
}
#ifdef HAS_PROTO_MESSAGE_DUMP
void BluetoothDeviceClearCacheResponse::dump_to(std::string &out) const {
@@ -7855,8 +7808,7 @@ void BluetoothScannerStateResponse::encode(ProtoWriteBuffer buffer) const {
buffer.encode_enum<enums::BluetoothScannerMode>(2, this->mode);
}
void BluetoothScannerStateResponse::calculate_size(uint32_t &total_size) const {
ProtoSize::add_enum_field(total_size, 1, static_cast<uint32_t>(this->state), false);
ProtoSize::add_enum_field(total_size, 1, static_cast<uint32_t>(this->mode), false);
total_size += 12; // Pre-calculated maximum size
}
#ifdef HAS_PROTO_MESSAGE_DUMP
void BluetoothScannerStateResponse::dump_to(std::string &out) const {
@@ -7886,7 +7838,7 @@ void BluetoothScannerSetModeRequest::encode(ProtoWriteBuffer buffer) const {
buffer.encode_enum<enums::BluetoothScannerMode>(1, this->mode);
}
void BluetoothScannerSetModeRequest::calculate_size(uint32_t &total_size) const {
ProtoSize::add_enum_field(total_size, 1, static_cast<uint32_t>(this->mode), false);
total_size += 6; // Pre-calculated maximum size
}
#ifdef HAS_PROTO_MESSAGE_DUMP
void BluetoothScannerSetModeRequest::dump_to(std::string &out) const {
@@ -7917,8 +7869,7 @@ void SubscribeVoiceAssistantRequest::encode(ProtoWriteBuffer buffer) const {
buffer.encode_uint32(2, this->flags);
}
void SubscribeVoiceAssistantRequest::calculate_size(uint32_t &total_size) const {
ProtoSize::add_bool_field(total_size, 1, this->subscribe, false);
ProtoSize::add_uint32_field(total_size, 1, this->flags, false);
total_size += 8; // Pre-calculated maximum size
}
#ifdef HAS_PROTO_MESSAGE_DUMP
void SubscribeVoiceAssistantRequest::dump_to(std::string &out) const {
@@ -7965,9 +7916,7 @@ void VoiceAssistantAudioSettings::encode(ProtoWriteBuffer buffer) const {
buffer.encode_float(3, this->volume_multiplier);
}
void VoiceAssistantAudioSettings::calculate_size(uint32_t &total_size) const {
ProtoSize::add_uint32_field(total_size, 1, this->noise_suppression_level, false);
ProtoSize::add_uint32_field(total_size, 1, this->auto_gain, false);
ProtoSize::add_fixed_field<4>(total_size, 1, this->volume_multiplier != 0.0f, false);
total_size += 17; // Pre-calculated maximum size
}
#ifdef HAS_PROTO_MESSAGE_DUMP
void VoiceAssistantAudioSettings::dump_to(std::string &out) const {
@@ -8082,8 +8031,7 @@ void VoiceAssistantResponse::encode(ProtoWriteBuffer buffer) const {
buffer.encode_bool(2, this->error);
}
void VoiceAssistantResponse::calculate_size(uint32_t &total_size) const {
ProtoSize::add_uint32_field(total_size, 1, this->port, false);
ProtoSize::add_bool_field(total_size, 1, this->error, false);
total_size += 8; // Pre-calculated maximum size
}
#ifdef HAS_PROTO_MESSAGE_DUMP
void VoiceAssistantResponse::dump_to(std::string &out) const {
@@ -8382,7 +8330,7 @@ bool VoiceAssistantAnnounceFinished::decode_varint(uint32_t field_id, ProtoVarIn
}
void VoiceAssistantAnnounceFinished::encode(ProtoWriteBuffer buffer) const { buffer.encode_bool(1, this->success); }
void VoiceAssistantAnnounceFinished::calculate_size(uint32_t &total_size) const {
ProtoSize::add_bool_field(total_size, 1, this->success, false);
total_size += 2; // Pre-calculated maximum size
}
#ifdef HAS_PROTO_MESSAGE_DUMP
void VoiceAssistantAnnounceFinished::dump_to(std::string &out) const {
@@ -8709,8 +8657,7 @@ void AlarmControlPanelStateResponse::encode(ProtoWriteBuffer buffer) const {
buffer.encode_enum<enums::AlarmControlPanelState>(2, this->state);
}
void AlarmControlPanelStateResponse::calculate_size(uint32_t &total_size) const {
ProtoSize::add_fixed_field<4>(total_size, 1, this->key != 0, false);
ProtoSize::add_enum_field(total_size, 1, static_cast<uint32_t>(this->state), false);
total_size += 11; // Pre-calculated maximum size
}
#ifdef HAS_PROTO_MESSAGE_DUMP
void AlarmControlPanelStateResponse::dump_to(std::string &out) const {
@@ -9168,11 +9115,7 @@ void DateStateResponse::encode(ProtoWriteBuffer buffer) const {
buffer.encode_uint32(5, this->day);
}
void DateStateResponse::calculate_size(uint32_t &total_size) const {
ProtoSize::add_fixed_field<4>(total_size, 1, this->key != 0, false);
ProtoSize::add_bool_field(total_size, 1, this->missing_state, false);
ProtoSize::add_uint32_field(total_size, 1, this->year, false);
ProtoSize::add_uint32_field(total_size, 1, this->month, false);
ProtoSize::add_uint32_field(total_size, 1, this->day, false);
total_size += 25; // Pre-calculated maximum size
}
#ifdef HAS_PROTO_MESSAGE_DUMP
void DateStateResponse::dump_to(std::string &out) const {
@@ -9239,10 +9182,7 @@ void DateCommandRequest::encode(ProtoWriteBuffer buffer) const {
buffer.encode_uint32(4, this->day);
}
void DateCommandRequest::calculate_size(uint32_t &total_size) const {
ProtoSize::add_fixed_field<4>(total_size, 1, this->key != 0, false);
ProtoSize::add_uint32_field(total_size, 1, this->year, false);
ProtoSize::add_uint32_field(total_size, 1, this->month, false);
ProtoSize::add_uint32_field(total_size, 1, this->day, false);
total_size += 23; // Pre-calculated maximum size
}
#ifdef HAS_PROTO_MESSAGE_DUMP
void DateCommandRequest::dump_to(std::string &out) const {
@@ -9409,11 +9349,7 @@ void TimeStateResponse::encode(ProtoWriteBuffer buffer) const {
buffer.encode_uint32(5, this->second);
}
void TimeStateResponse::calculate_size(uint32_t &total_size) const {
ProtoSize::add_fixed_field<4>(total_size, 1, this->key != 0, false);
ProtoSize::add_bool_field(total_size, 1, this->missing_state, false);
ProtoSize::add_uint32_field(total_size, 1, this->hour, false);
ProtoSize::add_uint32_field(total_size, 1, this->minute, false);
ProtoSize::add_uint32_field(total_size, 1, this->second, false);
total_size += 25; // Pre-calculated maximum size
}
#ifdef HAS_PROTO_MESSAGE_DUMP
void TimeStateResponse::dump_to(std::string &out) const {
@@ -9480,10 +9416,7 @@ void TimeCommandRequest::encode(ProtoWriteBuffer buffer) const {
buffer.encode_uint32(4, this->second);
}
void TimeCommandRequest::calculate_size(uint32_t &total_size) const {
ProtoSize::add_fixed_field<4>(total_size, 1, this->key != 0, false);
ProtoSize::add_uint32_field(total_size, 1, this->hour, false);
ProtoSize::add_uint32_field(total_size, 1, this->minute, false);
ProtoSize::add_uint32_field(total_size, 1, this->second, false);
total_size += 23; // Pre-calculated maximum size
}
#ifdef HAS_PROTO_MESSAGE_DUMP
void TimeCommandRequest::dump_to(std::string &out) const {
@@ -9850,9 +9783,7 @@ void ValveStateResponse::encode(ProtoWriteBuffer buffer) const {
buffer.encode_enum<enums::ValveOperation>(3, this->current_operation);
}
void ValveStateResponse::calculate_size(uint32_t &total_size) const {
ProtoSize::add_fixed_field<4>(total_size, 1, this->key != 0, false);
ProtoSize::add_fixed_field<4>(total_size, 1, this->position != 0.0f, false);
ProtoSize::add_enum_field(total_size, 1, static_cast<uint32_t>(this->current_operation), false);
total_size += 16; // Pre-calculated maximum size
}
#ifdef HAS_PROTO_MESSAGE_DUMP
void ValveStateResponse::dump_to(std::string &out) const {
@@ -9909,10 +9840,7 @@ void ValveCommandRequest::encode(ProtoWriteBuffer buffer) const {
buffer.encode_bool(4, this->stop);
}
void ValveCommandRequest::calculate_size(uint32_t &total_size) const {
ProtoSize::add_fixed_field<4>(total_size, 1, this->key != 0, false);
ProtoSize::add_bool_field(total_size, 1, this->has_position, false);
ProtoSize::add_fixed_field<4>(total_size, 1, this->position != 0.0f, false);
ProtoSize::add_bool_field(total_size, 1, this->stop, false);
total_size += 14; // Pre-calculated maximum size
}
#ifdef HAS_PROTO_MESSAGE_DUMP
void ValveCommandRequest::dump_to(std::string &out) const {
@@ -10067,9 +9995,7 @@ void DateTimeStateResponse::encode(ProtoWriteBuffer buffer) const {
buffer.encode_fixed32(3, this->epoch_seconds);
}
void DateTimeStateResponse::calculate_size(uint32_t &total_size) const {
ProtoSize::add_fixed_field<4>(total_size, 1, this->key != 0, false);
ProtoSize::add_bool_field(total_size, 1, this->missing_state, false);
ProtoSize::add_fixed_field<4>(total_size, 1, this->epoch_seconds != 0, false);
total_size += 12; // Pre-calculated maximum size
}
#ifdef HAS_PROTO_MESSAGE_DUMP
void DateTimeStateResponse::dump_to(std::string &out) const {
@@ -10110,8 +10036,7 @@ void DateTimeCommandRequest::encode(ProtoWriteBuffer buffer) const {
buffer.encode_fixed32(2, this->epoch_seconds);
}
void DateTimeCommandRequest::calculate_size(uint32_t &total_size) const {
ProtoSize::add_fixed_field<4>(total_size, 1, this->key != 0, false);
ProtoSize::add_fixed_field<4>(total_size, 1, this->epoch_seconds != 0, false);
total_size += 10; // Pre-calculated maximum size
}
#ifdef HAS_PROTO_MESSAGE_DUMP
void DateTimeCommandRequest::dump_to(std::string &out) const {
@@ -10393,8 +10318,7 @@ void UpdateCommandRequest::encode(ProtoWriteBuffer buffer) const {
buffer.encode_enum<enums::UpdateCommand>(2, this->command);
}
void UpdateCommandRequest::calculate_size(uint32_t &total_size) const {
ProtoSize::add_fixed_field<4>(total_size, 1, this->key != 0, false);
ProtoSize::add_enum_field(total_size, 1, static_cast<uint32_t>(this->command), false);
total_size += 11; // Pre-calculated maximum size
}
#ifdef HAS_PROTO_MESSAGE_DUMP
void UpdateCommandRequest::dump_to(std::string &out) const {

View File

@@ -90,6 +90,10 @@ def force_str(force: bool) -> str:
class TypeInfo(ABC):
"""Base class for all type information."""
# Maximum size for fixed-size types (in bytes)
# -1 for variable length types
maximum_size = -1
def __init__(self, field: descriptor.FieldDescriptorProto) -> None:
self._field = field
@@ -259,7 +263,7 @@ class TypeInfo(ABC):
"""
TYPE_INFO: dict[int, TypeInfo] = {}
TYPE_INFO: dict[int, type[TypeInfo]] = {}
def register_type(name: int):
@@ -280,6 +284,7 @@ class DoubleType(TypeInfo):
decode_64bit = "value.as_double()"
encode_func = "encode_double"
wire_type = WireType.FIXED64 # Uses wire type 1 according to protobuf spec
maximum_size = 8 # 8 bytes for double
def dump(self, name: str) -> str:
o = f'sprintf(buffer, "%g", {name});\n'
@@ -299,6 +304,7 @@ class FloatType(TypeInfo):
decode_32bit = "value.as_float()"
encode_func = "encode_float"
wire_type = WireType.FIXED32 # Uses wire type 5
maximum_size = 4 # 4 bytes for float
def dump(self, name: str) -> str:
o = f'sprintf(buffer, "%g", {name});\n'
@@ -318,6 +324,7 @@ class Int64Type(TypeInfo):
decode_varint = "value.as_int64()"
encode_func = "encode_int64"
wire_type = WireType.VARINT # Uses wire type 0
maximum_size = 10 # Maximum 10 bytes for varint64
def dump(self, name: str) -> str:
o = f'sprintf(buffer, "%lld", {name});\n'
@@ -337,6 +344,7 @@ class UInt64Type(TypeInfo):
decode_varint = "value.as_uint64()"
encode_func = "encode_uint64"
wire_type = WireType.VARINT # Uses wire type 0
maximum_size = 10 # Maximum 10 bytes for varint64
def dump(self, name: str) -> str:
o = f'sprintf(buffer, "%llu", {name});\n'
@@ -356,6 +364,7 @@ class Int32Type(TypeInfo):
decode_varint = "value.as_int32()"
encode_func = "encode_int32"
wire_type = WireType.VARINT # Uses wire type 0
maximum_size = 5 # Maximum 5 bytes for varint32
def dump(self, name: str) -> str:
o = f'sprintf(buffer, "%" PRId32, {name});\n'
@@ -375,6 +384,7 @@ class Fixed64Type(TypeInfo):
decode_64bit = "value.as_fixed64()"
encode_func = "encode_fixed64"
wire_type = WireType.FIXED64 # Uses wire type 1
maximum_size = 8 # 8 bytes for fixed64
def dump(self, name: str) -> str:
o = f'sprintf(buffer, "%llu", {name});\n'
@@ -394,6 +404,7 @@ class Fixed32Type(TypeInfo):
decode_32bit = "value.as_fixed32()"
encode_func = "encode_fixed32"
wire_type = WireType.FIXED32 # Uses wire type 5
maximum_size = 4 # 4 bytes for fixed32
def dump(self, name: str) -> str:
o = f'sprintf(buffer, "%" PRIu32, {name});\n'
@@ -413,6 +424,7 @@ class BoolType(TypeInfo):
decode_varint = "value.as_bool()"
encode_func = "encode_bool"
wire_type = WireType.VARINT # Uses wire type 0
maximum_size = 1 # 1 byte for bool
def dump(self, name: str) -> str:
o = f"out.append(YESNO({name}));"
@@ -506,6 +518,7 @@ class UInt32Type(TypeInfo):
decode_varint = "value.as_uint32()"
encode_func = "encode_uint32"
wire_type = WireType.VARINT # Uses wire type 0
maximum_size = 5 # Maximum 5 bytes for varint32
def dump(self, name: str) -> str:
o = f'sprintf(buffer, "%" PRIu32, {name});\n'
@@ -520,6 +533,8 @@ class UInt32Type(TypeInfo):
@register_type(14)
class EnumType(TypeInfo):
maximum_size = 5 # Maximum 5 bytes for enum (same as uint32)
@property
def cpp_type(self) -> str:
return f"enums::{self._field.type_name[1:]}"
@@ -552,6 +567,7 @@ class SFixed32Type(TypeInfo):
decode_32bit = "value.as_sfixed32()"
encode_func = "encode_sfixed32"
wire_type = WireType.FIXED32 # Uses wire type 5
maximum_size = 4 # 4 bytes for sfixed32
def dump(self, name: str) -> str:
o = f'sprintf(buffer, "%" PRId32, {name});\n'
@@ -571,6 +587,7 @@ class SFixed64Type(TypeInfo):
decode_64bit = "value.as_sfixed64()"
encode_func = "encode_sfixed64"
wire_type = WireType.FIXED64 # Uses wire type 1
maximum_size = 8 # 8 bytes for sfixed64
def dump(self, name: str) -> str:
o = f'sprintf(buffer, "%lld", {name});\n'
@@ -590,6 +607,7 @@ class SInt32Type(TypeInfo):
decode_varint = "value.as_sint32()"
encode_func = "encode_sint32"
wire_type = WireType.VARINT # Uses wire type 0
maximum_size = 5 # Maximum 5 bytes for sint32
def dump(self, name: str) -> str:
o = f'sprintf(buffer, "%" PRId32, {name});\n'
@@ -609,6 +627,7 @@ class SInt64Type(TypeInfo):
decode_varint = "value.as_sint64()"
encode_func = "encode_sint64"
wire_type = WireType.VARINT # Uses wire type 0
maximum_size = 10 # Maximum 10 bytes for sint64
def dump(self, name: str) -> str:
o = f'sprintf(buffer, "%lld", {name});\n'
@@ -622,9 +641,11 @@ class SInt64Type(TypeInfo):
class RepeatedTypeInfo(TypeInfo):
maximum_size = -1 # Repeated fields are variable length
def __init__(self, field: descriptor.FieldDescriptorProto) -> None:
super().__init__(field)
self._ti: TypeInfo = TYPE_INFO[field.type](field)
self._ti = TYPE_INFO[field.type](field)
@property
def cpp_type(self) -> str:
@@ -762,6 +783,40 @@ def build_enum_type(desc) -> tuple[str, str]:
return out, cpp
PROTOBUF_TYPE_SIZES = {
type_id: type_class.maximum_size for type_id, type_class in TYPE_INFO.items()
}
def calculate_fixed_message_size(desc: descriptor.DescriptorProto) -> int:
"""Calculate the maximum size of a fixed-size message.
Args:
desc: The descriptor of the message to calculate the size for.
Returns:
The maximum size of the message, or -1 if it has variable length
"""
total_size: int = 0
for field in desc.field:
if field.label == 3: # Repeated field
return -1 # Can't be fixed size
max_data_size = PROTOBUF_TYPE_SIZES[field.type]
if max_data_size == -1:
return -1
ti = TYPE_INFO[field.type]
# Get field ID size
field_id_size = ti(field).calculate_field_id_size()
# Total field size is field ID size + data size
total_size += field_id_size + max_data_size
return total_size
def build_message_type(desc: descriptor.DescriptorProto) -> tuple[str, str]:
public_content: list[str] = []
protected_content: list[str] = []
@@ -773,6 +828,9 @@ def build_message_type(desc: descriptor.DescriptorProto) -> tuple[str, str]:
dump: list[str] = []
size_calc: list[str] = []
# Calculate if this message has a fixed size
fixed_size = calculate_fixed_message_size(desc)
for field in desc.field:
if field.label == 3:
ti = RepeatedTypeInfo(field)
@@ -851,9 +909,13 @@ def build_message_type(desc: descriptor.DescriptorProto) -> tuple[str, str]:
# Add calculate_size method
o = f"void {desc.name}::calculate_size(uint32_t &total_size) const {{"
# Check if this message has a pre-calculated fixed size
if fixed_size > 0:
# For messages with no variable length or repeated fields, use pre-calculated size
o += f"\n total_size += {fixed_size}; // Pre-calculated maximum size\n"
# Add a check for empty/default objects to short-circuit the calculation
# Only add this optimization if we have fields to check
if size_calc:
elif size_calc:
# For a single field, just inline it for simplicity
if len(size_calc) == 1 and len(size_calc[0]) + len(o) + 3 < 120:
o += f" {size_calc[0]} "