[api] Add encode_small_varint helper for single-byte tag+value encoding

Replace two separate write_raw_byte calls with a single
encode_small_varint(tag, value) call that writes both bytes
with one bounds check. Used for enum fields with max < 128.
This commit is contained in:
J. Nick Koston
2026-04-05 18:53:48 -10:00
parent 5a6330e366
commit 883a300785
3 changed files with 69 additions and 108 deletions
+51 -102
View File
@@ -75,8 +75,7 @@ uint32_t DeviceInfo::calculate_size() const {
void SerialProxyInfo::encode(ProtoWriteBuffer &buffer) const {
buffer.encode_string(1, this->name);
if (this->port_type) {
buffer.write_raw_byte(16);
buffer.write_raw_byte(static_cast<uint8_t>(this->port_type));
buffer.encode_small_varint(16, static_cast<uint8_t>(this->port_type));
}
}
uint32_t SerialProxyInfo::calculate_size() const {
@@ -220,8 +219,7 @@ void ListEntitiesBinarySensorResponse::encode(ProtoWriteBuffer &buffer) const {
buffer.encode_string(8, this->icon);
#endif
if (this->entity_category) {
buffer.write_raw_byte(72);
buffer.write_raw_byte(static_cast<uint8_t>(this->entity_category));
buffer.encode_small_varint(72, static_cast<uint8_t>(this->entity_category));
}
#ifdef USE_DEVICES
buffer.encode_uint32(10, this->device_id);
@@ -277,8 +275,7 @@ void ListEntitiesCoverResponse::encode(ProtoWriteBuffer &buffer) const {
buffer.encode_string(10, this->icon);
#endif
if (this->entity_category) {
buffer.write_raw_byte(88);
buffer.write_raw_byte(static_cast<uint8_t>(this->entity_category));
buffer.encode_small_varint(88, static_cast<uint8_t>(this->entity_category));
}
buffer.encode_bool(12, this->supports_stop);
#ifdef USE_DEVICES
@@ -310,8 +307,7 @@ void CoverStateResponse::encode(ProtoWriteBuffer &buffer) const {
buffer.encode_float(3, this->position);
buffer.encode_float(4, this->tilt);
if (this->current_operation) {
buffer.write_raw_byte(40);
buffer.write_raw_byte(static_cast<uint8_t>(this->current_operation));
buffer.encode_small_varint(40, static_cast<uint8_t>(this->current_operation));
}
#ifdef USE_DEVICES
buffer.encode_uint32(6, this->device_id);
@@ -380,8 +376,7 @@ void ListEntitiesFanResponse::encode(ProtoWriteBuffer &buffer) const {
buffer.encode_string(10, this->icon);
#endif
if (this->entity_category) {
buffer.write_raw_byte(88);
buffer.write_raw_byte(static_cast<uint8_t>(this->entity_category));
buffer.encode_small_varint(88, static_cast<uint8_t>(this->entity_category));
}
for (const char *it : *this->supported_preset_modes) {
buffer.encode_string(12, it, strlen(it), true);
@@ -419,8 +414,7 @@ void FanStateResponse::encode(ProtoWriteBuffer &buffer) const {
buffer.encode_bool(2, this->state);
buffer.encode_bool(3, this->oscillating);
if (this->direction) {
buffer.write_raw_byte(40);
buffer.write_raw_byte(static_cast<uint8_t>(this->direction));
buffer.encode_small_varint(40, static_cast<uint8_t>(this->direction));
}
buffer.encode_int32(6, this->speed_level);
buffer.encode_string(7, this->preset_mode);
@@ -520,8 +514,7 @@ void ListEntitiesLightResponse::encode(ProtoWriteBuffer &buffer) const {
buffer.encode_string(14, this->icon);
#endif
if (this->entity_category) {
buffer.write_raw_byte(120);
buffer.write_raw_byte(static_cast<uint8_t>(this->entity_category));
buffer.encode_small_varint(120, static_cast<uint8_t>(this->entity_category));
}
#ifdef USE_DEVICES
buffer.encode_uint32(16, this->device_id);
@@ -557,8 +550,7 @@ void LightStateResponse::encode(ProtoWriteBuffer &buffer) const {
buffer.encode_bool(2, this->state);
buffer.encode_float(3, this->brightness);
if (this->color_mode) {
buffer.write_raw_byte(88);
buffer.write_raw_byte(static_cast<uint8_t>(this->color_mode));
buffer.encode_small_varint(88, static_cast<uint8_t>(this->color_mode));
}
buffer.encode_float(10, this->color_brightness);
buffer.encode_float(4, this->red);
@@ -715,13 +707,11 @@ void ListEntitiesSensorResponse::encode(ProtoWriteBuffer &buffer) const {
buffer.encode_bool(8, this->force_update);
buffer.encode_string(9, this->device_class);
if (this->state_class) {
buffer.write_raw_byte(80);
buffer.write_raw_byte(static_cast<uint8_t>(this->state_class));
buffer.encode_small_varint(80, static_cast<uint8_t>(this->state_class));
}
buffer.encode_bool(12, this->disabled_by_default);
if (this->entity_category) {
buffer.write_raw_byte(104);
buffer.write_raw_byte(static_cast<uint8_t>(this->entity_category));
buffer.encode_small_varint(104, static_cast<uint8_t>(this->entity_category));
}
#ifdef USE_DEVICES
buffer.encode_uint32(14, this->device_id);
@@ -777,8 +767,7 @@ void ListEntitiesSwitchResponse::encode(ProtoWriteBuffer &buffer) const {
buffer.encode_bool(6, this->assumed_state);
buffer.encode_bool(7, this->disabled_by_default);
if (this->entity_category) {
buffer.write_raw_byte(64);
buffer.write_raw_byte(static_cast<uint8_t>(this->entity_category));
buffer.encode_small_varint(64, static_cast<uint8_t>(this->entity_category));
}
buffer.encode_string(9, this->device_class);
#ifdef USE_DEVICES
@@ -854,8 +843,7 @@ void ListEntitiesTextSensorResponse::encode(ProtoWriteBuffer &buffer) const {
#endif
buffer.encode_bool(6, this->disabled_by_default);
if (this->entity_category) {
buffer.write_raw_byte(56);
buffer.write_raw_byte(static_cast<uint8_t>(this->entity_category));
buffer.encode_small_varint(56, static_cast<uint8_t>(this->entity_category));
}
buffer.encode_string(8, this->device_class);
#ifdef USE_DEVICES
@@ -912,8 +900,7 @@ bool SubscribeLogsRequest::decode_varint(uint32_t field_id, proto_varint_value_t
}
void SubscribeLogsResponse::encode(ProtoWriteBuffer &buffer) const {
if (this->level) {
buffer.write_raw_byte(8);
buffer.write_raw_byte(static_cast<uint8_t>(this->level));
buffer.encode_small_varint(8, static_cast<uint8_t>(this->level));
}
buffer.encode_bytes(3, this->message_ptr_, this->message_len_);
}
@@ -1152,8 +1139,7 @@ bool GetTimeResponse::decode_32bit(uint32_t field_id, Proto32Bit value) {
void ListEntitiesServicesArgument::encode(ProtoWriteBuffer &buffer) const {
buffer.encode_string(1, this->name);
if (this->type) {
buffer.write_raw_byte(16);
buffer.write_raw_byte(static_cast<uint8_t>(this->type));
buffer.encode_small_varint(16, static_cast<uint8_t>(this->type));
}
}
uint32_t ListEntitiesServicesArgument::calculate_size() const {
@@ -1169,8 +1155,7 @@ void ListEntitiesServicesResponse::encode(ProtoWriteBuffer &buffer) const {
buffer.encode_sub_message(3, it);
}
if (this->supports_response) {
buffer.write_raw_byte(32);
buffer.write_raw_byte(static_cast<uint8_t>(this->supports_response));
buffer.encode_small_varint(32, static_cast<uint8_t>(this->supports_response));
}
}
uint32_t ListEntitiesServicesResponse::calculate_size() const {
@@ -1319,8 +1304,7 @@ void ListEntitiesCameraResponse::encode(ProtoWriteBuffer &buffer) const {
buffer.encode_string(6, this->icon);
#endif
if (this->entity_category) {
buffer.write_raw_byte(56);
buffer.write_raw_byte(static_cast<uint8_t>(this->entity_category));
buffer.encode_small_varint(56, static_cast<uint8_t>(this->entity_category));
}
#ifdef USE_DEVICES
buffer.encode_uint32(8, this->device_id);
@@ -1469,29 +1453,24 @@ uint32_t ListEntitiesClimateResponse::calculate_size() const {
void ClimateStateResponse::encode(ProtoWriteBuffer &buffer) const {
buffer.write_tag_and_fixed32(13, this->key);
if (this->mode) {
buffer.write_raw_byte(16);
buffer.write_raw_byte(static_cast<uint8_t>(this->mode));
buffer.encode_small_varint(16, static_cast<uint8_t>(this->mode));
}
buffer.encode_float(3, this->current_temperature);
buffer.encode_float(4, this->target_temperature);
buffer.encode_float(5, this->target_temperature_low);
buffer.encode_float(6, this->target_temperature_high);
if (this->action) {
buffer.write_raw_byte(64);
buffer.write_raw_byte(static_cast<uint8_t>(this->action));
buffer.encode_small_varint(64, static_cast<uint8_t>(this->action));
}
if (this->fan_mode) {
buffer.write_raw_byte(72);
buffer.write_raw_byte(static_cast<uint8_t>(this->fan_mode));
buffer.encode_small_varint(72, static_cast<uint8_t>(this->fan_mode));
}
if (this->swing_mode) {
buffer.write_raw_byte(80);
buffer.write_raw_byte(static_cast<uint8_t>(this->swing_mode));
buffer.encode_small_varint(80, static_cast<uint8_t>(this->swing_mode));
}
buffer.encode_string(11, this->custom_fan_mode);
if (this->preset) {
buffer.write_raw_byte(96);
buffer.write_raw_byte(static_cast<uint8_t>(this->preset));
buffer.encode_small_varint(96, static_cast<uint8_t>(this->preset));
}
buffer.encode_string(13, this->custom_preset);
buffer.encode_float(14, this->current_humidity);
@@ -1623,8 +1602,7 @@ void ListEntitiesWaterHeaterResponse::encode(ProtoWriteBuffer &buffer) const {
#endif
buffer.encode_bool(5, this->disabled_by_default);
if (this->entity_category) {
buffer.write_raw_byte(48);
buffer.write_raw_byte(static_cast<uint8_t>(this->entity_category));
buffer.encode_small_varint(48, static_cast<uint8_t>(this->entity_category));
}
#ifdef USE_DEVICES
buffer.encode_uint32(7, this->device_id);
@@ -1664,8 +1642,7 @@ void WaterHeaterStateResponse::encode(ProtoWriteBuffer &buffer) const {
buffer.encode_float(2, this->current_temperature);
buffer.encode_float(3, this->target_temperature);
if (this->mode) {
buffer.write_raw_byte(32);
buffer.write_raw_byte(static_cast<uint8_t>(this->mode));
buffer.encode_small_varint(32, static_cast<uint8_t>(this->mode));
}
#ifdef USE_DEVICES
buffer.encode_uint32(5, this->device_id);
@@ -1742,13 +1719,11 @@ void ListEntitiesNumberResponse::encode(ProtoWriteBuffer &buffer) const {
buffer.encode_float(8, this->step);
buffer.encode_bool(9, this->disabled_by_default);
if (this->entity_category) {
buffer.write_raw_byte(80);
buffer.write_raw_byte(static_cast<uint8_t>(this->entity_category));
buffer.encode_small_varint(80, static_cast<uint8_t>(this->entity_category));
}
buffer.encode_string(11, this->unit_of_measurement);
if (this->mode) {
buffer.write_raw_byte(96);
buffer.write_raw_byte(static_cast<uint8_t>(this->mode));
buffer.encode_small_varint(96, static_cast<uint8_t>(this->mode));
}
buffer.encode_string(13, this->device_class);
#ifdef USE_DEVICES
@@ -1833,8 +1808,7 @@ void ListEntitiesSelectResponse::encode(ProtoWriteBuffer &buffer) const {
}
buffer.encode_bool(7, this->disabled_by_default);
if (this->entity_category) {
buffer.write_raw_byte(64);
buffer.write_raw_byte(static_cast<uint8_t>(this->entity_category));
buffer.encode_small_varint(64, static_cast<uint8_t>(this->entity_category));
}
#ifdef USE_DEVICES
buffer.encode_uint32(9, this->device_id);
@@ -1927,8 +1901,7 @@ void ListEntitiesSirenResponse::encode(ProtoWriteBuffer &buffer) const {
buffer.encode_bool(8, this->supports_duration);
buffer.encode_bool(9, this->supports_volume);
if (this->entity_category) {
buffer.write_raw_byte(80);
buffer.write_raw_byte(static_cast<uint8_t>(this->entity_category));
buffer.encode_small_varint(80, static_cast<uint8_t>(this->entity_category));
}
#ifdef USE_DEVICES
buffer.encode_uint32(11, this->device_id);
@@ -2037,8 +2010,7 @@ void ListEntitiesLockResponse::encode(ProtoWriteBuffer &buffer) const {
#endif
buffer.encode_bool(6, this->disabled_by_default);
if (this->entity_category) {
buffer.write_raw_byte(56);
buffer.write_raw_byte(static_cast<uint8_t>(this->entity_category));
buffer.encode_small_varint(56, static_cast<uint8_t>(this->entity_category));
}
buffer.encode_bool(8, this->assumed_state);
buffer.encode_bool(9, this->supports_open);
@@ -2070,8 +2042,7 @@ uint32_t ListEntitiesLockResponse::calculate_size() const {
void LockStateResponse::encode(ProtoWriteBuffer &buffer) const {
buffer.write_tag_and_fixed32(13, this->key);
if (this->state) {
buffer.write_raw_byte(16);
buffer.write_raw_byte(static_cast<uint8_t>(this->state));
buffer.encode_small_varint(16, static_cast<uint8_t>(this->state));
}
#ifdef USE_DEVICES
buffer.encode_uint32(3, this->device_id);
@@ -2136,8 +2107,7 @@ void ListEntitiesButtonResponse::encode(ProtoWriteBuffer &buffer) const {
#endif
buffer.encode_bool(6, this->disabled_by_default);
if (this->entity_category) {
buffer.write_raw_byte(56);
buffer.write_raw_byte(static_cast<uint8_t>(this->entity_category));
buffer.encode_small_varint(56, static_cast<uint8_t>(this->entity_category));
}
buffer.encode_string(8, this->device_class);
#ifdef USE_DEVICES
@@ -2189,8 +2159,7 @@ void MediaPlayerSupportedFormat::encode(ProtoWriteBuffer &buffer) const {
buffer.encode_uint32(2, this->sample_rate);
buffer.encode_uint32(3, this->num_channels);
if (this->purpose) {
buffer.write_raw_byte(32);
buffer.write_raw_byte(static_cast<uint8_t>(this->purpose));
buffer.encode_small_varint(32, static_cast<uint8_t>(this->purpose));
}
buffer.encode_uint32(5, this->sample_bytes);
}
@@ -2212,8 +2181,7 @@ void ListEntitiesMediaPlayerResponse::encode(ProtoWriteBuffer &buffer) const {
#endif
buffer.encode_bool(6, this->disabled_by_default);
if (this->entity_category) {
buffer.write_raw_byte(56);
buffer.write_raw_byte(static_cast<uint8_t>(this->entity_category));
buffer.encode_small_varint(56, static_cast<uint8_t>(this->entity_category));
}
buffer.encode_bool(8, this->supports_pause);
for (auto &it : this->supported_formats) {
@@ -2249,8 +2217,7 @@ uint32_t ListEntitiesMediaPlayerResponse::calculate_size() const {
void MediaPlayerStateResponse::encode(ProtoWriteBuffer &buffer) const {
buffer.write_tag_and_fixed32(13, this->key);
if (this->state) {
buffer.write_raw_byte(16);
buffer.write_raw_byte(static_cast<uint8_t>(this->state));
buffer.encode_small_varint(16, static_cast<uint8_t>(this->state));
}
buffer.encode_float(3, this->volume);
buffer.encode_bool(4, this->muted);
@@ -2713,16 +2680,13 @@ uint32_t BluetoothDeviceClearCacheResponse::calculate_size() const {
}
void BluetoothScannerStateResponse::encode(ProtoWriteBuffer &buffer) const {
if (this->state) {
buffer.write_raw_byte(8);
buffer.write_raw_byte(static_cast<uint8_t>(this->state));
buffer.encode_small_varint(8, static_cast<uint8_t>(this->state));
}
if (this->mode) {
buffer.write_raw_byte(16);
buffer.write_raw_byte(static_cast<uint8_t>(this->mode));
buffer.encode_small_varint(16, static_cast<uint8_t>(this->mode));
}
if (this->configured_mode) {
buffer.write_raw_byte(24);
buffer.write_raw_byte(static_cast<uint8_t>(this->configured_mode));
buffer.encode_small_varint(24, static_cast<uint8_t>(this->configured_mode));
}
}
uint32_t BluetoothScannerStateResponse::calculate_size() const {
@@ -3049,8 +3013,7 @@ void ListEntitiesAlarmControlPanelResponse::encode(ProtoWriteBuffer &buffer) con
#endif
buffer.encode_bool(6, this->disabled_by_default);
if (this->entity_category) {
buffer.write_raw_byte(56);
buffer.write_raw_byte(static_cast<uint8_t>(this->entity_category));
buffer.encode_small_varint(56, static_cast<uint8_t>(this->entity_category));
}
buffer.encode_uint32(8, this->supported_features);
buffer.encode_bool(9, this->requires_code);
@@ -3080,8 +3043,7 @@ uint32_t ListEntitiesAlarmControlPanelResponse::calculate_size() const {
void AlarmControlPanelStateResponse::encode(ProtoWriteBuffer &buffer) const {
buffer.write_tag_and_fixed32(13, this->key);
if (this->state) {
buffer.write_raw_byte(16);
buffer.write_raw_byte(static_cast<uint8_t>(this->state));
buffer.encode_small_varint(16, static_cast<uint8_t>(this->state));
}
#ifdef USE_DEVICES
buffer.encode_uint32(3, this->device_id);
@@ -3143,15 +3105,13 @@ void ListEntitiesTextResponse::encode(ProtoWriteBuffer &buffer) const {
#endif
buffer.encode_bool(6, this->disabled_by_default);
if (this->entity_category) {
buffer.write_raw_byte(56);
buffer.write_raw_byte(static_cast<uint8_t>(this->entity_category));
buffer.encode_small_varint(56, static_cast<uint8_t>(this->entity_category));
}
buffer.encode_uint32(8, this->min_length);
buffer.encode_uint32(9, this->max_length);
buffer.encode_string(10, this->pattern);
if (this->mode) {
buffer.write_raw_byte(88);
buffer.write_raw_byte(static_cast<uint8_t>(this->mode));
buffer.encode_small_varint(88, static_cast<uint8_t>(this->mode));
}
#ifdef USE_DEVICES
buffer.encode_uint32(12, this->device_id);
@@ -3238,8 +3198,7 @@ void ListEntitiesDateResponse::encode(ProtoWriteBuffer &buffer) const {
#endif
buffer.encode_bool(6, this->disabled_by_default);
if (this->entity_category) {
buffer.write_raw_byte(56);
buffer.write_raw_byte(static_cast<uint8_t>(this->entity_category));
buffer.encode_small_varint(56, static_cast<uint8_t>(this->entity_category));
}
#ifdef USE_DEVICES
buffer.encode_uint32(8, this->device_id);
@@ -3324,8 +3283,7 @@ void ListEntitiesTimeResponse::encode(ProtoWriteBuffer &buffer) const {
#endif
buffer.encode_bool(6, this->disabled_by_default);
if (this->entity_category) {
buffer.write_raw_byte(56);
buffer.write_raw_byte(static_cast<uint8_t>(this->entity_category));
buffer.encode_small_varint(56, static_cast<uint8_t>(this->entity_category));
}
#ifdef USE_DEVICES
buffer.encode_uint32(8, this->device_id);
@@ -3410,8 +3368,7 @@ void ListEntitiesEventResponse::encode(ProtoWriteBuffer &buffer) const {
#endif
buffer.encode_bool(6, this->disabled_by_default);
if (this->entity_category) {
buffer.write_raw_byte(56);
buffer.write_raw_byte(static_cast<uint8_t>(this->entity_category));
buffer.encode_small_varint(56, static_cast<uint8_t>(this->entity_category));
}
buffer.encode_string(8, this->device_class);
for (const char *it : *this->event_types) {
@@ -3469,8 +3426,7 @@ void ListEntitiesValveResponse::encode(ProtoWriteBuffer &buffer) const {
#endif
buffer.encode_bool(6, this->disabled_by_default);
if (this->entity_category) {
buffer.write_raw_byte(56);
buffer.write_raw_byte(static_cast<uint8_t>(this->entity_category));
buffer.encode_small_varint(56, static_cast<uint8_t>(this->entity_category));
}
buffer.encode_string(8, this->device_class);
buffer.encode_bool(9, this->assumed_state);
@@ -3503,8 +3459,7 @@ void ValveStateResponse::encode(ProtoWriteBuffer &buffer) const {
buffer.write_tag_and_fixed32(13, this->key);
buffer.encode_float(2, this->position);
if (this->current_operation) {
buffer.write_raw_byte(24);
buffer.write_raw_byte(static_cast<uint8_t>(this->current_operation));
buffer.encode_small_varint(24, static_cast<uint8_t>(this->current_operation));
}
#ifdef USE_DEVICES
buffer.encode_uint32(4, this->device_id);
@@ -3562,8 +3517,7 @@ void ListEntitiesDateTimeResponse::encode(ProtoWriteBuffer &buffer) const {
#endif
buffer.encode_bool(6, this->disabled_by_default);
if (this->entity_category) {
buffer.write_raw_byte(56);
buffer.write_raw_byte(static_cast<uint8_t>(this->entity_category));
buffer.encode_small_varint(56, static_cast<uint8_t>(this->entity_category));
}
#ifdef USE_DEVICES
buffer.encode_uint32(8, this->device_id);
@@ -3638,8 +3592,7 @@ void ListEntitiesUpdateResponse::encode(ProtoWriteBuffer &buffer) const {
#endif
buffer.encode_bool(6, this->disabled_by_default);
if (this->entity_category) {
buffer.write_raw_byte(56);
buffer.write_raw_byte(static_cast<uint8_t>(this->entity_category));
buffer.encode_small_varint(56, static_cast<uint8_t>(this->entity_category));
}
buffer.encode_string(8, this->device_class);
#ifdef USE_DEVICES
@@ -3763,8 +3716,7 @@ bool ZWaveProxyRequest::decode_length(uint32_t field_id, ProtoLengthDelimited va
}
void ZWaveProxyRequest::encode(ProtoWriteBuffer &buffer) const {
if (this->type) {
buffer.write_raw_byte(8);
buffer.write_raw_byte(static_cast<uint8_t>(this->type));
buffer.encode_small_varint(8, static_cast<uint8_t>(this->type));
}
buffer.encode_bytes(2, this->data, this->data_len);
}
@@ -3785,8 +3737,7 @@ void ListEntitiesInfraredResponse::encode(ProtoWriteBuffer &buffer) const {
#endif
buffer.encode_bool(5, this->disabled_by_default);
if (this->entity_category) {
buffer.write_raw_byte(48);
buffer.write_raw_byte(static_cast<uint8_t>(this->entity_category));
buffer.encode_small_varint(48, static_cast<uint8_t>(this->entity_category));
}
#ifdef USE_DEVICES
buffer.encode_uint32(7, this->device_id);
@@ -3984,12 +3935,10 @@ bool SerialProxyRequest::decode_varint(uint32_t field_id, proto_varint_value_t v
void SerialProxyRequestResponse::encode(ProtoWriteBuffer &buffer) const {
buffer.encode_uint32(1, this->instance);
if (this->type) {
buffer.write_raw_byte(16);
buffer.write_raw_byte(static_cast<uint8_t>(this->type));
buffer.encode_small_varint(16, static_cast<uint8_t>(this->type));
}
if (this->status) {
buffer.write_raw_byte(24);
buffer.write_raw_byte(static_cast<uint8_t>(this->status));
buffer.encode_small_varint(24, static_cast<uint8_t>(this->status));
}
buffer.encode_string(4, this->error_message);
}
+7
View File
@@ -276,6 +276,13 @@ class ProtoWriteBuffer {
void encode_bytes(uint32_t field_id, const uint8_t *data, size_t len, bool force = false) {
this->encode_string(field_id, reinterpret_cast<const char *>(data), len, force);
}
/** Encode a small varint field where both tag and value fit in one byte each.
* The tag is precomputed by the code generator. value must be < 128. */
void encode_small_varint(uint8_t precomputed_tag, uint8_t value) {
this->debug_check_bounds_(2);
*this->pos_++ = precomputed_tag;
*this->pos_++ = value;
}
void encode_uint32(uint32_t field_id, uint32_t value, bool force = false) {
if (value == 0 && !force)
return;
+11 -6
View File
@@ -244,10 +244,10 @@ class TypeInfo(ABC):
"encode_bool": "buffer.write_raw_byte({value} ? 0x01 : 0x00);",
}
# When max_value < 128, the varint is always 1 byte — use a direct byte write
# When max_value < 128, the varint is always 1 byte — use encode_small_varint
RAW_ENCODE_SMALL_MAP: dict[str, str] = {
"encode_uint32": "buffer.write_raw_byte(static_cast<uint8_t>({value}));",
"encode_uint64": "buffer.write_raw_byte(static_cast<uint8_t>({value}));",
"encode_uint32": "buffer.encode_small_varint({tag}, static_cast<uint8_t>({value}));",
"encode_uint64": "buffer.encode_small_varint({tag}, static_cast<uint8_t>({value}));",
}
def _encode_with_precomputed_tag(self, value_expr: str) -> str | None:
@@ -255,7 +255,7 @@ class TypeInfo(ABC):
Returns the raw encode string if the tag is a single byte and the
encode_func has a known raw equivalent, or None otherwise.
When max_value < 128, uses direct byte write instead of varint encoding.
When max_value < 128, uses encode_small_varint instead of varint encoding.
"""
if not self.force:
return None
@@ -270,6 +270,12 @@ class TypeInfo(ABC):
raw_expr = self.RAW_ENCODE_MAP.get(self.encode_func)
if raw_expr is None:
return None
if (
max_val is not None
and max_val < 128
and self.encode_func in self.RAW_ENCODE_SMALL_MAP
):
return raw_expr.format(tag=tag, value=value_expr)
return f"buffer.write_raw_byte({tag});\n{raw_expr.format(value=value_expr)}"
def _encode_bytes_with_precomputed_tag(
@@ -1341,8 +1347,7 @@ class EnumType(TypeInfo):
if tag < 128:
return (
f"if (this->{self.field_name}) {{\n"
f" buffer.write_raw_byte({tag});\n"
f" buffer.write_raw_byte(static_cast<uint8_t>(this->{self.field_name}));\n"
f" buffer.encode_small_varint({tag}, static_cast<uint8_t>(this->{self.field_name}));\n"
f"}}"
)
if self.force: