Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,6 @@
[submodule "third_party/wide-integer"]
path = third_party/wide-integer
url = ../../ckormanyos/wide-integer.git
[submodule "third_party/parallel-hashmap"]
path = third_party/parallel-hashmap
url = ../../greg7mdp/parallel-hashmap.git
2 changes: 1 addition & 1 deletion libgringo/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ set(source
# ]]]

add_library(libgringo STATIC ${header} ${source})
target_link_libraries(libgringo PUBLIC libpotassco libreify tsl::ordered_map tsl::hopscotch_map tsl::sparse_map tl::optional mpark::variant math::wide_integer)
target_link_libraries(libgringo PUBLIC libpotassco libreify tsl::ordered_map tsl::hopscotch_map tsl::sparse_map tl::optional mpark::variant math::wide_integer grep::phmap)
target_include_directories(libgringo
PUBLIC
"$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}>"
Expand Down
37 changes: 18 additions & 19 deletions libgringo/src/symbol.cc
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,12 @@

#include <algorithm>
#include <cstring>
#include <mutex>

#include <parallel_hashmap/phmap.h>

#include <gringo/hash_set.hh>
#include <gringo/symbol.hh>
#include <iterator>
#include <mutex>

#ifdef _MSC_VER
#pragma warning(disable : 4200) // nonstandard extension used: zero-sized array in struct/union
Expand Down Expand Up @@ -79,28 +81,22 @@ String toString(uint64_t rep) { return String::fromRep(ptr(rep)); }

template <class T> struct UniqueConstruct {
public:
using Set = hash_set<T, typename T::Hash, typename T::EqualTo>;
using Set =
phmap::parallel_flat_hash_set<T, typename T::Hash, typename T::EqualTo, std::allocator<T>, 6, std::mutex>;

template <class U> static T const &construct(U &&x) {
// TODO: in C++17 this can use a read/write lock to not block reading threads
size_t hash = typename T::Hash{}(x);
std::lock_guard<std::mutex> g(mutex_);
auto it = set_.find(x, hash);
if (it != set_.end()) {
return *it;
}
return *set_.insert(T{std::forward<U>(x), hash}).first;
size_t raw_hash = typename T::Hash{}(x);
size_t mixed_hash = phmap::phmap_mix<sizeof(size_t)>()(raw_hash);
return *set_.lazy_emplace_with_hash(x, mixed_hash,
[&x, raw_hash](auto const &ctor) { ctor(std::forward<U>(x), raw_hash); });
}

private:
static Set set_; // NOLINT
static std::mutex mutex_; // NOLINT
static Set set_; // NOLINT
};

template <class T> typename UniqueConstruct<T>::Set UniqueConstruct<T>::set_; // NOLINT

template <class T> typename std::mutex UniqueConstruct<T>::mutex_; // NOLINT

template <class T, class U> T const &construct_unique(U &&x) {
return UniqueConstruct<T>::construct(std::forward<U>(x));
}
Expand All @@ -112,8 +108,9 @@ class MSig {
using Cons = std::pair<String, uint32_t>;

struct Hash {
using is_transparent = void;
size_t operator()(MSig const &sig) const { return sig.hash_; }
size_t operator()(Cons const &sig) const { return hash_mix(get_value_hash(sig)); }
size_t operator()(Cons const &sig) const { return get_value_hash(sig); }
};
struct EqualTo {
using is_transparent = void;
Expand Down Expand Up @@ -190,9 +187,10 @@ class MFun {
using Cons = std::pair<Sig, SymSpan>;

struct Hash {
using is_transparent = void;
size_t operator()(MFun const &a) const { return a.fun_->hash(); }
size_t operator()(Cons const &a) const {
return hash_mix(get_value_hash(a.first, hash_range(begin(a.second), end(a.second))));
return get_value_hash(a.first, hash_range(begin(a.second), end(a.second)));
}
};
struct EqualTo {
Expand Down Expand Up @@ -285,9 +283,10 @@ class String::Impl {
class String::Impl::MString {
public:
struct Hash {
using is_transparent = void;
size_t operator()(MString const &str) const { return str.str_->hash(); }
size_t operator()(char const *str) const { return hash_mix(strhash(str)); }
size_t operator()(StringSpan const &str) const { return hash_mix(strhash(str)); }
size_t operator()(char const *str) const { return strhash(str); }
size_t operator()(StringSpan const &str) const { return strhash(str); }
};
struct EqualTo {
using is_transparent = void;
Expand Down
4 changes: 4 additions & 0 deletions third_party/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,7 @@ add_library(mpark::variant ALIAS variant)
add_library(wide_integer INTERFACE)
target_include_directories(wide_integer INTERFACE $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/wide-integer>)
add_library(math::wide_integer ALIAS wide_integer)

add_library(phmap INTERFACE)
target_include_directories(phmap INTERFACE $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/parallel-hashmap>)
add_library(grep::phmap ALIAS phmap)
1 change: 1 addition & 0 deletions third_party/parallel-hashmap
Submodule parallel-hashmap added at 8442f1
Loading