Skip to content

Commit

Permalink
Internal change
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 731380026
  • Loading branch information
protobuf-github-bot authored and copybara-github committed Feb 26, 2025
1 parent 74bf391 commit 365ae12
Show file tree
Hide file tree
Showing 6 changed files with 0 additions and 238 deletions.
125 changes: 0 additions & 125 deletions src/google/protobuf/extension_set.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1402,131 +1402,6 @@ size_t ExtensionSet::ByteSize() const {
return total_size;
}

#ifdef PROTOBUF_INTERNAL_V2_EXPERIMENT
size_t ExtensionSet::ByteSizeV2() const {
static constexpr int8_t kFieldSizes[] = {
-1,
sizeof(double), // TYPE_DOUBLE
sizeof(float), // TYPE_FLOAT
sizeof(int64_t), // TYPE_INT64
sizeof(uint64_t), // TYPE_UINT64
sizeof(int32_t), // TYPE_INT32
sizeof(uint64_t), // TYPE_FIXED64
sizeof(uint32_t), // TYPE_FIXED32
sizeof(bool), // TYPE_BOOL
-1, // TYPE_STRING
-1, // TYPE_GROUP
-1, // TYPE_MESSAGE
-1, // TYPE_BYTES
sizeof(uint32_t), // TYPE_UINT32
sizeof(int), // TYPE_ENUM
sizeof(uint32_t), // TYPE_SFIXED32
sizeof(uint64_t), // TYPE_SFIXED64
sizeof(int32_t), // TYPE_SINT32
sizeof(int64_t), // TYPE_SINT64
};

size_t total_size = 0;
ForEach(
[&total_size](int /* number */, const ExtensionSet::Extension& ext) {
// We assume that (singular) message extensions are common cases.
if (ABSL_PREDICT_TRUE(ext.type == WireFormatLite::TYPE_MESSAGE ||
ext.type == WireFormatLite::TYPE_GROUP)) {
if (ABSL_PREDICT_FALSE(ext.is_repeated)) {
const auto* rep = ext.ptr.repeated_message_value;
if (rep->empty()) return;
size_t size = 0;
for (const auto& each : *rep) {
size += each.ByteSizeV2();
}
total_size += v2::kRepeatedFieldTagSize +
v2::kLengthSize * rep->size() + size;
} else if (ABSL_PREDICT_TRUE(ext.is_lazy)) {
total_size += std::visit(
absl::Overload{
[&](const MessageLite* ptr) {
return WireFormatLite::LengthPrefixedByteSizeV2(
ptr->ByteSizeV2());
},
[](size_t unparsed_size) {
return WireFormatLite::LengthPrefixedByteSizeV2(
unparsed_size);
}},
ext.ptr.lazymessage_value->UnparsedSizeOrMessage());
} else {
total_size += WireFormatLite::LengthPrefixedByteSizeV2(
ext.ptr.message_value->ByteSizeV2());
}
return;
}

// Handle string fields as numerics are fixed widths.
if (ext.type == WireFormatLite::TYPE_STRING ||
ext.type == WireFormatLite::TYPE_BYTES) {
if (ext.is_repeated) {
total_size += WireFormatLite::RepeatedStringByteSizeV2(
*ext.ptr.repeated_string_value);
} else {
total_size += WireFormatLite::LengthPrefixedByteSizeV2(
ext.ptr.string_value->size());
}
return;
}

// The rest of the types are numeric fields with fixed widths for v2.
if (ext.is_repeated) {
switch (ext.type) {
case WireFormatLite::TYPE_DOUBLE:
total_size += WireFormatLite::RepeatedNumericByteSizeV2(
*ext.ptr.repeated_double_value);
break;
case WireFormatLite::TYPE_FLOAT:
total_size += WireFormatLite::RepeatedNumericByteSizeV2(
*ext.ptr.repeated_float_value);
break;
case WireFormatLite::TYPE_ENUM:
total_size += WireFormatLite::RepeatedNumericByteSizeV2(
*ext.ptr.repeated_enum_value);
break;
case WireFormatLite::TYPE_INT32:
case WireFormatLite::TYPE_SFIXED32:
case WireFormatLite::TYPE_SINT32:
total_size += WireFormatLite::RepeatedNumericByteSizeV2(
*ext.ptr.repeated_int32_t_value);
break;
case WireFormatLite::TYPE_INT64:
case WireFormatLite::TYPE_SFIXED64:
case WireFormatLite::TYPE_SINT64:
total_size += WireFormatLite::RepeatedNumericByteSizeV2(
*ext.ptr.repeated_int64_t_value);
break;
case WireFormatLite::TYPE_FIXED32:
case WireFormatLite::TYPE_UINT32:
total_size += WireFormatLite::RepeatedNumericByteSizeV2(
*ext.ptr.repeated_uint32_t_value);
break;
case WireFormatLite::TYPE_FIXED64:
case WireFormatLite::TYPE_UINT64:
total_size += WireFormatLite::RepeatedNumericByteSizeV2(
*ext.ptr.repeated_uint64_t_value);
break;
case WireFormatLite::TYPE_BOOL:
total_size += WireFormatLite::RepeatedNumericByteSizeV2(
*ext.ptr.repeated_bool_value);
break;
default:
Unreachable();
}
} else {
auto field_size = kFieldSizes[ext.type];
ABSL_DCHECK_NE(field_size, -1);
total_size += v2::kSingularFieldTagSize + field_size;
}
},
ExtensionSet::Prefetch{});
return total_size;
}
#endif // PROTOBUF_INTERNAL_V2_EXPERIMENT

