Skip to content

Commit e84d171

Browse files
ckennellycopybara-github
authored andcommitted
Use __builtin_operator_new to faciliate compiler optimizations of these allocs.
PiperOrigin-RevId: 814330806
1 parent 6d00d87 commit e84d171

File tree

1 file changed

+20
-4
lines changed

1 file changed

+20
-4
lines changed

src/google/protobuf/arena.h

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -742,8 +742,16 @@ template <typename T>
742742
PROTOBUF_NOINLINE void* PROTOBUF_NONNULL
743743
Arena::DefaultConstruct(Arena* PROTOBUF_NULLABLE arena) {
744744
static_assert(is_destructor_skippable<T>::value, "");
745-
void* mem = arena != nullptr ? arena->AllocateAligned(sizeof(T))
746-
: ::operator new(sizeof(T));
745+
void* mem;
746+
if (arena != nullptr) {
747+
mem = arena->AllocateAligned(sizeof(T));
748+
} else {
749+
#if ABSL_HAVE_BUILTIN(__builtin_operator_new)
750+
mem = __builtin_operator_new(sizeof(T));
751+
#else
752+
mem = ::operator new(sizeof(T));
753+
#endif
754+
}
747755
return new (mem) T(arena);
748756
}
749757

@@ -766,8 +774,16 @@ PROTOBUF_NOINLINE void* PROTOBUF_NONNULL Arena::CopyConstruct(
766774
internal::Prefetch<kPrefetchOpts, T, T>(typed_from);
767775
}
768776
static_assert(is_destructor_skippable<T>::value, "");
769-
void* mem = arena != nullptr ? arena->AllocateAligned(sizeof(T))
770-
: ::operator new(sizeof(T));
777+
void* mem;
778+
if (arena != nullptr) {
779+
mem = arena->AllocateAligned(sizeof(T));
780+
} else {
781+
#if ABSL_HAVE_BUILTIN(__builtin_operator_new)
782+
mem = __builtin_operator_new(sizeof(T));
783+
#else
784+
mem = ::operator new(sizeof(T));
785+
#endif
786+
}
771787
return new (mem) T(arena, *typed_from);
772788
}
773789

0 commit comments

Comments
 (0)