Skip to content

Commit 195da19

Browse files
ckennellycopybara-github
authored andcommitted
Use __builtin_operator_new to faciliate compiler optimizations of these allocs.
PiperOrigin-RevId: 815794039
1 parent 0d6b997 commit 195da19

File tree

1 file changed

+14
-5
lines changed

1 file changed

+14
-5
lines changed

src/google/protobuf/message_lite.h

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
#include <utility>
3434

3535
#include "absl/base/attributes.h"
36+
#include "absl/base/config.h"
3637
#include "absl/log/absl_check.h"
3738
#include "absl/numeric/bits.h"
3839
#include "absl/strings/cord.h"
@@ -1445,11 +1446,19 @@ template <typename MessageLite>
14451446
PROTOBUF_ALWAYS_INLINE MessageLite* MessageCreator::New(
14461447
const MessageLite* prototype_for_func,
14471448
const MessageLite* prototype_for_copy, Arena* arena) const {
1448-
return PlacementNew(prototype_for_func, prototype_for_copy,
1449-
arena != nullptr
1450-
? arena->AllocateAligned(allocation_size_)
1451-
: ::operator new(allocation_size_),
1452-
arena);
1449+
void* mem;
1450+
if (arena != nullptr) {
1451+
mem = arena->AllocateAligned(allocation_size_);
1452+
} else {
1453+
#if ABSL_HAVE_BUILTIN(__builtin_operator_new)
1454+
// Allows the compiler to merge or optimize away the allocation even if it
1455+
// would violate the observability guarantees of ::operator new.
1456+
mem = __builtin_operator_new(allocation_size_);
1457+
#else
1458+
mem = ::operator new(allocation_size_);
1459+
#endif
1460+
}
1461+
return PlacementNew(prototype_for_func, prototype_for_copy, mem, arena);
14531462
}
14541463

14551464
} // namespace internal

0 commit comments

Comments
 (0)