Skip to content

Commit

Permalink
Backport: Remove if_constexpr usage for future Abseil compatibility (#…
Browse files Browse the repository at this point in the history
…20488)

* Use `if constexpr` instead of `absl::utility_internal::IfConstexprElseIfConstexprElse`

PiperOrigin-RevId: 728737911

* Remove `absl::if_constexpr` from list of used Abseil targets

This is a follow-up on 0ea5ccd, moving to C++'s
`if constexpr`.

PiperOrigin-RevId: 729136260

* Remove references to unused if_constexpr.

This is deleted in an upcoming Abseil release.

PiperOrigin-RevId: 731356947

---------

Co-authored-by: Derek Mauro <dmauro@google.com>
Co-authored-by: Christian Blichmann <cblichmann@google.com>
  • Loading branch information
3 people authored Feb 26, 2025
1 parent 2b9b101 commit 450ee76
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 39 deletions.
1 change: 0 additions & 1 deletion cmake/abseil-cpp.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,6 @@ else()
absl::flat_hash_set
absl::function_ref
absl::hash
absl::if_constexpr
absl::layout
absl::log_initialize
absl::log_globals
Expand Down
2 changes: 0 additions & 2 deletions src/google/protobuf/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -457,7 +457,6 @@ cc_library(
"@abseil-cpp//absl/numeric:bits",
"@abseil-cpp//absl/synchronization",
"@abseil-cpp//absl/types:span",
"@abseil-cpp//absl/utility:if_constexpr",
],
)

Expand Down Expand Up @@ -584,7 +583,6 @@ cc_library(
"@abseil-cpp//absl/time",
"@abseil-cpp//absl/types:optional",
"@abseil-cpp//absl/types:span",
"@abseil-cpp//absl/utility:if_constexpr",
],
)

Expand Down
61 changes: 25 additions & 36 deletions src/google/protobuf/arena.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ using type_info = ::type_info;
#include "absl/base/optimization.h"
#include "absl/base/prefetch.h"
#include "absl/log/absl_check.h"
#include "absl/utility/internal/if_constexpr.h"
#include "google/protobuf/arena_align.h"
#include "google/protobuf/arena_allocation_policy.h"
#include "google/protobuf/port.h"
Expand Down Expand Up @@ -195,41 +194,31 @@ class PROTOBUF_EXPORT PROTOBUF_ALIGNAS(8) Arena final {
// otherwise, returns a heap-allocated object.
template <typename T, typename... Args>
PROTOBUF_NDEBUG_INLINE static T* Create(Arena* arena, Args&&... args) {
return absl::utility_internal::IfConstexprElse<
is_arena_constructable<T>::value>(
// Arena-constructable
[arena](auto&&... args) {
using Type = std::remove_const_t<T>;
#ifdef __cpp_if_constexpr
// DefaultConstruct/CopyConstruct are optimized for messages, which
// are both arena constructible and destructor skippable and they
// assume much. Don't use these functions unless the invariants
// hold.
if constexpr (is_destructor_skippable<T>::value) {
constexpr auto construct_type = GetConstructType<T, Args&&...>();
// We delegate to DefaultConstruct/CopyConstruct where appropriate
// because protobuf generated classes have external templates for
// these functions for code size reasons. When `if constexpr` is not
// available always use the fallback.
if constexpr (construct_type == ConstructType::kDefault) {
return static_cast<Type*>(DefaultConstruct<Type>(arena));
} else if constexpr (construct_type == ConstructType::kCopy) {
return static_cast<Type*>(CopyConstruct<Type>(arena, &args...));
}
}
#endif
return CreateArenaCompatible<Type>(arena,
std::forward<Args>(args)...);
},
// Non arena-constructable
[arena](auto&&... args) {
if (ABSL_PREDICT_FALSE(arena == nullptr)) {
return new T(std::forward<Args>(args)...);
}
return new (arena->AllocateInternal<T>())
T(std::forward<Args>(args)...);
},
std::forward<Args>(args)...);
if constexpr (is_arena_constructable<T>::value) {
using Type = std::remove_const_t<T>;
// DefaultConstruct/CopyConstruct are optimized for messages, which
// are both arena constructible and destructor skippable and they
// assume much. Don't use these functions unless the invariants
// hold.
if constexpr (is_destructor_skippable<T>::value) {
constexpr auto construct_type = GetConstructType<T, Args&&...>();
// We delegate to DefaultConstruct/CopyConstruct where appropriate
// because protobuf generated classes have external templates for
// these functions for code size reasons. When `if constexpr` is not
// available always use the fallback.
if constexpr (construct_type == ConstructType::kDefault) {
return static_cast<Type*>(DefaultConstruct<Type>(arena));
} else if constexpr (construct_type == ConstructType::kCopy) {
return static_cast<Type*>(CopyConstruct<Type>(arena, &args...));
}
}
return CreateArenaCompatible<Type>(arena, std::forward<Args>(args)...);
} else {
if (ABSL_PREDICT_FALSE(arena == nullptr)) {
return new T(std::forward<Args>(args)...);
}
return new (arena->AllocateInternal<T>()) T(std::forward<Args>(args)...);
}
}

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

0 comments on commit 450ee76

Please sign in to comment.