Skip to content

Commit

Permalink
Replace boost::container with cpp17::pmr
Browse files Browse the repository at this point in the history
This removes the boost dependency to avoid having to make that work in
our builds that are still using external packages. This change uses
std::pmr when it is detected as available and usable. On platforms that
don't have a working std::pmr (notably, macOS < 14), it uses the
implementation from Pablo Halpern that is implemented with purely C++11
features.

The major downfall to this approach is that if/when we start changing
allocator strategies it'll require us to backport/reimplement those
allocator strategies for macOS < 14. However, if we end up writing our
own allocation strategies they'll work on either implementation
seamlessly.
  • Loading branch information
davisp committed Feb 15, 2024
1 parent 2f96db6 commit 01b31c5
Show file tree
Hide file tree
Showing 11 changed files with 650 additions and 14 deletions.
9 changes: 9 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,15 @@ if(TILEDB_SANITIZER)
validate_sanitizer_options()
endif()

include(DetectStdPmr)

if(TILEDB_USE_CPP17_PMR)
message(STATUS "Building with cpp17::pmr")
add_definitions(-DUSE_CPP17_PMR)
else()
message(STATUS "Building with std::pmr")
endif()

#######################################################
# Header Files
#######################################################
Expand Down
69 changes: 69 additions & 0 deletions cmake/Modules/DetectStdPmr.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
#
# DetectStdPmr.cmake
#
#
# The MIT License
#
# Copyright (c) 2024 TileDB, Inc.
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
#
# Detect whether polymorphic allocators are available on the system.

# Special case for macOS when the MACOSX_DEPLOYMENT_TARGET is set to anything
# less than 14. For some reason, std::pmr is still detectable, but the resulting
# binary dies with a dyld missing symbol error.

if (ENV{MACOSX_DEPLOYMENT_TARGET})
string(COMPARE LESS "$ENV{MACOSX_DEPLOYMENT_TARGET}" "14" MACOS_BAD_PMR_SUPPORT)
if (MACOS_BAD_PMR_SUPPORT)
set(TILEDB_USE_CPP17_PMR ON)
message(STATUS "Using vendored cpp17::pmr for polymorphic allocators")
return()
endif()
endif()

# Otherwise, if we're not building a targeted macOS version, we just detect
# whether std::pmr is available.
#
# However CMake makes this extra awesome because try_run appears to have
# changed in a backwards compatible manner. We'll just version check for
# selecting which to run.

if (CMAKE_VERSION VERSION_LESS "3.25")
try_run(
TILEDB_CAN_RUN_STD_PMR
TILEDB_CAN_COMPILE_STD_PMR
"${CMAKE_CURRENT_BINARY_DIR}"
"${CMAKE_SOURCE_DIR}/cmake/inputs/detect_std_pmr.cc"
)
else()
try_run(
TILEDB_CAN_RUN_STD_PMR
TILEDB_CAN_COMPILE_STD_PMR
SOURCES "${CMAKE_SOURCE_DIR}/cmake/inputs/detect_std_pmr.cc"
)
endif()

if ("${TILEDB_CAN_COMPILE_STD_PMR}" AND "${TILEDB_CAN_RUN_STD_PMR}" EQUAL 0)
message(STATUS "Using std::pmr for polymorphic allocators")
else()
set(TILEDB_USE_CPP17_PMR ON)
message(STATUS "Using vendored cpp17::pmr for polymorphic allocators")
endif()
2 changes: 1 addition & 1 deletion cmake/TileDB-Superbuild.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -197,4 +197,4 @@ add_custom_target(package
DEPENDS tiledb
COMMAND ${CMAKE_CPACK_COMMAND} --config CPackConfig.cmake -G "$<IF:$<PLATFORM_ID:Windows>,ZIP,TGZ>"
WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/tiledb
)
)
7 changes: 7 additions & 0 deletions cmake/inputs/detect_std_pmr.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@

#include <memory_resource>

