From f9ac60a90ffcaacf576920508431d6fa7f3c92e4 Mon Sep 17 00:00:00 2001 From: daan Date: Fri, 12 Nov 2021 19:03:20 -0800 Subject: [PATCH] suppres eager commit delay for the first 4 threads --- include/mimalloc-internal.h | 1 + src/init.c | 8 ++++++++ src/segment.c | 2 +- 3 files changed, 10 insertions(+), 1 deletion(-) diff --git a/include/mimalloc-internal.h b/include/mimalloc-internal.h index 5c716a79..a2549f7e 100644 --- a/include/mimalloc-internal.h +++ b/include/mimalloc-internal.h @@ -63,6 +63,7 @@ static inline uintptr_t _mi_random_shuffle(uintptr_t x); extern mi_decl_cache_align mi_stats_t _mi_stats_main; extern mi_decl_cache_align const mi_page_t _mi_page_empty; bool _mi_is_main_thread(void); +size_t _mi_current_thread_count(void); bool _mi_preloading(void); // true while the C runtime is not ready // os.c diff --git a/src/init.c b/src/init.c index cb9d123f..db3d24ba 100644 --- a/src/init.c +++ b/src/init.c @@ -332,6 +332,12 @@ bool _mi_is_main_thread(void) { return (_mi_heap_main.thread_id==0 || _mi_heap_main.thread_id == _mi_thread_id()); } +static _Atomic(uintptr_t) thread_count = ATOMIC_VAR_INIT(1); + +size_t _mi_current_thread_count(void) { + return mi_atomic_load_relaxed(&thread_count); +} + // This is called from the `mi_malloc_generic` void mi_thread_init(void) mi_attr_noexcept { @@ -344,6 +350,7 @@ void mi_thread_init(void) mi_attr_noexcept if (_mi_heap_init()) return; // returns true if already initialized _mi_stat_increase(&_mi_stats_main.threads, 1); + mi_atomic_increment_relaxed(&thread_count); //_mi_verbose_message("thread init: 0x%zx\n", _mi_thread_id()); } @@ -352,6 +359,7 @@ void mi_thread_done(void) mi_attr_noexcept { } static void _mi_thread_done(mi_heap_t* heap) { + mi_atomic_decrement_relaxed(&thread_count); _mi_stat_decrease(&_mi_stats_main.threads, 1); // check thread-id as on Windows shutdown with FLS the main (exit) thread may call this on thread-local heaps... diff --git a/src/segment.c b/src/segment.c index 4c2fa32e..1fffd745 100644 --- a/src/segment.c +++ b/src/segment.c @@ -579,7 +579,7 @@ static mi_segment_t* mi_segment_init(mi_segment_t* segment, size_t required, mi_ mi_assert_internal(segment_size >= required); // Initialize parameters - const bool eager_delayed = (page_kind <= MI_PAGE_MEDIUM && !_mi_is_main_thread() && + const bool eager_delayed = (page_kind <= MI_PAGE_MEDIUM && _mi_current_thread_count() > 4 && tld->count < (size_t)mi_option_get(mi_option_eager_commit_delay)); const bool eager = !eager_delayed && mi_option_is_enabled(mi_option_eager_commit); bool commit = eager; // || (page_kind >= MI_PAGE_LARGE);