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 4BF9F1F44D for ; Mon, 1 Apr 2024 10:15:07 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=80x24.org; s=selector1; t=1711966507; bh=R2GsSLBWzqXuew3MQs7eT/2Gn6MK5aWSuVpyAHNGsQc=; h=From:To:Subject:Date:From; b=lOb2uLGhuzWMDxq5/FE2N9bImtUrWHYJLAx2vJv/QJV0X0TLiLjEB0eN8kpR9U8nQ q5k64YHVqlHwX3oKAnTQeb3iVDGzyBUBDmpEVA6XHgYewzLjCj4Fxzk1P59RTomHDe Dp448zV2cxapq3zjtfLJqlD77RIzgKyq8lno7F4E= From: EW To: spew@80x24.org Subject: [PATCH] malloc: jemalloc size align Date: Mon, 1 Apr 2024 10:15:07 +0000 Message-ID: <20240401101507.18448-1-e@80x24.org> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit List-Id: --- malloc/malloc.c | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/malloc/malloc.c b/malloc/malloc.c index f7cd29bc..6e0b066d 100644 --- a/malloc/malloc.c +++ b/malloc/malloc.c @@ -3018,6 +3018,31 @@ tcache_thread_shutdown (void) #endif /* !USE_TCACHE */ +static inline size_t +size_class_pad (size_t bytes) +{ + if (bytes <= MAX_FAST_SIZE || bytes >= DEFAULT_MMAP_THRESHOLD_MAX) + return bytes; + /* + * Use jemalloc-inspired size classes for mid-size allocations to + * minimize fragmentation. This means we pay a 0-20% overhead on + * the initial allocations to improve the likelyhood of reuse. + */ + size_t max = sizeof(void *) << 4; + size_t nxt; + + do { + if (bytes <= max) { + size_t sc_bytes = ALIGN_UP (bytes, max >> 3); + + return sc_bytes <= DEFAULT_MMAP_THRESHOLD_MAX ? sc_bytes : bytes; + } + nxt = max << 1; + } while (nxt > max && nxt < DEFAULT_MMAP_THRESHOLD_MAX && (max = nxt)); + + return bytes; +} + void * __libc_malloc (size_t bytes) { @@ -3031,6 +3056,7 @@ __libc_malloc (size_t bytes) = atomic_forced_read (__malloc_hook); if (__builtin_expect (hook != NULL, 0)) return (*hook)(bytes, RETURN_ADDRESS (0)); + bytes = size_class_pad (bytes); #if USE_TCACHE /* int_free also calls request2size, be careful to not pad twice. */ size_t tbytes; @@ -3150,6 +3176,8 @@ __libc_realloc (void *oldmem, size_t bytes) if (oldmem == 0) return __libc_malloc (bytes); + bytes = size_class_pad (bytes); + /* chunk corresponding to oldmem */ const mchunkptr oldp = mem2chunk (oldmem); /* its size */ @@ -3391,6 +3419,7 @@ __libc_calloc (size_t n, size_t elem_size) return memset (mem, 0, sz); } + sz = size_class_pad (sz); MAYBE_INIT_TCACHE (); if (SINGLE_THREAD_P)