From a0a6ad3cf9568ca75269e51b43f60bdc0029f86b Mon Sep 17 00:00:00 2001 From: Daan Leijen Date: Fri, 3 Jan 2025 08:17:32 -0800 Subject: [PATCH 1/2] add thread_local for c++ --- include/mimalloc/internal.h | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/include/mimalloc/internal.h b/include/mimalloc/internal.h index df358e39..4a798d0a 100644 --- a/include/mimalloc/internal.h +++ b/include/mimalloc/internal.h @@ -38,6 +38,12 @@ terms of the MIT license. A copy of the license can be found in the file #define mi_decl_cache_align __attribute__((aligned(MI_CACHE_LINE))) #define mi_decl_weak __attribute__((weak)) #define mi_decl_hidden __attribute__((visibility("hidden"))) +#elif __cplusplus >= 201103L // c++11 +#define mi_decl_noinline +#define mi_decl_thread thread_local +#define mi_decl_cache_align alignas(MI_CACHE_LINE) +#define mi_decl_weak +#define mi_decl_hidden #else #define mi_decl_noinline #define mi_decl_thread __thread // hope for the best :-) From 2a75500ac2a43fc52c394181579347c7cb336965 Mon Sep 17 00:00:00 2001 From: Daan Leijen Date: Fri, 3 Jan 2025 08:38:36 -0800 Subject: [PATCH 2/2] disable large pages by default --- include/mimalloc/types.h | 15 +++++++++++++-- src/arena.c | 6 +++--- 2 files changed, 16 insertions(+), 5 deletions(-) diff --git a/include/mimalloc/types.h b/include/mimalloc/types.h index c5029a14..9fefdf60 100644 --- a/include/mimalloc/types.h +++ b/include/mimalloc/types.h @@ -99,6 +99,10 @@ terms of the MIT license. A copy of the license can be found in the file #define MI_ENCODE_FREELIST 1 #endif +// Enable large pages for objects between 128KiB and 512KiB. Disabled by default. +#ifndef MI_ENABLE_LARGE_PAGES +#define MI_ENABLE_LARGE_PAGES 0 +#endif // -------------------------------------------------------------- // Sizes of internal data-structures @@ -131,6 +135,7 @@ terms of the MIT license. A copy of the license can be found in the file #define MI_MEDIUM_PAGE_SIZE (8*MI_SMALL_PAGE_SIZE) // 512 KiB (=byte in the bchunk bitmap) #define MI_LARGE_PAGE_SIZE (MI_SIZE_SIZE*MI_MEDIUM_PAGE_SIZE) // 4 MiB (=word in the bchunk bitmap) + // Maximum number of size classes. (spaced exponentially in 12.5% increments) #define MI_BIN_HUGE (73U) #define MI_BIN_FULL (MI_BIN_HUGE+1) @@ -328,8 +333,14 @@ typedef struct mi_page_s { // The max object size are checked to not waste more than 12.5% internally over the page sizes. // (Except for large pages since huge objects are allocated in 4MiB chunks) #define MI_SMALL_MAX_OBJ_SIZE ((MI_SMALL_PAGE_SIZE-MI_PAGE_INFO_SIZE)/8) // < 8 KiB -#define MI_MEDIUM_MAX_OBJ_SIZE ((MI_MEDIUM_PAGE_SIZE-MI_PAGE_INFO_SIZE)/8) // < 64 KiB -#define MI_LARGE_MAX_OBJ_SIZE (MI_LARGE_PAGE_SIZE/4) // <= 512 KiB // note: this must be a nice power of 2 or we get rounding issues with `_mi_bin` + +#if MI_ENABLE_LARGE_PAGES +#define MI_MEDIUM_MAX_OBJ_SIZE (MI_MEDIUM_PAGE_SIZE-MI_PAGE_INFO_SIZE)/8) // < 64 KiB +#define MI_LARGE_MAX_OBJ_SIZE (MI_LARGE_PAGE_SIZE/4) // <= 512 KiB // note: this must be a nice power of 2 or we get rounding issues with `_mi_bin` +#else +#define MI_MEDIUM_MAX_OBJ_SIZE (MI_MEDIUM_PAGE_SIZE/8) // <= 64 KiB +#define MI_LARGE_MAX_OBJ_SIZE MI_MEDIUM_MAX_OBJ_SIZE +#endif #define MI_LARGE_MAX_OBJ_WSIZE (MI_LARGE_MAX_OBJ_SIZE/MI_SIZE_SIZE) diff --git a/src/arena.c b/src/arena.c index 60046cdc..cf1836f7 100644 --- a/src/arena.c +++ b/src/arena.c @@ -773,9 +773,9 @@ mi_page_t* _mi_arenas_page_alloc(mi_heap_t* heap, size_t block_size, size_t bloc else if (block_size <= MI_MEDIUM_MAX_OBJ_SIZE) { page = mi_arenas_page_regular_alloc(heap, mi_slice_count_of_size(MI_MEDIUM_PAGE_SIZE), block_size); } - //else if (block_size <= MI_LARGE_MAX_OBJ_SIZE) { - // page = mi_arenas_page_regular_alloc(heap, mi_slice_count_of_size(MI_LARGE_PAGE_SIZE), block_size); - // } + else if (block_size <= MI_LARGE_MAX_OBJ_SIZE) { + page = mi_arenas_page_regular_alloc(heap, mi_slice_count_of_size(MI_LARGE_PAGE_SIZE), block_size); + } else { page = mi_arenas_page_singleton_alloc(heap, block_size, block_alignment); }