// Defined in extension_set_heavy.cc.
// int ExtensionSet::SpaceUsedExcludingSelf() const
Expand Down
7 changes: 0 additions & 7 deletions src/google/protobuf/extension_set.h
Original file line number Diff line number Diff line change
Expand Up @@ -531,9 +531,6 @@ class PROTOBUF_EXPORT ExtensionSet {

// Returns the total serialized size of all the extensions.
size_t ByteSize() const;
#ifdef PROTOBUF_INTERNAL_V2_EXPERIMENT
size_t ByteSizeV2() const;
#endif // PROTOBUF_INTERNAL_V2_EXPERIMENT

// Like ByteSize() but uses MessageSet format.
size_t MessageSetByteSize() const;
Expand Down Expand Up @@ -672,10 +669,6 @@ class PROTOBUF_EXPORT ExtensionSet {
const MessageLite* prototype, int number, uint8_t* target,
io::EpsCopyOutputStream* stream) const = 0;

#ifdef PROTOBUF_INTERNAL_V2_EXPERIMENT
virtual size_t ByteSizeLongV2() const = 0;
virtual uint8_t* WriteMessageToArrayV2(uint8_t* target) const = 0;
#endif // PROTOBUF_INTERNAL_V2_EXPERIMENT

private:
virtual void UnusedKeyMethod(); // Dummy key method to avoid weak vtable.
Expand Down
7 changes: 0 additions & 7 deletions src/google/protobuf/generated_message_bases.cc
Original file line number Diff line number Diff line change
Expand Up @@ -48,13 +48,6 @@ size_t ZeroFieldsBase::ByteSizeLong(const MessageLite& base) {
return msg.MaybeComputeUnknownFieldsSize(0, &msg._impl_._cached_size_);
}

#ifdef PROTOBUF_INTERNAL_V2_EXPERIMENT
size_t ZeroFieldsBase::ByteSizeV2Impl(const MessageLite& base) {
auto& msg = static_cast<const ZeroFieldsBase&>(base);
return msg.MaybeComputeUnknownFieldsSize(v2::kPoisonV2HeaderSize,
&msg._impl_._cached_size_);
}
#endif // PROTOBUF_INTERNAL_V2_EXPERIMENT

::uint8_t* ZeroFieldsBase::_InternalSerialize(const MessageLite& msg,
::uint8_t* target,
Expand Down
8 changes: 0 additions & 8 deletions src/google/protobuf/generated_message_bases.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,6 @@ class PROTOBUF_EXPORT ZeroFieldsBase : public Message {
public:
ABSL_ATTRIBUTE_REINITIALIZES void Clear() PROTOBUF_FINAL { Clear(*this); }
size_t ByteSizeLong() const PROTOBUF_FINAL { return ByteSizeLong(*this); }
#ifdef PROTOBUF_INTERNAL_V2_EXPERIMENT
size_t ByteSizeV2() const { return ByteSizeV2Impl(*this); }
#endif // PROTOBUF_INTERNAL_V2_EXPERIMENT
int GetCachedSize() const { return _impl_._cached_size_.Get(); }
::uint8_t* _InternalSerialize(
::uint8_t* target, io::EpsCopyOutputStream* stream) const PROTOBUF_FINAL {
Expand All @@ -52,11 +49,6 @@ class PROTOBUF_EXPORT ZeroFieldsBase : public Message {
void InternalSwap(ZeroFieldsBase* other);
static void Clear(MessageLite& msg);
static size_t ByteSizeLong(const MessageLite& base);
#ifdef PROTOBUF_INTERNAL_V2_EXPERIMENT
static size_t ByteSizeV2Impl(const MessageLite& base);

static constexpr auto& _v2_table_ = v2::kEmptyMessageTable;
#endif // PROTOBUF_INTERNAL_V2_EXPERIMENT
static ::uint8_t* _InternalSerialize(const MessageLite& msg,
::uint8_t* target,
io::EpsCopyOutputStream* stream);
Expand Down
52 changes: 0 additions & 52 deletions src/google/protobuf/message_lite.h
Original file line number Diff line number Diff line change
Expand Up @@ -367,11 +367,6 @@ struct PROTOBUF_EXPORT ClassData {
// char[] just beyond the ClassData.
bool is_lite;
bool is_dynamic = false;
#ifdef PROTOBUF_INTERNAL_V2_EXPERIMENT
const void* v2_message_table = nullptr;
const void* v2_parse_table = nullptr;
size_t (*byte_size_v2)(const MessageLite&) = nullptr;
#endif // PROTOBUF_INTERNAL_V2_EXPERIMENT

// In normal mode we have the small constructor to avoid the cost in
// codegen.
Expand All @@ -383,12 +378,6 @@ struct PROTOBUF_EXPORT ClassData {
void (*merge_to_from)(MessageLite& to, const MessageLite& from_msg),
internal::MessageCreator message_creator, uint32_t cached_size_offset,
bool is_lite
#ifdef PROTOBUF_INTERNAL_V2_EXPERIMENT
,
const void* v2_message_table = nullptr,
const void* v2_parse_table = nullptr,
size_t (*byte_size_v2)(const MessageLite&) = nullptr
#endif // PROTOBUF_INTERNAL_V2_EXPERIMENT
)
: prototype(prototype),
tc_table(tc_table),
Expand All @@ -398,12 +387,6 @@ struct PROTOBUF_EXPORT ClassData {
message_creator(message_creator),
cached_size_offset(cached_size_offset),
is_lite(is_lite)
#ifdef PROTOBUF_INTERNAL_V2_EXPERIMENT
,
v2_message_table(v2_message_table),
v2_parse_table(v2_parse_table),
byte_size_v2(byte_size_v2)
#endif // PROTOBUF_INTERNAL_V2_EXPERIMENT
{
}
#endif // !PROTOBUF_CUSTOM_VTABLE
Expand All @@ -422,12 +405,6 @@ struct PROTOBUF_EXPORT ClassData {
uint8_t* (*serialize)(const MessageLite& msg, uint8_t* ptr,
io::EpsCopyOutputStream* stream),
uint32_t cached_size_offset, bool is_lite
#ifdef PROTOBUF_INTERNAL_V2_EXPERIMENT
,
const void* v2_message_table = nullptr,
const void* v2_parse_table = nullptr,
size_t (*byte_size_v2)(const MessageLite&) = nullptr
#endif // PROTOBUF_INTERNAL_V2_EXPERIMENT
)
: prototype(prototype),
tc_table(tc_table),
Expand All @@ -443,12 +420,6 @@ struct PROTOBUF_EXPORT ClassData {
#endif // PROTOBUF_CUSTOM_VTABLE
cached_size_offset(cached_size_offset),
is_lite(is_lite)
#ifdef PROTOBUF_INTERNAL_V2_EXPERIMENT
,
v2_message_table(v2_message_table),
v2_parse_table(v2_parse_table),
byte_size_v2(byte_size_v2)
#endif // PROTOBUF_INTERNAL_V2_EXPERIMENT
{
}

Expand Down Expand Up @@ -849,9 +820,6 @@ class PROTOBUF_EXPORT MessageLite {
virtual size_t ByteSizeLong() const = 0;
#endif // PROTOBUF_CUSTOM_VTABLE

#ifdef PROTOBUF_INTERNAL_V2_EXPERIMENT
size_t ByteSizeV2() const { return GetClassData()->byte_size_v2(*this); }
#endif // PROTOBUF_INTERNAL_V2_EXPERIMENT

// Legacy ByteSize() API.
[[deprecated("Please use ByteSizeLong() instead")]] int ByteSize() const {
Expand Down Expand Up @@ -967,26 +935,6 @@ class PROTOBUF_EXPORT MessageLite {
return tc_table;
}

#ifdef PROTOBUF_INTERNAL_V2_EXPERIMENT
// TODO: b/393403510 - For now, we return a void* to avoid taking a dependency
// to the V2 parse table header. This will require an additional
// reinterpret_cast in the V2 parse loop. When we start feeling confident in
// the V2 implementation, we can work on making this function return the
// proper type (should be internal::v2::ParseTableBase*).
const void* GetV2ParseTable() const {
auto* data = GetClassData();
ABSL_DCHECK_NE(data, nullptr);

auto* table = data->v2_parse_table;
// TODO: b/393403284 - The V1 implementation has a descriptor method that
// can get the parse table for dynamic messages:
// http://google3/third_party/protobuf/message_lite.h;l=490;rcl=718025165
//
// Eventually, we should also support that for V2 parse.
ABSL_DCHECK_NE(table, nullptr);
return table;
}
#endif // PROTOBUF_INTERNAL_V2_EXPERIMENT

#if defined(PROTOBUF_CUSTOM_VTABLE)
explicit constexpr MessageLite(const internal::ClassData* data)
Expand Down
39 changes: 0 additions & 39 deletions src/google/protobuf/wire_format_lite.h
Original file line number Diff line number Diff line change
Expand Up @@ -760,45 +760,6 @@ class PROTOBUF_EXPORT WireFormatLite {
// wire if we encode the data as a length delimited field.
static inline size_t LengthDelimitedSize(size_t length);

#ifdef PROTOBUF_INTERNAL_V2_EXPERIMENT
// v2 wireformat helpers for ByteSize.
template <typename T>
static size_t RepeatedNumericByteSizeV2(const RepeatedField<T>& value) {
return value.empty() ? 0
: v2::kRepeatedFieldTagSize + value.size() * sizeof(T);
}

template <typename RepeatedT>
static size_t RepeatedStringByteSizeV2(const RepeatedT& value) {
if (value.empty()) return 0;

size_t total_bytes = 0;
for (const auto& each : value) {
total_bytes += each.size();
}
// Add size for length (sizeof(uint32_t)) per element.
return v2::kRepeatedFieldTagSize + v2::kLengthSize * value.size() +
total_bytes;
}

template <typename RepeatedT>
static size_t RepeatedMessageByteSizeV2(const RepeatedT& value) {
if (value.empty()) return 0;

size_t total_bytes = 0;
for (const auto& each : value) {
total_bytes += each.ByteSizeV2();
}
// Add size for length (sizeof(uint32_t)) per element.
return v2::kRepeatedFieldTagSize + v2::kLengthSize * value.size() +
total_bytes;
}

static inline size_t LengthPrefixedByteSizeV2(size_t field_size) {
// <tag> <field_number> <length> <payload>
return v2::kSingularFieldTagSize + v2::kLengthSize + field_size;
}
#endif // PROTOBUF_INTERNAL_V2_EXPERIMENT

private:
// A helper method for the repeated primitive reader. This method has
Expand Down

0 comments on commit 365ae12

Please sign in to comment.