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

Streamline creation of new elements in RepeatedPtrField #18086

Closed
wants to merge 1 commit into from
Closed
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
4 changes: 1 addition & 3 deletions src/google/protobuf/extension_set.cc
Original file line number Diff line number Diff line change
Expand Up @@ -823,9 +823,7 @@ MessageLite* ExtensionSet::AddMessage(int number, FieldType type,
ABSL_DCHECK_TYPE(*extension, REPEATED_FIELD, MESSAGE);
}

return reinterpret_cast<internal::RepeatedPtrFieldBase*>(
extension->ptr.repeated_message_value)
->AddMessage(&prototype);
return extension->ptr.repeated_message_value->AddFromPrototype(&prototype);
}

// Defined in extension_set_heavy.cc.
Expand Down
3 changes: 2 additions & 1 deletion src/google/protobuf/lazy_repeated_field.h
Original file line number Diff line number Diff line change
Expand Up @@ -328,7 +328,8 @@ class LazyRepeatedPtrField {
const char* ptr2 = ptr;
TagType next_tag;
do {
MessageLite* submsg = value->AddMessage(prototype);
MessageLite* submsg =
value->AddFromPrototype<GenericTypeHandler<MessageLite>>(prototype);
// ptr2 points to the start of the element's encoded length.
ptr = ctx->ParseMessage(submsg, ptr2);
if (PROTOBUF_PREDICT_FALSE(ptr == nullptr)) return nullptr;
Expand Down
22 changes: 0 additions & 22 deletions src/google/protobuf/message.cc
Original file line number Diff line number Diff line change
Expand Up @@ -495,28 +495,6 @@ const internal::RepeatedFieldAccessor* Reflection::RepeatedFieldAccessor(
}

namespace internal {
template <>
#if defined(_MSC_VER) && (_MSC_VER >= 1800)
// Note: force noinline to workaround MSVC compiler bug with /Zc:inline, issue
// #240
PROTOBUF_NOINLINE
#endif
Message*
GenericTypeHandler<Message>::NewFromPrototype(const Message* prototype,
Arena* arena) {
return prototype->New(arena);
}
template <>
#if defined(_MSC_VER) && (_MSC_VER >= 1800)
// Note: force noinline to workaround MSVC compiler bug with /Zc:inline, issue
// #240
PROTOBUF_NOINLINE
#endif
Arena*
GenericTypeHandler<Message>::GetArena(Message* value) {
return value->GetArena();
}

template void InternalMetadata::DoClear<UnknownFieldSet>();
template void InternalMetadata::DoMergeFrom<UnknownFieldSet>(
const UnknownFieldSet& other);
Expand Down
10 changes: 0 additions & 10 deletions src/google/protobuf/message_lite.cc
Original file line number Diff line number Diff line change
Expand Up @@ -703,16 +703,6 @@ absl::Cord MessageLite::SerializePartialAsCord() const {

namespace internal {

MessageLite* NewFromPrototypeHelper(const MessageLite* prototype,
Arena* arena) {
return prototype->New(arena);
}
template <>
void GenericTypeHandler<MessageLite>::Merge(const MessageLite& from,
MessageLite* to) {
to->CheckTypeAndMergeFrom(from);
}

// Non-inline variants of std::string specializations for
// various InternalMetadata routines.
template <>
Expand Down
5 changes: 0 additions & 5 deletions src/google/protobuf/message_lite.h
Original file line number Diff line number Diff line change
Expand Up @@ -225,9 +225,6 @@ class WireFormatLite;
class WeakFieldMap;
class RustMapHelper;

template <typename Type>
class GenericTypeHandler; // defined in repeated_field.h

// We compute sizes as size_t but cache them as int. This function converts a
// computed size to a cached size. Since we don't proceed with serialization
// if the total size was > INT_MAX, it is not important what this function
Expand Down Expand Up @@ -963,8 +960,6 @@ class PROTOBUF_EXPORT MessageLite {

template <typename Type>
friend class Arena::InternalHelper;
template <typename Type>
friend class internal::GenericTypeHandler;

friend auto internal::GetClassData(const MessageLite& msg);

Expand Down
7 changes: 4 additions & 3 deletions src/google/protobuf/repeated_field.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,13 @@
#include <iterator>
#include <limits>
#include <memory>
#include <string>
#include <type_traits>
#include <utility>

#include "absl/base/attributes.h"
#include "absl/base/dynamic_annotations.h"
#include "absl/base/optimization.h"
#include "absl/log/absl_check.h"
#include "absl/log/absl_log.h"
#include "absl/meta/type_traits.h"
#include "absl/strings/cord.h"
#include "google/protobuf/arena.h"
Expand All @@ -46,7 +44,6 @@
#include "google/protobuf/port.h"
#include "google/protobuf/repeated_ptr_field.h"


// Must be included last.
#include "google/protobuf/port_def.inc"

Expand Down Expand Up @@ -79,7 +76,11 @@ constexpr int RepeatedFieldLowerClampLimit() {
// overflows when multiplied by 2 (which is undefined behavior). Sizes above
// this will clamp to the maximum int value instead of following exponential
// growth when growing a repeated field.
#if defined(__cpp_inline_variables)
inline constexpr int kRepeatedFieldUpperClampLimit =
#else
constexpr int kRepeatedFieldUpperClampLimit =
#endif
(std::numeric_limits<int>::max() / 2) + 1;

template <typename Element>
Expand Down
2 changes: 1 addition & 1 deletion src/google/protobuf/repeated_field_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
#include <limits>
#include <list>
#include <memory>
#include <new>
#include <sstream>
#include <string>
#include <type_traits>
Expand All @@ -34,7 +35,6 @@
#include <gtest/gtest.h>
#include "absl/log/absl_check.h"
#include "absl/numeric/bits.h"
#include "absl/random/random.h"
#include "absl/strings/cord.h"
#include "absl/strings/str_cat.h"
#include "absl/types/span.h"
Expand Down
19 changes: 1 addition & 18 deletions src/google/protobuf/repeated_ptr_field.cc
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@
#include <cstdint>
#include <cstring>
#include <limits>
#include <new>
#include <string>

#include "absl/base/prefetch.h"
#include "absl/log/absl_check.h"
#include "google/protobuf/arena.h"
#include "google/protobuf/message_lite.h"
Expand Down Expand Up @@ -93,14 +93,6 @@ void RepeatedPtrFieldBase::DestroyProtos() {
tagged_rep_or_elem_ = nullptr;
}

void* RepeatedPtrFieldBase::AddMessageLite(ElementFactory factory) {
return AddInternal(factory);
}

void* RepeatedPtrFieldBase::AddString() {
return AddInternal([](Arena* arena) { return NewStringElement(arena); });
}

void RepeatedPtrFieldBase::CloseGap(int start, int num) {
if (using_sso()) {
if (start == 0 && num == 1) {
Expand All @@ -116,11 +108,6 @@ void RepeatedPtrFieldBase::CloseGap(int start, int num) {
ExchangeCurrentSize(current_size_ - num);
}

MessageLite* RepeatedPtrFieldBase::AddMessage(const MessageLite* prototype) {
return static_cast<MessageLite*>(
AddInternal([prototype](Arena* a) { return prototype->New(a); }));
}

void InternalOutOfLineDeleteMessageLite(MessageLite* message) {
delete message;
}
Expand Down Expand Up @@ -227,10 +214,6 @@ void RepeatedPtrFieldBase::MergeFrom<MessageLite>(
}
}

void* NewStringElement(Arena* arena) {
return Arena::Create<std::string>(arena);
}

} // namespace internal
} // namespace protobuf
} // namespace google
Expand Down
Loading
Loading