From d81b800e126650595bff34b5e208802e8ac645b0 Mon Sep 17 00:00:00 2001 From: daan Date: Tue, 20 Aug 2019 08:58:53 -0700 Subject: [PATCH 1/2] optimize stat counter increase --- src/stats.c | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/src/stats.c b/src/stats.c index e7d398b2..39015f94 100644 --- a/src/stats.c +++ b/src/stats.c @@ -28,11 +28,14 @@ void _mi_stats_done(mi_stats_t* stats) { Statistics operations ----------------------------------------------------------- */ +static bool mi_is_in_main(void* stat) { + return ((uint8_t*)stat >= (uint8_t*)&_mi_stats_main + && (uint8_t*)stat < ((uint8_t*)&_mi_stats_main + sizeof(mi_stats_t))); +} + static void mi_stat_update(mi_stat_count_t* stat, int64_t amount) { if (amount == 0) return; - bool in_main = ((uint8_t*)stat >= (uint8_t*)&_mi_stats_main - && (uint8_t*)stat < ((uint8_t*)&_mi_stats_main + sizeof(mi_stats_t))); - if (in_main) + if (mi_is_in_main(stat)) { // add atomically (for abandoned pages) int64_t current = mi_atomic_add(&stat->current,amount); @@ -58,11 +61,16 @@ static void mi_stat_update(mi_stat_count_t* stat, int64_t amount) { } void _mi_stat_counter_increase(mi_stat_counter_t* stat, size_t amount) { - mi_atomic_add( &stat->count, 1 ); - mi_atomic_add( &stat->total, (int64_t)amount ); + if (mi_is_in_main(stat)) { + mi_atomic_add( &stat->count, 1 ); + mi_atomic_add( &stat->total, (int64_t)amount ); + } + else { + stat->count++; + stat->total += amount; + } } - void _mi_stat_increase(mi_stat_count_t* stat, size_t amount) { mi_stat_update(stat, (int64_t)amount); } From ff88361329d16dc629c61abe63a285e5c977ce80 Mon Sep 17 00:00:00 2001 From: daan Date: Tue, 20 Aug 2019 09:45:50 -0700 Subject: [PATCH 2/2] lower block size for keeping retired pages --- src/page.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/page.c b/src/page.c index 41e41b89..549ced38 100644 --- a/src/page.c +++ b/src/page.c @@ -71,7 +71,7 @@ static bool mi_page_is_valid_init(mi_page_t* page) { mi_assert_internal(page->block_size > 0); mi_assert_internal(page->used <= page->capacity); mi_assert_internal(page->capacity <= page->reserved); - + mi_segment_t* segment = _mi_page_segment(page); uint8_t* start = _mi_page_start(segment,page,NULL); mi_assert_internal(start == _mi_segment_page_start(segment,page,page->block_size,NULL)); @@ -390,7 +390,7 @@ void _mi_page_retire(mi_page_t* page) { // is the only page left with free blocks. It is not clear // how to check this efficiently though... for now we just check // if its neighbours are almost fully used. - if (mi_likely(page->block_size <= MI_MEDIUM_OBJ_SIZE_MAX)) { + if (mi_likely(page->block_size <= MI_SMALL_SIZE_MAX)) { if (mi_page_mostly_used(page->prev) && mi_page_mostly_used(page->next)) { _mi_stat_counter_increase(&_mi_stats_main.page_no_retire,1); return; // dont't retire after all @@ -734,10 +734,10 @@ void* _mi_malloc_generic(mi_heap_t* heap, size_t size) mi_attr_noexcept // call potential deferred free routines _mi_deferred_free(heap, false); - + // free delayed frees from other threads _mi_heap_delayed_free(heap); - + // huge allocation? mi_page_t* page; if (mi_unlikely(size > MI_LARGE_OBJ_SIZE_MAX)) {