int
main() {
auto resource = std::pmr::get_default_resource();
}
5 changes: 3 additions & 2 deletions tiledb/common/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -65,10 +65,11 @@ commence(object_library baseline)
memory_tracker.cc
pmr.cc
)
if (TILEDB_USE_CPP17_PMR)
this_target_sources(polymorphic_allocator/polymorphic_allocator.cc)
endif()
find_package(Spdlog_EP REQUIRED)
find_package(Boost REQUIRED COMPONENTS container)
target_link_libraries(baseline PUBLIC spdlog::spdlog)
target_link_libraries(baseline PUBLIC Boost::container)
target_link_libraries(baseline PUBLIC common)
conclude(object_library)

Expand Down
6 changes: 5 additions & 1 deletion tiledb/common/pmr.cc
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,11 @@
namespace tiledb::common::pmr {

memory_resource* get_default_resource() {
return boost::container::pmr::get_default_resource();
#ifdef USE_CPP17_PMR
return cpp17::pmr::get_default_resource();
#else
return std::pmr::get_default_resource();
#endif
}

} // namespace tiledb::common::pmr
32 changes: 23 additions & 9 deletions tiledb/common/pmr.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,25 +37,39 @@
#include <unordered_map>
#include <vector>

#include <boost/container/pmr/memory_resource.hpp>
#include <boost/container/pmr/polymorphic_allocator.hpp>
#include <boost/container/pmr/vector.hpp>
#ifdef USE_CPP17_PMR
#include "polymorphic_allocator/polymorphic_allocator.h"
#else
#include <memory_resource>
#endif

#include "common.h"

namespace tiledb::common::pmr {

using memory_resource = boost::container::pmr::memory_resource;
#ifdef USE_CPP17_PMR

using memory_resource = cpp17::pmr::memory_resource;

template <class Tp>
using polymorphic_allocator = cpp17::pmr::polymorphic_allocator<Tp>;

#else

using memory_resource = std::pmr::memory_resource;

template <class Tp>
using polymorphic_allocator = std::pmr::polymorphic_allocator<Tp>;
#endif

memory_resource* get_default_resource();

/* ********************************* */
/* PMR VECTOR DECLARATION */
/* ********************************* */

template <class Tp>
using pmr_vector =
std::vector<Tp, boost::container::pmr::polymorphic_allocator<Tp>>;

memory_resource* get_default_resource();
using pmr_vector = std::vector<Tp, polymorphic_allocator<Tp>>;

template <class Tp>
class vector : public pmr_vector<Tp> {
Expand Down Expand Up @@ -141,7 +155,7 @@ using pmr_unordered_map = std::unordered_map<
T,
Hash,
KeyEqual,
boost::container::pmr::polymorphic_allocator<std::pair<const Key, T>>>;
polymorphic_allocator<std::pair<const Key, T>>>;

template <
class Key,
Expand Down
15 changes: 15 additions & 0 deletions tiledb/common/polymorphic_allocator/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
Polymorphic Allocator Fallback Implementation
===

This implementation of polymorphic_allocator was pulled from Pablo Halpern's
C++11 implementation available here:

https://github.com/phalpern/CppCon2017Code/tree/d26e7f4f6c593fe135c6b454aee93486790726b7

I have a personal forked copy here in case that repository ever dissappears:

https://github.com/davisp/phalpern-CppCon2017Code/tree/d26e7f4f6c593fe135c6b454aee93486790726b7

The only changes from the original files in that repository are to reformat
using TileDB coding style and adding a handful of `std::` namespace qualifiers
to avoid symbol name clashes with internal TileDB symbols.
24 changes: 24 additions & 0 deletions tiledb/common/polymorphic_allocator/polymorphic_allocator.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/* polymorphic_allocator.cpp -*-C++-*-
*
* Copyright 2012 Pablo Halpern.
* 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)
*/

#include "polymorphic_allocator.h"

namespace cpp17 {

atomic<pmr::memory_resource*> pmr::memory_resource::s_default_resource(nullptr);

pmr::new_delete_resource* pmr::new_delete_resource_singleton() {
// TBD: I think the standard makes this exception-safe, otherwise, we need
// to use 'call_once()' in '<mutex>'.
static new_delete_resource singleton;
return &singleton;
}

} // namespace cpp17

// end polymorphic_allocator.cpp
Loading

0 comments on commit 01b31c5

Please sign in to comment.