dropped foa_mixer in favor of internal mix policy governed by hash_traits

This commit is contained in:
joaquintides
2022-10-08 11:47:00 +02:00
parent 16f86b0770
commit 3913fce638
4 changed files with 157 additions and 179 deletions

View File

@ -21,6 +21,8 @@
#include <boost/cstdint.hpp>
#include <boost/predef.h>
#include <boost/type_traits/is_nothrow_swappable.hpp>
#include <boost/unordered/detail/xmx.hpp>
#include <boost/unordered/hash_traits.hpp>
#include <climits>
#include <cmath>
#include <cstddef>
@ -752,6 +754,19 @@ struct table_arrays
value_type *elements;
};
struct no_mix
{
static inline std::size_t mix(std::size_t x)noexcept{return x;}
};
struct xmx_mix
{
static inline std::size_t mix(std::size_t x)noexcept
{
return xmx(x);
}
};
struct if_constexpr_void_else{void operator()()const{}};
template<bool B,typename F,typename G=if_constexpr_void_else>
@ -827,6 +842,11 @@ table:empty_value<Hash,0>,empty_value<Pred,1>,empty_value<Allocator,1>
static constexpr auto N=group_type::N;
using size_policy=pow2_size_policy;
using prober=pow2_quadratic_prober;
using mix_policy=typename std::conditional<
hash_traits<Hash>::is_avalanching::value,
no_mix,
xmx_mix
>::type;
using alloc_traits=boost::allocator_traits<Allocator>;
public:
@ -1158,7 +1178,7 @@ public:
template<typename Key>
BOOST_FORCEINLINE iterator find(const Key& x)
{
auto hash=h()(x);
auto hash=hash_for(x);
return find_impl(x,position_for(hash),hash);
}
@ -1285,6 +1305,12 @@ private:
return std::get<0>(k);
}
template<typename Key>
inline std::size_t hash_for(const Key& x)
{
return mix_policy::mix(h()(x));
}
inline std::size_t position_for(std::size_t hash)const
{
return position_for(hash,arrays);
@ -1341,7 +1367,7 @@ private:
BOOST_FORCEINLINE std::pair<iterator,bool> emplace_impl(Args&&... args)
{
const auto &k=key_from(std::forward<Args>(args)...);
auto hash=h()(k);
auto hash=hash_for(k);
auto pos0=position_for(hash);
auto it=find_impl(k,pos0,hash);
@ -1398,13 +1424,13 @@ private:
template<typename Value>
void unchecked_insert(Value&& x)
{
auto hash=h()(key_from(x));
auto hash=hash_for(key_from(x));
unchecked_emplace_at(position_for(hash),hash,std::forward<Value>(x));
}
void nosize_transfer_element(value_type* p,const arrays_type& arrays_)
{
auto hash=h()(key_from(*p));
auto hash=hash_for(key_from(*p));
nosize_unchecked_emplace_at(
arrays_,position_for(hash,arrays_),hash,type_policy::move(*p));
destroy_element(p);

View File

@ -1,175 +0,0 @@
/* Hash mixer for boost::unordered::unordered_flat_[map|set].
*
* Copyright 2022 Joaquin M Lopez Munoz.
* Copyright 2022 Peter Dimov.
* Distributed under the Boost Software License, Version 1.0.
* (See accompanying file LICENSE_1_0.txt or copy at
* http://www.boost.org/LICENSE_1_0.txt)
*
* See https://www.boost.org/libs/unordered for library home page.
*/
#ifndef BOOST_UNORDERED_DETAIL_FOA_MIXER_HPP
#define BOOST_UNORDERED_DETAIL_FOA_MIXER_HPP
#include <boost/config.hpp>
#include <boost/container_hash/hash_fwd.hpp>
#include <boost/core/empty_value.hpp>
#include <boost/cstdint.hpp>
#include <climits>
#include <cstddef>
#include <type_traits>
#include <utility>
namespace boost{
namespace unordered{
namespace detail{
namespace foa{
/* mixer<Hash> is functionally equivalent to Hash except if Hash is
* boost::hash<T> for any of the Ts where boost::hash implements a
* trivial hashing function not fit for open-addressing hash container:
* in these cases, the result of boost::hash is post-mixed using
*
* - 64 bits: xmx (TODO: Peter Dimov to explain)
* - 32 bits: xmx33 (TODO: Peter Dimov to explain)
*/
#if defined(BOOST_GCC)
/* GCC's -Wshadow triggers at scenarios like this:
*
* struct foo{};
* template<typename Base>
* struct derived:Base
* {
* void f(){int foo;}
* };
*
* derived<foo>x;
* x.f(); // declaration of "foo" in derived::f shadows base type "foo"
*
* This makes shadowing warnings unavoidable in general when a class template
* derives from a user-provided class, as is the case with mixer_impl.
*/
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wshadow"
#endif
template<typename Hash,typename MixPolicy>
class mixer_impl:empty_value<Hash>
{
public:
using base=empty_value<Hash>;
#if BOOST_CXX_VERSION<201703L
using argument_type=typename Hash::argument_type;
using result_type=std::size_t;
#endif
mixer_impl()=default;
template<typename... Args>
mixer_impl(Args&&... args):base{empty_init,std::forward<Args>(args)...}{}
Hash& get_base()noexcept{return base::get();}
const Hash& get_base()const noexcept{return base::get();}
template<
typename Key,
typename std::enable_if<
std::is_same<
std::size_t,
decltype(std::declval<const Hash>()(std::declval<const Key&>()))
>::value
>::type* =nullptr
>
std::size_t operator()(const Key& x)const
noexcept(noexcept(std::declval<const Hash>()(x)))
{
return MixPolicy::mix(get_base()(x));
}
friend void swap(mixer_impl& x,mixer_impl& y)
{
using std::swap;
swap(x.get_base(),y.get_base());
}
};
#if defined(BOOST_GCC)
#pragma GCC diagnostic pop /* ignored "-Wshadow" */
#endif
struct no_mix
{
static inline std::size_t mix(std::size_t x)noexcept{return x;}
};
#if defined(SIZE_MAX)
#if ((((SIZE_MAX >> 16) >> 16) >> 16) >> 15) != 0
#define BOOST_UNORDERED_64B_ARCHITECTURE /* >64 bits assumed as 64 bits */
#endif
#elif defined(UINTPTR_MAX) /* used as proxy for std::size_t */
#if ((((UINTPTR_MAX >> 16) >> 16) >> 16) >> 15) != 0
#define BOOST_UNORDERED_64B_ARCHITECTURE
#endif
#endif
struct xmx_mix
{
static inline std::size_t mix(std::size_t x)noexcept
{
#if defined(BOOST_UNORDERED_64B_ARCHITECTURE)
boost::uint64_t z=x;
z^=z>> 23;
z*=0xff51afd7ed558ccdull;
z^=z>>23;
return (std::size_t)z;
#else /* 32 bits assumed */
x^=x>>18;
x*=0x56b5aaadu;
x^=x>>16;
return x;
#endif
}
};
#ifdef BOOST_UNORDERED_64B_ARCHITECTURE
#undef BOOST_UNORDERED_64B_ARCHITECTURE
#endif
template<typename Hash> struct is_boost_hash:std::false_type{};
template<typename Key> struct is_boost_hash<boost::hash<Key>>:std::true_type{};
template<typename Hash> struct boost_hash_key_impl{using type=void;};
template<typename Key> struct boost_hash_key_impl<boost::hash<Key>>
{
using type=Key;
};
template<typename Hash> using boost_hash_key=
typename boost_hash_key_impl<Hash>::type;
template<typename Hash>
using mixer=typename std::conditional<
is_boost_hash<Hash>::value&&(
std::is_integral<boost_hash_key<Hash>>::value||
std::is_enum<boost_hash_key<Hash>>::value||
std::is_floating_point<boost_hash_key<Hash>>::value|| // TODO: not sure about this one
std::is_pointer<boost_hash_key<Hash>>::value),
mixer_impl<Hash,xmx_mix>,
mixer_impl<Hash,no_mix>
>::type;
} /* namespace foa */
} /* namespace detail */
} /* namespace unordered */
} /* namespace boost */
#endif

View File

@ -0,0 +1,75 @@
/* 32b/64b xmx mix function.
*
* Copyright 2022 Peter Dimov.
* Copyright 2022 Joaquin M Lopez Munoz.
* Distributed under the Boost Software License, Version 1.0.
* (See accompanying file LICENSE_1_0.txt or copy at
* http://www.boost.org/LICENSE_1_0.txt)
*
* See https://www.boost.org/libs/unordered for library home page.
*/
#ifndef BOOST_UNORDERED_DETAIL_XMX_HPP
#define BOOST_UNORDERED_DETAIL_XMX_HPP
#include <boost/cstdint.hpp>
#include <climits>
#include <cstddef>
namespace boost{
namespace unordered{
namespace detail{
/* Bit mixer for improvement of statistical properties of hash functions.
* The implementation is different on 64bit and 32bit architectures:
*
* - 64bit: same as xmx function in
* http://jonkagstrom.com/bit-mixer-construction/index.html
* - 32bit: generated by Hash Function Prospector
* (https://github.com/skeeto/hash-prospector) and selected as the
* best overall performer in benchmarks of Boost.Unordered flat containers.
* Score assigned by Hash Prospector: 333.7934929677524
*/
#if defined(SIZE_MAX)
#if ((((SIZE_MAX >> 16) >> 16) >> 16) >> 15) != 0
#define BOOST_UNORDERED_64B_ARCHITECTURE /* >64 bits assumed as 64 bits */
#endif
#elif defined(UINTPTR_MAX) /* used as proxy for std::size_t */
#if ((((UINTPTR_MAX >> 16) >> 16) >> 16) >> 15) != 0
#define BOOST_UNORDERED_64B_ARCHITECTURE
#endif
#endif
static inline std::size_t xmx(std::size_t x)noexcept
{
#if defined(BOOST_UNORDERED_64B_ARCHITECTURE)
boost::uint64_t z=(boost::uint64_t)x;
z^=z>>23;
z*=0xff51afd7ed558ccdull;
z^=z>>23;
return (std::size_t)z;
#else /* 32 bits assumed */
x^=x>>18;
x*=0x56b5aaadu;
x^=x>>16;
return x;
#endif
}
#ifdef BOOST_UNORDERED_64B_ARCHITECTURE
#undef BOOST_UNORDERED_64B_ARCHITECTURE
#endif
} /* namespace detail */
} /* namespace unordered */
} /* namespace boost */
#endif

View File

@ -0,0 +1,52 @@
/* Hash function characterization.
*
* Copyright 2022 Joaquin M Lopez Munoz.
* Distributed under the Boost Software License, Version 1.0.
* (See accompanying file LICENSE_1_0.txt or copy at
* http://www.boost.org/LICENSE_1_0.txt)
*
* See https://www.boost.org/libs/unordered for library home page.
*/
#ifndef BOOST_UNORDERED_HASH_TRAITS_HPP
#define BOOST_UNORDERED_HASH_TRAITS_HPP
#include <boost/type_traits/make_void.hpp>
#include <type_traits>
namespace boost{
namespace unordered{
namespace detail{
template<typename Hash,typename=void>
struct hash_is_avalanching
{
using type=std::false_type;
};
template<typename Hash>
struct hash_is_avalanching<Hash,void_t<typename Hash::is_avalanching>>
{
using type=std::true_type;
};
} /* namespace detail */
/* Partially specializable by users for concrete hash functions when
* actual characterization differs from default.
*/
template<typename Hash>
struct hash_traits
{
/* std::true_type if the type Hash::is_avalanching is present,
* std::false_type otherwise.
*/
using is_avalanching=typename detail::hash_is_avalanching<Hash>::type;
};
} /* namespace unordered */
} /* namespace boost */
#endif