Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use mimalloc for {Alloc,FreeAll}Temporary() #646

Merged
merged 2 commits into from Jul 12, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitmodules
Expand Up @@ -26,3 +26,6 @@
[submodule "extlib/q3d"]
path = extlib/q3d
url = https://github.com/q3k/q3d
[submodule "extlib/mimalloc"]
path = extlib/mimalloc
url = https://github.com/microsoft/mimalloc
2 changes: 1 addition & 1 deletion .travis/install-debian.sh
Expand Up @@ -7,4 +7,4 @@ sudo apt-get install -q -y \
libfontconfig1-dev libgtkmm-3.0-dev libpangomm-1.4-dev libgl-dev \
libgl-dev libglu-dev libspnav-dev

git submodule update --init extlib/libdxfrw extlib/flatbuffers extlib/q3d
git submodule update --init extlib/libdxfrw extlib/flatbuffers extlib/q3d extlib/mimalloc
8 changes: 8 additions & 0 deletions CMakeLists.txt
Expand Up @@ -175,6 +175,14 @@ message(STATUS "Using in-tree q3d")
add_subdirectory(extlib/q3d)
set(Q3D_INCLUDE_DIR ${CMAKE_BINARY_DIR}/extlib/q3d)

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 "")
add_subdirectory(extlib/mimalloc)
set(MIMALLOC_INCLUDE_DIR ${CMAKE_SOURCE_DIR}/extlib/mimalloc/include)

if(WIN32 OR APPLE)
# On Win32 and macOS we use vendored packages, since there is little to no benefit
# to trying to find system versions. In particular, trying to link to libraries from
Expand Down
4 changes: 2 additions & 2 deletions README.md
Expand Up @@ -83,7 +83,7 @@ Before building, check out the project and the necessary submodules:

git clone https://github.com/solvespace/solvespace
cd solvespace
git submodule update --init extlib/libdxfrw extlib/flatbuffers extlib/q3d
git submodule update --init extlib/libdxfrw extlib/flatbuffers extlib/q3d extlib/mimalloc

After that, build SolveSpace as following:

Expand Down Expand Up @@ -185,7 +185,7 @@ Before building, check out the project and the necessary submodules:

git clone https://github.com/solvespace/solvespace
cd solvespace
git submodule update --init extlib/libdxfrw extlib/flatbuffers extlib/q3d
git submodule update --init extlib/libdxfrw extlib/flatbuffers extlib/q3d extlib/mimalloc

After that, build SolveSpace as following:

Expand Down
1 change: 1 addition & 0 deletions extlib/mimalloc
Submodule mimalloc added at 07c6e6
2 changes: 1 addition & 1 deletion pkg/snap/snap/snapcraft.yaml
Expand Up @@ -45,7 +45,7 @@ parts:
snapcraftctl set-version "$version"
git describe --exact-match HEAD && grade="stable" || grade="devel"
snapcraftctl set-grade "$grade"
git submodule update --init extlib/libdxfrw extlib/flatbuffers extlib/q3d
git submodule update --init extlib/libdxfrw extlib/flatbuffers extlib/q3d extlib/mimalloc
configflags:
- -DCMAKE_INSTALL_PREFIX=/usr
- -DCMAKE_BUILD_TYPE=Release
Expand Down
15 changes: 11 additions & 4 deletions src/CMakeLists.txt
Expand Up @@ -59,7 +59,11 @@ target_include_directories(slvs
PUBLIC ${CMAKE_SOURCE_DIR}/include)

target_link_libraries(slvs
${util_LIBRARIES})
${util_LIBRARIES}
mimalloc-static)

add_dependencies(slvs
mimalloc-static)

set_target_properties(slvs PROPERTIES
PUBLIC_HEADER ${CMAKE_SOURCE_DIR}/include/slvs.h
Expand All @@ -81,7 +85,8 @@ include_directories(
${PNG_PNG_INCLUDE_DIR}
${FREETYPE_INCLUDE_DIRS}
${CAIRO_INCLUDE_DIRS}
${Q3D_INCLUDE_DIR})
${Q3D_INCLUDE_DIR}
${MIMALLOC_INCLUDE_DIR})

if(Backtrace_FOUND)
include_directories(
Expand Down Expand Up @@ -213,7 +218,8 @@ add_library(solvespace-core STATIC
${solvespace_core_SOURCES})

add_dependencies(solvespace-core
q3d_header)
q3d_header
mimalloc-static)

target_link_libraries(solvespace-core
${OpenMP_CXX_LIBRARIES}
Expand All @@ -222,7 +228,8 @@ target_link_libraries(solvespace-core
${ZLIB_LIBRARY}
${PNG_LIBRARY}
${FREETYPE_LIBRARY}
flatbuffers)
flatbuffers
mimalloc-static)

if(Backtrace_FOUND)
target_link_libraries(solvespace-core
Expand Down
67 changes: 18 additions & 49 deletions src/platform/platform.cpp
Expand Up @@ -10,6 +10,7 @@
# include <CoreFoundation/CFBundle.h>
#endif
#include "solvespace.h"
#include "mimalloc.h"
#include "config.h"
#if defined(WIN32)
// Conversely, include Microsoft headers after solvespace.h to avoid clashes.
Expand All @@ -18,7 +19,6 @@
#else
# include <unistd.h>
# include <sys/stat.h>
# include <mutex>
#endif

namespace SolveSpace {
Expand Down Expand Up @@ -680,64 +680,33 @@ void DebugPrint(const char *fmt, ...) {
#endif

//-----------------------------------------------------------------------------
// Temporary arena, on Windows.
// Temporary arena.
//-----------------------------------------------------------------------------

#if defined(WIN32)

static HANDLE TempArena = NULL;

void *AllocTemporary(size_t size)
{
if(!TempArena)
TempArena = HeapCreate(0, 0, 0);
void *ptr = HeapAlloc(TempArena, HEAP_ZERO_MEMORY, size);
ssassert(ptr != NULL, "out of memory");
return ptr;
}
struct MimallocHeap {
mi_heap_t *heap = mi_heap_new();

void FreeAllTemporary()
{
HeapDestroy(TempArena);
TempArena = NULL;
}

#endif

//-----------------------------------------------------------------------------
// Temporary arena, on Linux.
//-----------------------------------------------------------------------------

#if !defined(WIN32)
MimallocHeap() {
ssassert(heap != NULL, "out of memory");
}

struct ArenaChunk {
ArenaChunk *next;
~MimallocHeap() {
mi_heap_destroy(heap);
}
};

static std::mutex TempArenaMutex;
static ArenaChunk *TempArena = NULL;
static thread_local MimallocHeap TempArena;

void *AllocTemporary(size_t size)
{
ArenaChunk *chunk = (ArenaChunk *)calloc(1, sizeof(ArenaChunk) + size);
ssassert(chunk != NULL, "out of memory");
std::lock_guard<std::mutex> guard(TempArenaMutex);
chunk->next = TempArena;
TempArena = chunk;
return (void *)(chunk + 1);
void *AllocTemporary(size_t size) {
void *ptr = mi_heap_zalloc(TempArena.heap, size);
ssassert(ptr != NULL, "out of memory");
return ptr;
}

void FreeAllTemporary()
{
std::lock_guard<std::mutex> guard(TempArenaMutex);
while(TempArena) {
ArenaChunk *chunk = TempArena;
TempArena = TempArena->next;
free(chunk);
}
void FreeAllTemporary() {
MimallocHeap temp;
std::swap(TempArena.heap, temp.heap);
}

#endif

}
}