From c6b3f2cf583d8fec124f9d70a172b513363506fe Mon Sep 17 00:00:00 2001 From: Abseil Team Date: Thu, 13 Aug 2020 14:05:00 -0700 Subject: Export of internal Abseil changes -- 0c8282d75798c77733eee6167870bcc6acc0bfc1 by Evan Brown : Provide mutable access to the key in node handles using std::launder when compiled with C++17 or later. Also, document why we can't provide mutable access to the key without C++17. Note: we use Policy::mutable_key() because btree already uses Policy::key() internally to get const key access, and we want to avoid calling std::launder unless we need mutable access to the key. PiperOrigin-RevId: 326519000 -- 8018d0c3044400f0a731b0d2d00b606742c98818 by Xiaoyi Zhang : Move `Status` internal symbols from the public header into an internal header file. PiperOrigin-RevId: 326471847 -- 87a7644864ba7c003b0611898aaba1b71c840376 by Abseil Team : Avoid a costly divide (the division accounts for 10% of the time spent in the function). When the division is signed, the compiler has to generate a div. When it is unsigned, it can generate a shift: https://godbolt.org/z/vGfTv4. As per the test above the div, we know that the value is unsigned. PiperOrigin-RevId: 326453275 GitOrigin-RevId: 0c8282d75798c77733eee6167870bcc6acc0bfc1 Change-Id: I0a953558358055ab3dc6a533d8930698509b1195 --- absl/container/internal/btree.h | 7 ++++++- absl/container/internal/common.h | 7 +++++-- absl/container/internal/container_memory.h | 15 ++++++++++++++ absl/container/internal/hash_policy_traits.h | 31 +++++++++++++++++++++------- 4 files changed, 50 insertions(+), 10 deletions(-) (limited to 'absl/container/internal') diff --git a/absl/container/internal/btree.h b/absl/container/internal/btree.h index 293e5771..4d405734 100644 --- a/absl/container/internal/btree.h +++ b/absl/container/internal/btree.h @@ -292,6 +292,11 @@ struct map_params : common_params decltype(slot_policy::mutable_key(s)) { + return slot_policy::mutable_key(s); + } static mapped_type &value(value_type *value) { return value->second; } }; @@ -1043,7 +1048,7 @@ class btree { #endif } - enum { + enum : uint32_t { kNodeValues = node_type::kNodeValues, kMinNodeValues = kNodeValues / 2, }; diff --git a/absl/container/internal/common.h b/absl/container/internal/common.h index 8990f294..030e9d4a 100644 --- a/absl/container/internal/common.h +++ b/absl/container/internal/common.h @@ -146,8 +146,11 @@ class node_handle decltype(PolicyTraits::key(std::declval())) { - return PolicyTraits::key(this->slot()); + // When C++17 is available, we can use std::launder to provide mutable + // access to the key. Otherwise, we provide const access. + auto key() const + -> decltype(PolicyTraits::mutable_key(std::declval())) { + return PolicyTraits::mutable_key(this->slot()); } mapped_type& mapped() const { diff --git a/absl/container/internal/container_memory.h b/absl/container/internal/container_memory.h index c7777a93..d02c576d 100644 --- a/absl/container/internal/container_memory.h +++ b/absl/container/internal/container_memory.h @@ -18,6 +18,7 @@ #include #include #include +#include #include #include #include @@ -352,6 +353,20 @@ struct map_slot_policy { return slot->value; } + // When C++17 is available, we can use std::launder to provide mutable + // access to the key for use in node handle. +#if defined(__cpp_lib_launder) && __cpp_lib_launder >= 201606 + static K& mutable_key(slot_type* slot) { + // Still check for kMutableKeys so that we can avoid calling std::launder + // unless necessary because it can interfere with optimizations. + return kMutableKeys::value ? slot->key + : *std::launder(const_cast( + std::addressof(slot->value.first))); + } +#else // !(defined(__cpp_lib_launder) && __cpp_lib_launder >= 201606) + static const K& mutable_key(slot_type* slot) { return key(slot); } +#endif + static const K& key(const slot_type* slot) { return kMutableKeys::value ? slot->key : slot->value.first; } diff --git a/absl/container/internal/hash_policy_traits.h b/absl/container/internal/hash_policy_traits.h index 3e1209c6..46c97b18 100644 --- a/absl/container/internal/hash_policy_traits.h +++ b/absl/container/internal/hash_policy_traits.h @@ -17,6 +17,7 @@ #include #include +#include #include #include @@ -29,15 +30,34 @@ namespace container_internal { // Defines how slots are initialized/destroyed/moved. template struct hash_policy_traits { + // The type of the keys stored in the hashtable. + using key_type = typename Policy::key_type; + private: struct ReturnKey { - // We return `Key` here. + // When C++17 is available, we can use std::launder to provide mutable + // access to the key for use in node handle. +#if defined(__cpp_lib_launder) && __cpp_lib_launder >= 201606 + template ::value, int> = 0> + static key_type& Impl(Key&& k, int) { + return *std::launder( + const_cast(std::addressof(std::forward(k)))); + } +#endif + + template + static Key Impl(Key&& k, char) { + return std::forward(k); + } + // When Key=T&, we forward the lvalue reference. // When Key=T, we return by value to avoid a dangling reference. // eg, for string_hash_map. template - Key operator()(Key&& k, const Args&...) const { - return std::forward(k); + auto operator()(Key&& k, const Args&...) const + -> decltype(Impl(std::forward(k), 0)) { + return Impl(std::forward(k), 0); } }; @@ -52,9 +72,6 @@ struct hash_policy_traits { // The actual object stored in the hash table. using slot_type = typename Policy::slot_type; - // The type of the keys stored in the hashtable. - using key_type = typename Policy::key_type; - // The argument type for insertions into the hashtable. This is different // from value_type for increased performance. See initializer_list constructor // and insert() member functions for more details. @@ -156,7 +173,7 @@ struct hash_policy_traits { // Returns the "key" portion of the slot. // Used for node handle manipulation. template - static auto key(slot_type* slot) + static auto mutable_key(slot_type* slot) -> decltype(P::apply(ReturnKey(), element(slot))) { return P::apply(ReturnKey(), element(slot)); } -- cgit v1.2.3