From patchwork Sun Jul 7 05:09:49 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: k4lizen X-Patchwork-Id: 1957650 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; dkim=pass (2048-bit key; secure) header.d=proton.me header.i=@proton.me header.a=rsa-sha256 header.s=a6n5gxupq5edffbhgcht7yzmoa.protonmail header.b=jGU5l6vk; dkim-atps=neutral Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=sourceware.org (client-ip=2620:52:3:1:0:246e:9693:128c; helo=server2.sourceware.org; envelope-from=libc-alpha-bounces~incoming=patchwork.ozlabs.org@sourceware.org; receiver=patchwork.ozlabs.org) Received: from server2.sourceware.org (server2.sourceware.org [IPv6:2620:52:3:1:0:246e:9693:128c]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (secp384r1) server-digest SHA384) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4WGwLF4MPfz1xrJ for ; Sun, 7 Jul 2024 15:10:13 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 6ED89388A029 for ; Sun, 7 Jul 2024 05:10:10 +0000 (GMT) X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail-43167.protonmail.ch (mail-43167.protonmail.ch [185.70.43.167]) by sourceware.org (Postfix) with ESMTPS id 53C53385B505 for ; Sun, 7 Jul 2024 05:09:55 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 53C53385B505 Authentication-Results: sourceware.org; dmarc=pass (p=quarantine dis=none) header.from=proton.me Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=proton.me ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 53C53385B505 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=185.70.43.167 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1720328997; cv=none; b=hmUjujJvNmCiLxwp7kBgWsS/vYEjGZoxcSDZeIkUqLJ0krn7L/2uS4pYiVzC0VEl+CTTJLf0kUAAcpei+xPR4YGYnzN40Rt/djX3dN1yCIjX2B1FBqonJ+sN76eUnAqrZGDM7o2jT7VBMjjAATVm3MXBc0DRZgLATGIh8f0Rgj8= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1720328997; c=relaxed/simple; bh=0qipIRiYjQ0A+RUZghmWJSPc0sD4Kc9EpXyhbKOaTZY=; h=DKIM-Signature:Date:To:From:Subject:Message-ID:MIME-Version; b=tLhF4IfJRLpOJfhk+Py1MIrFiiFmwb31ySgaSZCekdeCC0lztCgsksbMXKJFsLBDzE9o8R2BSx22ZKDY91bQeyfNkIVXzDG4yQzS9y7t7/yv9TShSC2McN2ZeROtWtn+mPY3CURkksrkQPhyiOWi+zTIEQ/YpAyTnNq0Tf9IRN4= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=proton.me; s=a6n5gxupq5edffbhgcht7yzmoa.protonmail; t=1720328992; x=1720588192; bh=9+qavibxTfR81v6DsykLqG6AwaoFABy1ucQG0szLM1g=; h=Date:To:From:Subject:Message-ID:Feedback-ID:From:To:Cc:Date: Subject:Reply-To:Feedback-ID:Message-ID:BIMI-Selector; b=jGU5l6vkr9WKSZWrSQVnzBBqDEjsUhwoHQDa3hVEQl2D8fQFweiQOLIZqBUEJwopK Tn0pMbOLM4Marxk2tgGt9kAwQ7S8bAoYMvgslkTROvmJ7AlNbUmZTFJsFkRMC/0RNA NZY7NKT6A99ZeXqv3Dt6UxTkMjWDRyk3+1orQKBEUr794EFVVuFfqP15gSndNDV84C FMdIk2m17BbRiD3q+pu7Sr94K6fCqMSI9HHNaX7J7c+cYVeRPdVPYrdMAjIlcBa1eb tuCdGNeVFgLE3/O0wYPYs6Zg8YvE+BVTsL5Yc4KWVGTUq/N8Qs+hWZ6hvcuAJ0t9+m 9i7mk5qrNVp+g== Date: Sun, 07 Jul 2024 05:09:49 +0000 To: "libc-alpha@sourceware.org" From: k4lizen Subject: [PATCH] malloc: send freed small chunks to smallbin Message-ID: Feedback-ID: 102038742:user:proton X-Pm-Message-ID: 6221cd7ca1513109effbf6320f6cc9740285be1a MIME-Version: 1.0 X-Spam-Status: No, score=-9.4 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, KAM_INFOUSMEBIZ, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H4, RCVD_IN_MSPIKE_WL, SPF_HELO_PASS, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: libc-alpha@sourceware.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Libc-alpha mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: libc-alpha-bounces~incoming=patchwork.ozlabs.org@sourceware.org Large chunks get added to the unsorted bin since sorting them takes time, for small chunks the benefit of adding them to the unsorted bin is non-existant, actually hurting performance. Splitting and malloc_consolidate still add small chunks to unsorted, but we can hint the compiler that that is a relatively rare occurance. Benchmarking shows this to be consistently good. --- malloc/malloc.c | 51 ++++++++++++++++++++++++++++++++----------------- 1 file changed, 34 insertions(+), 17 deletions(-) diff --git a/malloc/malloc.c b/malloc/malloc.c index bcb6e5b83c..14b0866194 100644 --- a/malloc/malloc.c +++ b/malloc/malloc.c @@ -4156,9 +4156,9 @@ _int_malloc (mstate av, size_t bytes) #endif } - /* place chunk in bin */ - - if (in_smallbin_range (size)) + /* Place chunk in bin. Only malloc_consolidate() and splitting can put + small chunks into the unsorted bin. */ + if (__glibc_unlikely (in_smallbin_range (size))) { victim_index = smallbin_index (size); bck = bin_at (av, victim_index); @@ -4723,23 +4723,41 @@ _int_free_create_chunk (mstate av, mchunkptr p, INTERNAL_SIZE_T size, } else clear_inuse_bit_at_offset(nextchunk, 0); + mchunkptr bck, fwd; + + if(in_smallbin_range (size)){ /* - Place the chunk in unsorted chunk list. Chunks are - not placed into regular bins until after they have - been given one chance to be used in malloc. + Place small chunks directly in their smallbin, so they + don't pollute the unsorted bin. */ - mchunkptr bck = unsorted_chunks (av); - mchunkptr fwd = bck->fd; - if (__glibc_unlikely (fwd->bk != bck)) - malloc_printerr ("free(): corrupted unsorted chunks"); - p->fd = fwd; + int chunk_index = smallbin_index (size); + bck = bin_at (av, chunk_index); + fwd = bck->fd; + + if (__glibc_unlikely (fwd->bk != bck)) + malloc_printerr ("free(): chunks in smallbin corrupted"); + + mark_bin (av, chunk_index); + } + else + { + /* + Place large chunks in unsorted chunk list. Large chunks are + not placed into regular bins until after they have + been given one chance to be used in malloc. + */ + + bck = unsorted_chunks (av); + fwd = bck->fd; + if (__glibc_unlikely (fwd->bk != bck)) + malloc_printerr ("free(): corrupted unsorted chunks"); + p->fd_nextsize = NULL; + p->bk_nextsize = NULL; + } + p->bk = bck; - if (!in_smallbin_range(size)) - { - p->fd_nextsize = NULL; - p->bk_nextsize = NULL; - } + p->fd = fwd; bck->fd = p; fwd->bk = p; @@ -4748,7 +4766,6 @@ _int_free_create_chunk (mstate av, mchunkptr p, INTERNAL_SIZE_T size, check_free_chunk(av, p); } - else { /* If the chunk borders the current high end of memory,