diff --git a/src/google/protobuf/arena.h b/src/google/protobuf/arena.h index b60b7d0fc9..8a7d1b4804 100644 --- a/src/google/protobuf/arena.h +++ b/src/google/protobuf/arena.h @@ -271,7 +271,7 @@ class PROTOBUF_EXPORT PROTOBUF_ALIGNAS(8) Arena final { // is obtained from the arena). template PROTOBUF_NDEBUG_INLINE static T* Create(Arena* arena, Args&&... args) { - if (arena == nullptr) { + if (PROTOBUF_PREDICT_FALSE(arena == nullptr)) { return new T(std::forward(args)...); } auto destructor = @@ -320,10 +320,13 @@ class PROTOBUF_EXPORT PROTOBUF_ALIGNAS(8) Arena final { "CreateArray requires a trivially destructible type"); GOOGLE_ABSL_CHECK_LE(num_elements, std::numeric_limits::max() / sizeof(T)) << "Requested size is too large to fit into size_t."; - if (arena == nullptr) { + if (PROTOBUF_PREDICT_FALSE(arena == nullptr)) { return static_cast(::operator new[](num_elements * sizeof(T))); } else { - return arena->CreateInternalRawArray(num_elements); + // We count on compiler to realize that if sizeof(T) is a multiple of + // 8 AlignUpTo can be elided. + return static_cast( + arena->AllocateAlignedForArray(sizeof(T) * num_elements, alignof(T))); } } @@ -598,18 +601,6 @@ class PROTOBUF_EXPORT PROTOBUF_ALIGNAS(8) Arena final { std::forward(args)...); } - // Just allocate the required size for the given type assuming the - // type has a trivial constructor. - template - PROTOBUF_NDEBUG_INLINE T* CreateInternalRawArray(size_t num_elements) { - GOOGLE_ABSL_CHECK_LE(num_elements, std::numeric_limits::max() / sizeof(T)) - << "Requested size is too large to fit into size_t."; - // We count on compiler to realize that if sizeof(T) is a multiple of - // 8 AlignUpTo can be elided. - const size_t n = sizeof(T) * num_elements; - return static_cast(AllocateAlignedForArray(n, alignof(T))); - } - template PROTOBUF_NDEBUG_INLINE T* DoCreateMessage(Args&&... args) { return InternalHelper::Construct(