From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on dcvr.yhbt.net X-Spam-Level: X-Spam-ASN: X-Spam-Status: No, score=-4.2 required=3.0 tests=ALL_TRUSTED,AWL,BAYES_00, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF shortcircuit=no autolearn=ham autolearn_force=no version=3.4.6 Received: from localhost (dcvr.yhbt.net [127.0.0.1]) by dcvr.yhbt.net (Postfix) with ESMTP id A1A791F51A for ; Wed, 3 Apr 2024 21:42:22 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=80x24.org; s=selector1; t=1712180542; bh=xBFVpfFEkUz6ll2mvVREHom4VbC+dF5XeoDcr91rYy4=; h=From:To:Subject:Date:In-Reply-To:References:From; b=oHImUho6mFosAPV1ZvnI6D/Lja2vFormyyDreObGEaTNKS/zDsS+Pr2D8BLwC07st Bb8cYXWosDBKNsMY0hdDhEELgLW7Vr2lx0F7VyfUX3KTqaWs+/gSHO3QzeB6r1s49U augGhvffeZ8ftl2roI04cegl6bkzXBFv78CvvHxE= From: Eric Wong To: mwrap-perl@80x24.org Subject: [PATCH 2/3] implement dynamic mmap threshold Date: Wed, 3 Apr 2024 21:42:21 +0000 Message-ID: <20240403214222.3258695-3-e@80x24.org> In-Reply-To: <20240403214222.3258695-1-e@80x24.org> References: <20240403214222.3258695-1-e@80x24.org> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit List-Id: mmap is an expensive operation, so follow glibc's lead and adjust the mmap threshold dynamically. Unlike glibc, there's no way to disable this at the moment. I don't think disabling is worth the effort since avoiding mmap is the default in glibc and few users change it. Furthermore, avoiding mmap lets us test the effectiveness of the prior size classes commit at reducing fragmentation. --- dlmalloc_c.h | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/dlmalloc_c.h b/dlmalloc_c.h index e842a53..348c194 100644 --- a/dlmalloc_c.h +++ b/dlmalloc_c.h @@ -5623,6 +5623,8 @@ void* mspace_malloc(mspace msp, size_t bytes) { } void mspace_free(mspace msp, void* mem) { + const size_t mm_thresh_max = 1 << (sizeof(void *) == 4 ? 19U : 25U); + if (mem != 0) { mchunkptr p = mem2chunk(mem); #if FOOTERS && 0 /* mwrap called get_mstate_for */ @@ -5643,7 +5645,15 @@ void mspace_free(mspace msp, void* mem) { if (!pinuse(p)) { size_t prevsize = p->prev_foot; if (is_mmapped(p)) { + size_t asize = psize; + psize += prevsize + MMAP_FOOT_PAD; + + /* adjust dynamic mmap threshold, similar to glibc */ + if (asize > mparams.mmap_threshold && asize <= mm_thresh_max) { + mparams.mmap_threshold = asize; + mparams.trim_threshold = asize * 2; + } if (CALL_MUNMAP((char*)p - prevsize, psize) == 0) fm->footprint -= psize; goto postaction;