Merge pull request #175 from boostorg/feature/optimized_try_emplace

feature/optimized_try_emplace
This commit is contained in:
joaquintides
2022-12-13 09:24:56 +01:00
committed by GitHub

View File

@ -1023,6 +1023,8 @@ inline void prefetch(const void* p)
#endif #endif
} }
struct try_emplace_args_t{};
#if defined(BOOST_GCC) #if defined(BOOST_GCC)
/* GCC's -Wshadow triggers at scenarios like this: /* GCC's -Wshadow triggers at scenarios like this:
* *
@ -1316,12 +1318,10 @@ public:
template<typename Key,typename... Args> template<typename Key,typename... Args>
BOOST_FORCEINLINE std::pair<iterator,bool> try_emplace( BOOST_FORCEINLINE std::pair<iterator,bool> try_emplace(
Key&& k,Args&&... args) Key&& x,Args&&... args)
{ {
return emplace_impl( return emplace_impl(
std::piecewise_construct, try_emplace_args_t{},std::forward<Key>(x),std::forward<Args>(args)...);
std::forward_as_tuple(std::forward<Key>(k)),
std::forward_as_tuple(std::forward<Args>(args)...));
} }
BOOST_FORCEINLINE std::pair<iterator,bool> BOOST_FORCEINLINE std::pair<iterator,bool>
@ -1510,6 +1510,37 @@ private:
alloc_traits::construct(al(),p,std::forward<Args>(args)...); alloc_traits::construct(al(),p,std::forward<Args>(args)...);
} }
template<typename... Args>
void construct_element(value_type* p,try_emplace_args_t,Args&&... args)
{
construct_element_from_try_emplace_args(
p,
std::integral_constant<bool,std::is_same<key_type,value_type>::value>{},
std::forward<Args>(args)...);
}
template<typename Key,typename... Args>
void construct_element_from_try_emplace_args(
value_type* p,std::false_type,Key&& x,Args&&... args)
{
alloc_traits::construct(
al(),p,
std::piecewise_construct,
std::forward_as_tuple(std::forward<Key>(x)),
std::forward_as_tuple(std::forward<Args>(args)...));
}
/* This overload allows boost::unordered_flat_set to internally use
* try_emplace to implement heterogeneous insert (P2363).
*/
template<typename Key>
void construct_element_from_try_emplace_args(
value_type* p,std::true_type,Key&& x)
{
alloc_traits::construct(al(),p,std::forward<Key>(x));
}
void destroy_element(value_type* p)noexcept void destroy_element(value_type* p)noexcept
{ {
alloc_traits::destroy(al(),p); alloc_traits::destroy(al(),p);
@ -1631,12 +1662,11 @@ private:
return type_policy::extract(x); return type_policy::extract(x);
} }
template<typename Arg1,typename Arg2> template<typename Key,typename... Args>
static inline auto key_from( static inline const Key& key_from(
std::piecewise_construct_t,const Arg1& k,const Arg2&) try_emplace_args_t,const Key& x,const Args&...)
->decltype(std::get<0>(k))
{ {
return std::get<0>(k); return x;
} }
template<typename Key> template<typename Key>