Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make WireFormat::(ByteSize|_InternalSerialize) LazyField compatible (take #3 but disabled) #18346

Merged
merged 1 commit into from
Sep 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions src/google/protobuf/extension_set.cc
Original file line number Diff line number Diff line change
Expand Up @@ -513,6 +513,31 @@ const int& ExtensionSet::GetRefRepeatedEnum(int number, int index) const {
return extension->ptr.repeated_enum_value->Get(index);
}

size_t ExtensionSet::GetMessageByteSizeLong(int number) const {
const Extension* extension = FindOrNull(number);
ABSL_CHECK(extension != nullptr) << "not present";
ABSL_DCHECK_TYPE(*extension, OPTIONAL_FIELD, MESSAGE);
return extension->is_lazy ? extension->ptr.lazymessage_value->ByteSizeLong()
: extension->ptr.message_value->ByteSizeLong();
}

uint8_t* ExtensionSet::InternalSerializeMessage(
int number, const MessageLite* prototype, uint8_t* target,
io::EpsCopyOutputStream* stream) const {
const Extension* extension = FindOrNull(number);
ABSL_CHECK(extension != nullptr) << "not present";
ABSL_DCHECK_TYPE(*extension, OPTIONAL_FIELD, MESSAGE);

if (extension->is_lazy) {
return extension->ptr.lazymessage_value->WriteMessageToArray(
prototype, number, target, stream);
}

const auto* msg = extension->ptr.message_value;
return WireFormatLite::InternalWriteMessage(
number, *msg, msg->GetCachedSize(), target, stream);
}

void ExtensionSet::SetRepeatedEnum(int number, int index, int value) {
Extension* extension = FindOrNull(number);
ABSL_CHECK(extension != nullptr) << "Index out-of-bounds (field is empty).";
Expand Down
5 changes: 5 additions & 0 deletions src/google/protobuf/extension_set.h
Original file line number Diff line number Diff line change
Expand Up @@ -576,6 +576,11 @@ class PROTOBUF_EXPORT ExtensionSet {
const bool& GetRefRepeatedBool(int number, int index) const;
const int& GetRefRepeatedEnum(int number, int index) const;

size_t GetMessageByteSizeLong(int number) const;
uint8_t* InternalSerializeMessage(int number, const MessageLite* prototype,
uint8_t* target,
io::EpsCopyOutputStream* stream) const;

// Implementation of _InternalSerialize for non-empty map_.
uint8_t* _InternalSerializeImpl(const MessageLite* extendee,
int start_field_number, int end_field_number,
Expand Down
23 changes: 23 additions & 0 deletions src/google/protobuf/wire_format.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1232,6 +1232,7 @@ uint8_t* WireFormat::InternalSerializeField(const FieldDescriptor* field,
return InternalSerializeMessageSetItem(field, message, target, stream);
}


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

HANDLE_TYPE(GROUP, Group, Message)
#ifndef PROTOBUF_ENABLE_FIX_CODE_SIZE_LAZY
HANDLE_TYPE(MESSAGE, Message, Message)
#else // !PROTOBUF_ENABLE_FIX_CODE_SIZE_LAZY
case FieldDescriptor::TYPE_MESSAGE: {
if (field->is_repeated()) {
for (size_t j = 0; j < count; ++j) {
data_size += WireFormatLite::MessageSize(
message_reflection->GetRepeatedMessage(message, field, j));
}
break;
}
if (field->is_extension()) {
data_size += WireFormatLite::LengthDelimitedSize(
message_reflection->GetExtensionSet(message).GetMessageByteSizeLong(
field->number()));
break;
}
data_size += WireFormatLite::MessageSize(
message_reflection->GetMessage(message, field));
break;
}
#endif // PROTOBUF_ENABLE_FIX_CODE_SIZE_LAZY

#undef HANDLE_TYPE
#undef HANDLE_FIXED_TYPE

Expand Down
Loading