Skip to content

Commit 0ea5ccd

Browse files
derekmaurocopybara-github
authored andcommitted
Use if constexpr instead of absl::utility_internal::IfConstexprElseIfConstexprElse
PiperOrigin-RevId: 728737911
1 parent 28de0eb commit 0ea5ccd

File tree

1 file changed

+25
-36
lines changed

1 file changed

+25
-36
lines changed

src/google/protobuf/arena.h

Lines changed: 25 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@ using type_info = ::type_info;
3232
#include "absl/base/optimization.h"
3333
#include "absl/base/prefetch.h"
3434
#include "absl/log/absl_check.h"
35-
#include "absl/utility/internal/if_constexpr.h"
3635
#include "google/protobuf/arena_align.h"
3736
#include "google/protobuf/arena_allocation_policy.h"
3837
#include "google/protobuf/port.h"
@@ -195,41 +194,31 @@ class PROTOBUF_EXPORT PROTOBUF_ALIGNAS(8) Arena final {
195194
// otherwise, returns a heap-allocated object.
196195
template <typename T, typename... Args>
197196
PROTOBUF_NDEBUG_INLINE static T* Create(Arena* arena, Args&&... args) {
198-
return absl::utility_internal::IfConstexprElse<
199-
is_arena_constructable<T>::value>(
200-
// Arena-constructable
201-
[arena](auto&&... args) {
202-
using Type = std::remove_const_t<T>;
203-
#ifdef __cpp_if_constexpr
204-
// DefaultConstruct/CopyConstruct are optimized for messages, which
205-
// are both arena constructible and destructor skippable and they
206-
// assume much. Don't use these functions unless the invariants
207-
// hold.
208-
if constexpr (is_destructor_skippable<T>::value) {
209-
constexpr auto construct_type = GetConstructType<T, Args&&...>();
210-
// We delegate to DefaultConstruct/CopyConstruct where appropriate
211-
// because protobuf generated classes have external templates for
212-
// these functions for code size reasons. When `if constexpr` is not
213-
// available always use the fallback.
214-
if constexpr (construct_type == ConstructType::kDefault) {
215-
return static_cast<Type*>(DefaultConstruct<Type>(arena));
216-
} else if constexpr (construct_type == ConstructType::kCopy) {
217-
return static_cast<Type*>(CopyConstruct<Type>(arena, &args...));
218-
}
219-
}
220-
#endif
221-
return CreateArenaCompatible<Type>(arena,
222-
std::forward<Args>(args)...);
223-
},
224-
// Non arena-constructable
225-
[arena](auto&&... args) {
226-
if (ABSL_PREDICT_FALSE(arena == nullptr)) {
227-
return new T(std::forward<Args>(args)...);
228-
}
229-
return new (arena->AllocateInternal<T>())
230-
T(std::forward<Args>(args)...);
231-
},
232-
std::forward<Args>(args)...);
197+
if constexpr (is_arena_constructable<T>::value) {
198+
using Type = std::remove_const_t<T>;
199+
// DefaultConstruct/CopyConstruct are optimized for messages, which
200+
// are both arena constructible and destructor skippable and they
201+
// assume much. Don't use these functions unless the invariants
202+
// hold.
203+
if constexpr (is_destructor_skippable<T>::value) {
204+
constexpr auto construct_type = GetConstructType<T, Args&&...>();
205+
// We delegate to DefaultConstruct/CopyConstruct where appropriate
206+
// because protobuf generated classes have external templates for
207+
// these functions for code size reasons. When `if constexpr` is not
208+
// available always use the fallback.
209+
if constexpr (construct_type == ConstructType::kDefault) {
210+
return static_cast<Type*>(DefaultConstruct<Type>(arena));
211+
} else if constexpr (construct_type == ConstructType::kCopy) {
212+
return static_cast<Type*>(CopyConstruct<Type>(arena, &args...));
213+
}
214+
}
215+
return CreateArenaCompatible<Type>(arena, std::forward<Args>(args)...);
216+
} else {
217+
if (ABSL_PREDICT_FALSE(arena == nullptr)) {
218+
return new T(std::forward<Args>(args)...);
219+
}
220+
return new (arena->AllocateInternal<T>()) T(std::forward<Args>(args)...);
221+
}
233222
}
234223

235224
// API to delete any objects not on an arena. This can be used to safely

0 commit comments

Comments
 (0)