From 1063d2b829bbf1b5cf03f5f8e1e9fe4c71c9f966 Mon Sep 17 00:00:00 2001 From: Samuel Benzaquen Date: Thu, 1 Dec 2022 10:08:04 -0800 Subject: Move the vtable into a function to delay instantiation until the function is called. When the variable is a global the compiler is allowed to instantiate it more aggresively and it might happen before the types involved are complete. When it is inside a function the compiler can't instantiate it until after the functions are called. Remove an unused member from the vtable. Replace transfer_slot_fn with a generic function when relocation is available to reduce duplication. PiperOrigin-RevId: 492227302 Change-Id: I07499f63b91c59c0ae42402683387c7b84a6f0ee --- absl/container/internal/common_policy_traits.h | 45 ++++++++++++++++++-------- 1 file changed, 31 insertions(+), 14 deletions(-) (limited to 'absl/container/internal/common_policy_traits.h') diff --git a/absl/container/internal/common_policy_traits.h b/absl/container/internal/common_policy_traits.h index 0fd4866e..b42c9a48 100644 --- a/absl/container/internal/common_policy_traits.h +++ b/absl/container/internal/common_policy_traits.h @@ -63,7 +63,7 @@ struct common_policy_traits { // UNINITIALIZED template static void transfer(Alloc* alloc, slot_type* new_slot, slot_type* old_slot) { - transfer_impl(alloc, new_slot, old_slot, 0); + transfer_impl(alloc, new_slot, old_slot, Rank0{}); } // PRECONDITION: `slot` is INITIALIZED @@ -80,29 +80,46 @@ struct common_policy_traits { return P::element(slot); } + static constexpr bool transfer_uses_memcpy() { + return std::is_same>( + nullptr, nullptr, nullptr, Rank0{})), + std::true_type>::value; + } + private: + // To rank the overloads below for overload resoltion. Rank0 is preferred. + struct Rank2 {}; + struct Rank1 : Rank2 {}; + struct Rank0 : Rank1 {}; + // Use auto -> decltype as an enabler. template static auto transfer_impl(Alloc* alloc, slot_type* new_slot, - slot_type* old_slot, int) + slot_type* old_slot, Rank0) -> decltype((void)P::transfer(alloc, new_slot, old_slot)) { P::transfer(alloc, new_slot, old_slot); } - template - static void transfer_impl(Alloc* alloc, slot_type* new_slot, - slot_type* old_slot, char) { #if defined(__cpp_lib_launder) && __cpp_lib_launder >= 201606 - if (absl::is_trivially_relocatable()) { - // TODO(b/247130232,b/251814870): remove casts after fixing warnings. - std::memcpy(static_cast( - std::launder(const_cast*>( - &element(new_slot)))), - static_cast(&element(old_slot)), - sizeof(value_type)); - return; - } + // This overload returns true_type for the trait below. + // The conditional_t is to make the enabler type dependent. + template >::value>> + static std::true_type transfer_impl(Alloc*, slot_type* new_slot, + slot_type* old_slot, Rank1) { + // TODO(b/247130232): remove casts after fixing warnings. + // TODO(b/251814870): remove casts after fixing warnings. + std::memcpy( + static_cast(std::launder( + const_cast*>(&element(new_slot)))), + static_cast(&element(old_slot)), sizeof(value_type)); + return {}; + } #endif + template + static void transfer_impl(Alloc* alloc, slot_type* new_slot, + slot_type* old_slot, Rank2) { construct(alloc, new_slot, std::move(element(old_slot))); destroy(alloc, old_slot); } -- cgit v1.2.3