Skip to content

Commit

Permalink
bela: Fix c++23 warning
Browse files Browse the repository at this point in the history
  • Loading branch information
fcharlie committed Sep 17, 2023
1 parent 47836da commit da3e775
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 23 deletions.
2 changes: 1 addition & 1 deletion include/bela/__phmap/VERSION
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
https://github.com/greg7mdp/parallel-hashmap.git
77cab8192a879e5d27188f97e8f2080dd7e36ca8
81ca45376940d4bfd56754608c8ed6c1900ab1e9
60 changes: 42 additions & 18 deletions include/bela/__phmap/phmap.h
Original file line number Diff line number Diff line change
Expand Up @@ -1268,21 +1268,16 @@ class raw_hash_set
size_t max_size() const { return (std::numeric_limits<size_t>::max)(); }

PHMAP_ATTRIBUTE_REINITIALIZES void clear() {
// Iterating over this container is O(bucket_count()). When bucket_count()
// is much greater than size(), iteration becomes prohibitively expensive.
// For clear() it is more important to reuse the allocated array when the
// container is small because allocation takes comparatively long time
// compared to destruction of the elements of the container. So we pick the
// largest bucket_count() threshold for which iteration is still fast and
// past that we simply deallocate the array.
if (empty())
return;
if (capacity_ > 127) {
destroy_slots();
} else if (capacity_) {
for (size_t i = 0; i != capacity_; ++i) {
if (IsFull(ctrl_[i])) {
PolicyTraits::destroy(&alloc_ref(), slots_ + i);
if (capacity_) {
if constexpr (!std::is_trivially_destructible<typename PolicyTraits::value_type>::value ||
std::is_same<typename Policy::is_flat, std::false_type>::value) {
// node map or not trivially destructible... we need to iterate and destroy values one by one
for (size_t i = 0; i != capacity_; ++i) {
if (IsFull(ctrl_[i])) {
PolicyTraits::destroy(&alloc_ref(), slots_ + i);
}
}
}
size_ = 0;
Expand Down Expand Up @@ -2011,12 +2006,19 @@ class raw_hash_set
}

void destroy_slots() {
if (!capacity_) return;
for (size_t i = 0; i != capacity_; ++i) {
if (IsFull(ctrl_[i])) {
PolicyTraits::destroy(&alloc_ref(), slots_ + i);
if (!capacity_)
return;

if constexpr (!std::is_trivially_destructible<typename PolicyTraits::value_type>::value ||
std::is_same<typename Policy::is_flat, std::false_type>::value) {
// node map, or not trivially destructible... we need to iterate and destroy values one by one
// std::cout << "either this is a node map or " << type_name<typename PolicyTraits::value_type>() << " is not trivially_destructible\n";
for (size_t i = 0; i != capacity_; ++i) {
if (IsFull(ctrl_[i])) {
PolicyTraits::destroy(&alloc_ref(), slots_ + i);
}
}
}
}
auto layout = MakeLayout(capacity_);
// Unpoison before returning the memory to the allocator.
SanitizerUnpoisonMemoryRegion(slots_, sizeof(slot_type) * capacity_);
Expand Down Expand Up @@ -4020,6 +4022,24 @@ class parallel_hash_map : public parallel_hash_set<N, RefSet, Mtx_, Policy, Hash
return std::get<2>(res);
}

// returns {pointer, bool} instead of {iterator, bool} per try_emplace.
// useful for node-based containers, since the pointer is not invalidated by concurrent insert etc.
template <class K = key_type, class... Args>
std::pair<typename parallel_hash_map::parallel_hash_set::pointer, bool> try_emplace_p(K&& k, Args&&... args) {
size_t hashval = this->hash(k);
typename Lockable::UniqueLock m;
auto res = this->find_or_prepare_insert_with_hash(hashval, k, m);
typename Base::Inner *inner = std::get<0>(res);
if (std::get<2>(res)) {
inner->set_.emplace_at(std::get<1>(res), std::piecewise_construct,
std::forward_as_tuple(std::forward<K>(k)),
std::forward_as_tuple(std::forward<Args>(args)...));
inner->set_.set_ctrl(std::get<1>(res), H2(hashval));
}
auto it = this->iterator_at(inner, inner->set_.iterator_at(std::get<1>(res)));
return {&*it, std::get<2>(res)};
}

// ----------- end of phmap extensions --------------------------

template <class K = key_type, class P = Policy, K* = nullptr>
Expand Down Expand Up @@ -4184,6 +4204,7 @@ struct FlatHashSetPolicy
using key_type = T;
using init_type = T;
using constant_iterators = std::true_type;
using is_flat = std::true_type;

template <class Allocator, class... Args>
static void construct(Allocator* alloc, slot_type* slot, Args&&... args) {
Expand Down Expand Up @@ -4226,6 +4247,7 @@ struct FlatHashMapPolicy
using key_type = K;
using mapped_type = V;
using init_type = std::pair</*non const*/ key_type, mapped_type>;
using is_flat = std::true_type;

template <class Allocator, class... Args>
static void construct(Allocator* alloc, slot_type* slot, Args&&... args) {
Expand Down Expand Up @@ -4308,6 +4330,7 @@ struct NodeHashSetPolicy
using key_type = T;
using init_type = T;
using constant_iterators = std::true_type;
using is_flat = std::false_type;

template <class Allocator, class... Args>
static T* new_element(Allocator* alloc, Args&&... args) {
Expand Down Expand Up @@ -4353,6 +4376,7 @@ class NodeHashMapPolicy
using key_type = Key;
using mapped_type = Value;
using init_type = std::pair</*non const*/ key_type, mapped_type>;
using is_flat = std::false_type;

template <class Allocator, class... Args>
static value_type* new_element(Allocator* alloc, Args&&... args) {
Expand Down
20 changes: 16 additions & 4 deletions include/bela/int128.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,14 @@
#define BELA_INTERNAL_WCHAR_T wchar_t
#endif // defined(_MSC_VER)

#ifndef _BLEA_INT128_HAS_CXX23
#if ((defined(_MSVC_LANG) && _MSVC_LANG > 202002L) || __cplusplus > 202002L)
#define _BLEA_INT128_HAS_CXX23 1
#else
#define _BLEA_INT128_HAS_CXX23 0
#endif
#endif // _BLEA_HAS_CXX23

namespace bela {

class int128;
Expand Down Expand Up @@ -254,8 +262,10 @@ template <> class numeric_limits<bela::uint128> {
static constexpr bool has_infinity = false;
static constexpr bool has_quiet_NaN = false;
static constexpr bool has_signaling_NaN = false;
#if !_BLEA_INT128_HAS_CXX23
static constexpr float_denorm_style has_denorm = denorm_absent;
static constexpr bool has_denorm_loss = false;
#endif
static constexpr float_round_style round_style = round_toward_zero;
static constexpr bool is_iec559 = false;
static constexpr bool is_bounded = true;
Expand Down Expand Up @@ -482,8 +492,10 @@ template <> class numeric_limits<bela::int128> {
static constexpr bool has_infinity = false;
static constexpr bool has_quiet_NaN = false;
static constexpr bool has_signaling_NaN = false;
#if !_BLEA_INT128_HAS_CXX23
static constexpr float_denorm_style has_denorm = denorm_absent;
static constexpr bool has_denorm_loss = false;
#endif
static constexpr float_round_style round_style = round_toward_zero;
static constexpr bool is_iec559 = false;
static constexpr bool is_bounded = true;
Expand Down Expand Up @@ -619,8 +631,8 @@ constexpr uint128::uint128(unsigned long long v) : lo_{v}, hi_{0} {}

#ifdef BELA_HAVE_INTRINSIC_INT128
constexpr uint128::uint128(__int128 v)
: lo_{static_cast<uint64_t>(v & ~uint64_t{0})}, hi_{static_cast<uint64_t>(static_cast<unsigned __int128>(v) >>
64)} {}
: lo_{static_cast<uint64_t>(v & ~uint64_t{0})},
hi_{static_cast<uint64_t>(static_cast<unsigned __int128>(v) >> 64)} {}
constexpr uint128::uint128(unsigned __int128 v)
: lo_{static_cast<uint64_t>(v & ~uint64_t{0})}, hi_{static_cast<uint64_t>(v >> 64)} {}
#endif // BELA_HAVE_INTRINSIC_INT128
Expand All @@ -646,8 +658,8 @@ constexpr uint128::uint128(unsigned long long v) : hi_{0}, lo_{v} {}

#ifdef BELA_HAVE_INTRINSIC_INT128
constexpr uint128::uint128(__int128 v)
: hi_{static_cast<uint64_t>(static_cast<unsigned __int128>(v) >> 64)}, lo_{static_cast<uint64_t>(v &
~uint64_t{0})} {}
: hi_{static_cast<uint64_t>(static_cast<unsigned __int128>(v) >> 64)},
lo_{static_cast<uint64_t>(v & ~uint64_t{0})} {}
constexpr uint128::uint128(unsigned __int128 v)
: hi_{static_cast<uint64_t>(v >> 64)}, lo_{static_cast<uint64_t>(v & ~uint64_t{0})} {}
#endif // BELA_HAVE_INTRINSIC_INT128
Expand Down

0 comments on commit da3e775

Please sign in to comment.