Skip to content

Commit

Permalink
refactor: Replace phmap with boost.unordered
Browse files Browse the repository at this point in the history
Minimize dependence on external projects by dropping `parallel-hashmap`
and replacing it with `boost-unordered`. `boost-unordered` provides a
fast `unordered_map` implementation since `1.80` and an even faster
`unordered_flat_map` since `1.81`. Therefore we bump the required
version.

Properly assert the required version during CMake configuration as well
as during transitive dependency lookup.
  • Loading branch information
BurningEnlightenment committed Apr 4, 2023
1 parent 485177f commit e596f42
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 19 deletions.
6 changes: 3 additions & 3 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -58,14 +58,14 @@ mark_as_advanced(DPLX_DP_USE_BRANCHING_INTEGER_ENCODER)

set(Boost_NO_WARN_NEW_VERSIONS ON)

find_package(concrete CONFIG REQUIRED)
find_package(concrete 0.0 CONFIG REQUIRED)

find_package(fmt CONFIG REQUIRED)
find_package(fmt 9 CONFIG REQUIRED)

find_package(status-code CONFIG REQUIRED)
find_package(outcome CONFIG REQUIRED)

find_package(Boost 1.71 REQUIRED)
find_package(Boost 1.81 REQUIRED)

find_package(yaml-cpp CONFIG)
set_package_properties(yaml-cpp PROPERTIES
Expand Down
8 changes: 8 additions & 0 deletions src/dplx/dp/detail/workaround.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

#pragma once

#include <dplx/predef/library/std.h>
#include <dplx/predef/version_number.h>
#include <dplx/predef/workaround.h>

Expand All @@ -27,3 +28,10 @@
DPLX_XDEF_WORKAROUND_TESTED_AT(DPLX_DP_DISABLE_WORKAROUNDS, \
DPLX_DP_FLAG_OUTDATED_WORKAROUNDS, symbol, \
major, minor, patch)

////////////////////////////////////////////////////////////////////////////////

// libstdc++ fails to forward pair members during uses_allocator construction
// see https://gcc.gnu.org/bugzilla/show_bug.cgi?id=108952
#define DPLX_DP_WORKAROUND_ISSUE_LIBSTDCPP_108952 \
DPLX_DP_WORKAROUND_TESTED_AT(DPLX_LIB_STD_GNU, 12, 2, 0)
30 changes: 18 additions & 12 deletions src/dplx/dp/state.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,12 @@
#include <memory_resource>
#include <utility>

#include <parallel_hashmap/phmap.h>
#include <boost/unordered/unordered_flat_map.hpp>

#include <dplx/cncr/utils.hpp>
#include <dplx/cncr/uuid.hpp>

#include <dplx/dp/detail/workaround.hpp>
#include <dplx/dp/fwd.hpp>

namespace dplx::dp::detail
Expand Down Expand Up @@ -121,6 +122,13 @@ class unique_erased_state_ptr
, mDelete(std::exchange(other.mDelete, nullptr))
{
}
#if DPLX_DP_WORKAROUND_ISSUE_LIBSTDCPP_108952
constexpr unique_erased_state_ptr(unique_erased_state_ptr &other) noexcept
: mObj(std::exchange(other.mObj, nullptr))
, mDelete(std::exchange(other.mDelete, nullptr))
{
}
#endif
constexpr auto operator=(unique_erased_state_ptr &&other) noexcept
-> unique_erased_state_ptr &
{
Expand Down Expand Up @@ -195,7 +203,7 @@ namespace dplx::dp::detail
{

template <typename Key, typename T>
using flat_hash_map = phmap::flat_hash_map<
using flat_hash_map = boost::unordered_flat_map<
Key,
T,
std::hash<Key>,
Expand Down Expand Up @@ -278,14 +286,14 @@ class state_store
auto emplace(state_key<StateT> const &key, Args &&...args)
-> std::pair<StateT *, bool>
{
auto const h = mImpl.hash(key.value);
if (auto it = mImpl.find(key.value, h); it != mImpl.end())
auto hint = mImpl.find(key.value);
if (hint != mImpl.end())
{
// this way, we don't need to allocate if key already has a value
return {it->second.template get<StateT>(), false};
return {hint->second.template get<StateT>(), false};
}
auto [it, emplaced] = mImpl.emplace_with_hash(
h, key.value,
auto it = mImpl.emplace_hint(
hint, key.value,
detail::allocate_state<StateT, allocator_type>(
mImpl.get_allocator(), static_cast<Args &&>(args)...));
return {it->second.template get<StateT>(), true};
Expand Down Expand Up @@ -401,15 +409,13 @@ class link_store
template <state_link T>
auto replace(state_link_key<T> const &key, T value) -> T
{
auto const h = mImpl.hash(key.value);
auto it = mImpl.find(key.value, h);
auto it = mImpl.find(key.value);
if (it == mImpl.end())
{
if (value != T{})
{
mImpl.emplace_with_hash(
h, key.value,
detail::erasure_cast<erased_type, T>(value));
mImpl.emplace_hint(it, key.value,
detail::erasure_cast<erased_type, T>(value));
}
return T{};
}
Expand Down
7 changes: 4 additions & 3 deletions tools/deeppack-config.cmake.in
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
@PACKAGE_INIT@

include(CMakeFindDependencyMacro)
find_dependency(Boost)
find_dependency(fmt)
find_dependency(Boost 1.81)
find_dependency(fmt 9)
find_dependency(status-code)
find_dependency(outcome)
find_dependency(concrete)
find_dependency(concrete 0.0)

include("${CMAKE_CURRENT_LIST_DIR}/deeppack-targets.cmake")

Expand Down
2 changes: 1 addition & 1 deletion vcpkg.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@
"boost-endian",
"boost-container",
"boost-mp11",
"boost-unordered",
"concrete",
"fmt",
"outcome",
"parallel-hashmap",
"status-code"
],
"features": {
Expand Down

0 comments on commit e596f42

Please sign in to comment.