Skip to content

Commit 6a53c52

Browse files
committed
EXPERIMENT: [C++] Access mimalloc through dynamically-resolved symbols
1 parent cd607d0 commit 6a53c52

6 files changed

+41
-21
lines changed

cpp/src/arrow/CMakeLists.txt

+8
Original file line numberDiff line numberDiff line change
@@ -428,7 +428,15 @@ if(ARROW_JEMALLOC)
428428
PROPERTIES SKIP_PRECOMPILE_HEADERS ON
429429
SKIP_UNITY_BUILD_INCLUSION ON)
430430
endif()
431+
if(ARROW_MIMALLOC)
432+
list(APPEND ARROW_MEMORY_POOL_SRCS memory_pool_mimalloc.cc)
433+
set_source_files_properties(memory_pool_mimalloc.cc
434+
PROPERTIES SKIP_PRECOMPILE_HEADERS ON
435+
SKIP_UNITY_BUILD_INCLUSION ON)
436+
endif()
437+
431438
arrow_add_object_library(ARROW_MEMORY_POOL ${ARROW_MEMORY_POOL_SRCS})
439+
432440
if(ARROW_JEMALLOC)
433441
foreach(ARROW_MEMORY_POOL_TARGET ${ARROW_MEMORY_POOL_TARGETS})
434442
target_link_libraries(${ARROW_MEMORY_POOL_TARGET} PRIVATE jemalloc::jemalloc)

cpp/src/arrow/memory_pool.cc

+7-12
Original file line numberDiff line numberDiff line change
@@ -54,16 +54,11 @@
5454
#endif
5555

5656
namespace arrow {
57-
58-
namespace memory_pool {
59-
60-
namespace internal {
57+
namespace memory_pool::internal {
6158

6259
alignas(kDefaultBufferAlignment) int64_t zero_size_area[1] = {kDebugXorSuffix};
6360

64-
} // namespace internal
65-
66-
} // namespace memory_pool
61+
} // namespace memory_pool::internal
6762

6863
namespace {
6964

@@ -394,15 +389,15 @@ class MimallocAllocator {
394389
*out = memory_pool::internal::kZeroSizeArea;
395390
return Status::OK();
396391
}
397-
*out = reinterpret_cast<uint8_t*>(
398-
mi_malloc_aligned(static_cast<size_t>(size), static_cast<size_t>(alignment)));
392+
*out = reinterpret_cast<uint8_t*>(arrow_mi_malloc_aligned(
393+
static_cast<size_t>(size), static_cast<size_t>(alignment)));
399394
if (*out == NULL) {
400395
return Status::OutOfMemory("malloc of size ", size, " failed");
401396
}
402397
return Status::OK();
403398
}
404399

405-
static void ReleaseUnused() { mi_collect(true); }
400+
static void ReleaseUnused() { arrow_mi_collect(true); }
406401

407402
static Status ReallocateAligned(int64_t old_size, int64_t new_size, int64_t alignment,
408403
uint8_t** ptr) {
@@ -417,7 +412,7 @@ class MimallocAllocator {
417412
return Status::OK();
418413
}
419414
*ptr = reinterpret_cast<uint8_t*>(
420-
mi_realloc_aligned(previous_ptr, static_cast<size_t>(new_size), alignment));
415+
arrow_mi_realloc_aligned(previous_ptr, static_cast<size_t>(new_size), alignment));
421416
if (*ptr == NULL) {
422417
*ptr = previous_ptr;
423418
return Status::OutOfMemory("realloc of size ", new_size, " failed");
@@ -429,7 +424,7 @@ class MimallocAllocator {
429424
if (ptr == memory_pool::internal::kZeroSizeArea) {
430425
DCHECK_EQ(size, 0);
431426
} else {
432-
mi_free(ptr);
427+
arrow_mi_free(ptr);
433428
}
434429
}
435430
};

cpp/src/arrow/memory_pool_benchmark.cc

+1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
#include "arrow/memory_pool.h"
1919
#include "arrow/result.h"
20+
#include "arrow/util/config.h"
2021
#include "arrow/util/logging.h"
2122

2223
#include "benchmark/benchmark.h"

cpp/src/arrow/memory_pool_internal.h

+13-8
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,9 @@
1919

2020
#include "arrow/memory_pool.h"
2121
#include "arrow/util/config.h"
22+
#include "arrow/util/macros.h"
2223

23-
namespace arrow {
24-
25-
namespace memory_pool {
26-
27-
namespace internal {
24+
namespace arrow::memory_pool::internal {
2825

2926
static constexpr int64_t kDebugXorSuffix = -0x181fe80e0b464188LL;
3027

@@ -48,8 +45,16 @@ class JemallocAllocator {
4845

4946
#endif // defined(ARROW_JEMALLOC)
5047

51-
} // namespace internal
48+
} // namespace arrow::memory_pool::internal
49+
50+
#ifdef ARROW_MIMALLOC
51+
52+
extern "C" {
5253

53-
} // namespace memory_pool
54+
ARROW_NOINLINE void* arrow_mi_malloc_aligned(size_t size, size_t alignment);
55+
ARROW_NOINLINE void* arrow_mi_realloc_aligned(void* p, size_t new_size, size_t alignment);
56+
ARROW_NOINLINE void arrow_mi_free(void* p);
57+
ARROW_NOINLINE void arrow_mi_collect(bool force);
58+
}
5459

55-
} // namespace arrow
60+
#endif // defined(ARROW_MIMALLOC)

cpp/src/arrow/memory_pool_test.cc

+4-1
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,10 @@ TYPED_TEST_P(TestMemoryPool, Reallocate) { this->TestReallocate(); }
7777

7878
TYPED_TEST_P(TestMemoryPool, Alignment) { this->TestAlignment(); }
7979

80-
REGISTER_TYPED_TEST_SUITE_P(TestMemoryPool, MemoryTracking, OOM, Reallocate, Alignment);
80+
TYPED_TEST_P(TestMemoryPool, ReleaseUnused) { this->TestReleaseUnused(); }
81+
82+
REGISTER_TYPED_TEST_SUITE_P(TestMemoryPool, MemoryTracking, OOM, Reallocate, Alignment,
83+
ReleaseUnused);
8184

8285
INSTANTIATE_TYPED_TEST_SUITE_P(Default, TestMemoryPool, DefaultMemoryPoolFactory);
8386
INSTANTIATE_TYPED_TEST_SUITE_P(System, TestMemoryPool, SystemMemoryPoolFactory);

cpp/src/arrow/memory_pool_test.h

+8
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,14 @@ class TestMemoryPoolBase : public ::testing::Test {
106106
pool->Free(data512, 10, 512);
107107
}
108108
}
109+
110+
void TestReleaseUnused() {
111+
auto pool = memory_pool();
112+
const int64_t nbytes = pool->bytes_allocated();
113+
pool->ReleaseUnused();
114+
// Unfortunately there's not much that we can assert here
115+
ASSERT_EQ(nbytes, pool->bytes_allocated());
116+
}
109117
};
110118

111119
} // namespace arrow

0 commit comments

Comments
 (0)