FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

an option to replace mimalloc with std::pmr::monotonic_buffer_resource by vthriller · Pull Request #1772 · solvespace/solvespace · GitHub

Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension .cpp  (1) .txt  (3) All 2 file types selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
53 changes: 33 additions & 20 deletions CMakeLists.txt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,11 @@ endif()
list(APPEND CMAKE_MODULE_PATH
"${CMAKE_SOURCE_DIR}/cmake/")

set(CMAKE_CXX_STANDARD 11)
if(ENABLE_STD_TEMP_HEAP)
set(CMAKE_CXX_STANDARD 17)
else()
set(CMAKE_CXX_STANDARD 11)
endif()
set(CMAKE_CXX_STANDARD_REQUIRED YES)

# for /MT on MSVC
Expand Down Expand Up @@ -56,6 +60,9 @@ else()
set(INCLUDE_NATIVE_COMPONENTS ON)
endif()

set(ENABLE_STD_TEMP_HEAP
OFF CACHE BOOL
"Use C++17 monotonic buffer instead of mimalloc")
set(ENABLE_GUI ON CACHE BOOL
"Whether the graphical interface is enabled")
set(USE_QT_GUI OFF CACHE BOOL
Expand Down Expand Up @@ -242,25 +249,27 @@ if(ENABLE_GUI OR ENABLE_CLI)
add_subdirectory(extlib/libdxfrw)
endif()

message(STATUS "Using in-tree mimalloc")
set(MI_OVERRIDE OFF CACHE BOOL "")
set(MI_BUILD_SHARED OFF CACHE BOOL "")
set(MI_BUILD_OBJECT OFF CACHE BOOL "")
set(MI_BUILD_TESTS OFF CACHE BOOL "")
if(EMSCRIPTEN)
# HACK: We only use mimalloc for the temporary heap, not as a general purpose
# allocator. However, the Emscripten port of mimalloc assumes that it is used
# as the system allocator, and depends on the slow emmalloc allocator.
# We do not want to enable emmalloc, as that would slow down allocations all
# over the place, so we must either use mimalloc as the system allocator as
# well, or override its definitions to just use the existing system allocator.
# The latter option is the least intrusive change from a stability standpoint,
# so for now that's what we do here. We can try to convert to mimalloc wholsale
# and remove this hack if we find the need to do so in the future.
set(MI_EXTRA_CPPDEFS "emmalloc_free=free;emmalloc_memalign=memalign" CACHE INTERNAL "")
endif()
add_subdirectory(extlib/mimalloc EXCLUDE_FROM_ALL)
set(MIMALLOC_INCLUDE_DIR ${CMAKE_SOURCE_DIR}/extlib/mimalloc/include)
if(NOT ENABLE_STD_TEMP_HEAP)
message(STATUS "Using in-tree mimalloc")
set(MI_OVERRIDE OFF CACHE BOOL "")
set(MI_BUILD_SHARED OFF CACHE BOOL "")
set(MI_BUILD_OBJECT OFF CACHE BOOL "")
set(MI_BUILD_TESTS OFF CACHE BOOL "")
if(EMSCRIPTEN)
# HACK: We only use mimalloc for the temporary heap, not as a general purpose
# allocator. However, the Emscripten port of mimalloc assumes that it is used
# as the system allocator, and depends on the slow emmalloc allocator.
# We do not want to enable emmalloc, as that would slow down allocations all
# over the place, so we must either use mimalloc as the system allocator as
# well, or override its definitions to just use the existing system allocator.
# The latter option is the least intrusive change from a stability standpoint,
# so for now that's what we do here. We can try to convert to mimalloc wholsale
# and remove this hack if we find the need to do so in the future.
set(MI_EXTRA_CPPDEFS "emmalloc_free=free;emmalloc_memalign=memalign" CACHE INTERNAL "")
endif()
add_subdirectory(extlib/mimalloc EXCLUDE_FROM_ALL)
set(MIMALLOC_INCLUDE_DIR ${CMAKE_SOURCE_DIR}/extlib/mimalloc/include)
endif()

if(NOT FORCE_VENDORED_Eigen3)
find_package(Eigen3 CONFIG)
Expand Down Expand Up @@ -469,6 +478,10 @@ if(WIN32)
-D_USE_MATH_DEFINES)
endif()

if(ENABLE_STD_TEMP_HEAP)
add_definitions(-DENABLE_STD_TEMP_HEAP)
endif()

if(MSVC)
# Many versions of MSVC do not have the (C99) inline keyword, instead
# they have their own __inline; this breaks `static inline` functions.
Expand Down
5 changes: 4 additions & 1 deletion src/CMakeLists.txt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,11 @@ target_link_libraries(slvs_deps INTERFACE
dxfrw
${ZLIB_LIBRARY}
${PNG_LIBRARY}
${FREETYPE_LIBRARY}
${FREETYPE_LIBRARY})
if(NOT ENABLE_STD_TEMP_HEAP)
target_link_libraries(slvs_deps INTERFACE
mimalloc-static)
endif()

if(NOT USE_QT_GUI OR ENABLE_CLI)
target_include_directories(slvs_deps SYSTEM INTERFACE ${CAIRO_INCLUDE_DIRS})
Expand Down
18 changes: 18 additions & 0 deletions src/platform/platformbase.cpp
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
#include <cstdarg>
#include <cstdio>

#ifdef ENABLE_STD_TEMP_HEAP
#include <memory_resource>
#include <strings.h>
#else
#include <mimalloc.h>
#endif

#if defined(WIN32)
# include <Windows.h>
Expand Down Expand Up @@ -71,6 +76,9 @@ void DebugPrint(const char *fmt, ...) {
// Temporary arena.
//-----------------------------------------------------------------------------

#ifdef ENABLE_STD_TEMP_HEAP
static thread_local std::pmr::monotonic_buffer_resource TempHeap;
#else
struct MimallocHeap {
mi_heap_t *heap = NULL;

Expand All @@ -81,20 +89,30 @@ struct MimallocHeap {
};

static thread_local MimallocHeap TempArena;
#endif

void *AllocTemporary(size_t size) {
#ifdef ENABLE_STD_TEMP_HEAP
void *ptr = TempHeap.allocate(size);
bzero(ptr, size);
#else
if(TempArena.heap == NULL) {
TempArena.heap = mi_heap_new();
ssassert(TempArena.heap != NULL, "out of memory");
}
void *ptr = mi_heap_zalloc(TempArena.heap, size);
#endif
ssassert(ptr != NULL, "out of memory");
return ptr;
}

void FreeAllTemporary() {
#ifdef ENABLE_STD_TEMP_HEAP
TempHeap.release();
#else
MimallocHeap temp;
std::swap(TempArena.heap, temp.heap);
#endif
}

}
Expand Down
5 changes: 4 additions & 1 deletion src/slvs/CMakeLists.txt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@ add_library(slvs-interface INTERFACE)
target_sources(slvs-interface INTERFACE lib.cpp)
target_compile_definitions(slvs-interface INTERFACE -DLIBRARY)
target_include_directories(slvs-interface INTERFACE ${CMAKE_SOURCE_DIR}/include)
target_link_libraries(slvs-interface INTERFACE slvs-solver mimalloc-static)
target_link_libraries(slvs-interface INTERFACE slvs-solver)
if(NOT ENABLE_STD_TEMP_HEAP)
target_link_libraries(slvs-interface INTERFACE mimalloc-static)
endif()

if(ENABLE_PYTHON_LIB)
add_custom_command(
Expand Down
Loading

Back | FazBrowse Home | New Git URL