Skip to content

Commit c860fa3

Browse files
Make WireFormat::(ByteSize|_InternalSerialize) LazyField compatible (take #3 but disabled)
PiperOrigin-RevId: 675319986
1 parent a80bc8a commit c860fa3

File tree

3 files changed

+53
-0
lines changed

3 files changed

+53
-0
lines changed

src/google/protobuf/extension_set.cc

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -513,6 +513,31 @@ const int& ExtensionSet::GetRefRepeatedEnum(int number, int index) const {
513513
return extension->ptr.repeated_enum_value->Get(index);
514514
}
515515

516+
size_t ExtensionSet::GetMessageByteSizeLong(int number) const {
517+
const Extension* extension = FindOrNull(number);
518+
ABSL_CHECK(extension != nullptr) << "not present";
519+
ABSL_DCHECK_TYPE(*extension, OPTIONAL_FIELD, MESSAGE);
520+
return extension->is_lazy ? extension->ptr.lazymessage_value->ByteSizeLong()
521+
: extension->ptr.message_value->ByteSizeLong();
522+
}
523+
524+
uint8_t* ExtensionSet::InternalSerializeMessage(
525+
int number, const MessageLite* prototype, uint8_t* target,
526+
io::EpsCopyOutputStream* stream) const {
527+
const Extension* extension = FindOrNull(number);
528+
ABSL_CHECK(extension != nullptr) << "not present";
529+
ABSL_DCHECK_TYPE(*extension, OPTIONAL_FIELD, MESSAGE);
530+
531+
if (extension->is_lazy) {
532+
return extension->ptr.lazymessage_value->WriteMessageToArray(
533+
prototype, number, target, stream);
534+
}
535+
536+
const auto* msg = extension->ptr.message_value;
537+
return WireFormatLite::InternalWriteMessage(
538+
number, *msg, msg->GetCachedSize(), target, stream);
539+
}
540+
516541
void ExtensionSet::SetRepeatedEnum(int number, int index, int value) {
517542
Extension* extension = FindOrNull(number);
518543
ABSL_CHECK(extension != nullptr) << "Index out-of-bounds (field is empty).";

src/google/protobuf/extension_set.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -576,6 +576,11 @@ class PROTOBUF_EXPORT ExtensionSet {
576576
const bool& GetRefRepeatedBool(int number, int index) const;
577577
const int& GetRefRepeatedEnum(int number, int index) const;
578578

579+
size_t GetMessageByteSizeLong(int number) const;
580+
uint8_t* InternalSerializeMessage(int number, const MessageLite* prototype,
581+
uint8_t* target,
582+
io::EpsCopyOutputStream* stream) const;
583+
579584
// Implementation of _InternalSerialize for non-empty map_.
580585
uint8_t* _InternalSerializeImpl(const MessageLite* extendee,
581586
int start_field_number, int end_field_number,

src/google/protobuf/wire_format.cc

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1232,6 +1232,7 @@ uint8_t* WireFormat::InternalSerializeField(const FieldDescriptor* field,
12321232
return InternalSerializeMessageSetItem(field, message, target, stream);
12331233
}
12341234

1235+
12351236
// For map fields, we can use either repeated field reflection or map
12361237
// reflection. Our choice has some subtle effects. If we use repeated field
12371238
// reflection here, then the repeated field representation becomes
@@ -1707,7 +1708,29 @@ size_t WireFormat::FieldDataOnlyByteSize(const FieldDescriptor* field,
17071708
HANDLE_FIXED_TYPE(BOOL, Bool)
17081709

17091710
HANDLE_TYPE(GROUP, Group, Message)
1711+
#ifndef PROTOBUF_ENABLE_FIX_CODE_SIZE_LAZY
17101712
HANDLE_TYPE(MESSAGE, Message, Message)
1713+
#else // !PROTOBUF_ENABLE_FIX_CODE_SIZE_LAZY
1714+
case FieldDescriptor::TYPE_MESSAGE: {
1715+
if (field->is_repeated()) {
1716+
for (size_t j = 0; j < count; ++j) {
1717+
data_size += WireFormatLite::MessageSize(
1718+
message_reflection->GetRepeatedMessage(message, field, j));
1719+
}
1720+
break;
1721+
}
1722+
if (field->is_extension()) {
1723+
data_size += WireFormatLite::LengthDelimitedSize(
1724+
message_reflection->GetExtensionSet(message).GetMessageByteSizeLong(
1725+
field->number()));
1726+
break;
1727+
}
1728+
data_size += WireFormatLite::MessageSize(
1729+
message_reflection->GetMessage(message, field));
1730+
break;
1731+
}
1732+
#endif // PROTOBUF_ENABLE_FIX_CODE_SIZE_LAZY
1733+
17111734
#undef HANDLE_TYPE
17121735
#undef HANDLE_FIXED_TYPE
17131736

0 commit comments

Comments
 (0)