Skip to content

Commit 4a4f830

Browse files
committed
feat(silo): use mimalloc as the default allocator
1 parent 1db8cf0 commit 4a4f830

4 files changed

Lines changed: 56 additions & 6 deletions

File tree

CMakeLists.txt

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,23 @@ set(CMAKE_PREFIX_PATH ${CMAKE_PREFIX_PATH} "${CMAKE_BINARY_DIR}/generators/")
2828
# Export compile commands for configuring language servers or debugging the build
2929
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
3030

31+
# ---------------------------------------------------------------------------
32+
# Allocator
33+
# ---------------------------------------------------------------------------
34+
35+
option(USE_MIMALLOC "Use mimalloc allocator" ON)
36+
37+
# Only use mimalloc in Release mode, because of conflicts with ASAN
38+
if(USE_MIMALLOC AND ${CMAKE_BUILD_TYPE} STREQUAL "Release")
39+
message("Using mimalloc as allocator")
40+
find_package(mimalloc REQUIRED)
41+
add_compile_definitions(SILO_USE_MIMALLOC=1)
42+
set(MIMALLOC_LIB mimalloc-static)
43+
else()
44+
set(MIMALLOC_LIB "")
45+
endif()
46+
47+
3148
# ---------------------------------------------------------------------------
3249
# Compile definitions
3350
# ---------------------------------------------------------------------------
@@ -113,6 +130,7 @@ add_library(silolib OBJECT ${SRC_SILO_WITHOUT_MAIN})
113130
target_link_libraries(
114131
silolib
115132
PUBLIC
133+
${MIMALLOC_LIB} # this should be first
116134
Arrow::arrow_static
117135
ArrowAcero::arrow_acero_static
118136
${Boost_LIBRARIES}

conanfile.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,10 @@ class SiloRecipe(ConanFile):
88
requires = [
99
"arrow/22.0.0",
1010
"boost/1.85.0",
11-
"poco/1.13.3",
12-
"nlohmann_json/3.12.0",
1311
"gtest/1.16.0",
12+
"mimalloc/2.2.4",
13+
"nlohmann_json/3.12.0",
14+
"poco/1.13.3",
1415
"re2/20240702",
1516
"roaring/4.2.1",
1617
"simdjson/3.12.3",
@@ -22,6 +23,7 @@ class SiloRecipe(ConanFile):
2223
default_options = {
2324
"abseil/*:shared": False,
2425

26+
"arrow/*:with_mimalloc": False,
2527
"arrow/*:compute": True,
2628
"arrow/*:acero": True,
2729

@@ -65,6 +67,9 @@ class SiloRecipe(ConanFile):
6567

6668
"hwloc/*:shared": False,
6769

70+
# this statically overrides the `malloc` symbol to use mimalloc
71+
"mimalloc/*:override": True,
72+
6873
"poco/*:shared": False,
6974
"poco/*:enable_json": True,
7075
"poco/*:enable_net": True,
@@ -104,6 +109,7 @@ def generate(self):
104109
deps.set_property("boost", "cmake_find_mode", "both")
105110
deps.set_property("gtest", "cmake_find_mode", "both")
106111
deps.set_property("hwloc", "cmake_find_mode", "both")
112+
deps.set_property("mimalloc", "cmake_find_mode", "both")
107113
deps.set_property("nlohmann_json", "cmake_find_mode", "both")
108114
deps.set_property("pcre2", "cmake_find_mode", "both")
109115
deps.set_property("poco", "cmake_find_mode", "both")

src/silo/api/memory_monitor.cpp

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22

33
#if defined(__linux__)
44

5-
#include <malloc.h>
6-
75
#include <filesystem>
86
#include <fstream>
97
#include <optional>
@@ -73,8 +71,7 @@ void MemoryMonitor::checkRssAndLimit(Poco::Timer& /*timer*/) {
7371
SPDLOG_INFO("Current memory consumption: {} KB", rss.value());
7472

7573
if (soft_memory_limit_in_kb.has_value() && rss.value() > soft_memory_limit_in_kb.value()) {
76-
SPDLOG_INFO("Manually invoking malloc_trim() to give back memory to OS.");
77-
malloc_trim(0);
74+
common::Allocator::trim();
7875
}
7976
}
8077
}

src/silo/common/allocator.h

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
#ifdef SILO_USE_MIMALLOC
2+
#include <mimalloc.h>
3+
#elif defined(__linux__)
4+
#include <malloc.h>
5+
#endif
6+
7+
#include <spdlog/spdlog.h>
8+
9+
namespace silo::common {
10+
11+
class Allocator {
12+
public:
13+
#ifdef SILO_USE_MIMALLOC
14+
static void trim() {
15+
SPDLOG_INFO("Manually invoking mi_collect(true) to give back memory to OS.");
16+
mi_collect(true);
17+
mi_collect(true); // This should be invoked twice
18+
}
19+
#elif defined(__linux__)
20+
static void trim() {
21+
SPDLOG_INFO("Manually invoking malloc_trim() to give back memory to OS.");
22+
malloc_trim(0);
23+
}
24+
#else
25+
static void trim() { SPDLOG_INFO("Allocator::trim() is not implemented for this platform."); }
26+
#endif
27+
};
28+
29+
} // namespace silo::common

0 commit comments

Comments
 (0)