From patchwork Wed Jul 3 08:25:28 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: k4lizen X-Patchwork-Id: 1956026 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=protonmail header.b=NyTtRBJy; dkim-atps=neutral Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=sourceware.org (client-ip=8.43.85.97; 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 [8.43.85.97]) (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 4WDXtn0MHcz1xqh for ; Wed, 3 Jul 2024 18:26:41 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 410EF3860C36 for ; Wed, 3 Jul 2024 08:26:39 +0000 (GMT) X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail-43166.protonmail.ch (mail-43166.protonmail.ch [185.70.43.166]) by sourceware.org (Postfix) with ESMTPS id E6231386075A for ; Wed, 3 Jul 2024 08:25:35 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org E6231386075A 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 E6231386075A Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=185.70.43.166 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1719995141; cv=none; b=e4bzcaTtQ7D7jAU84NSVxUwvYfVdRXTUpkx65lvVCiSBRh20E8muxyCF/R8Tas3qF+xQOWPb5Wj+ozR25S/ci85m+3Keh5N4gguOMLvP+6BdzBhe8Tb8lprrkPXcRGlftevpZyoToFWfLER04EsbGE2qSPeN+Xb9Xopbxgth84E= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1719995141; c=relaxed/simple; bh=kfjpZy6XdmWt15L9HqOJ8MPoOYpi909MCCrKqwd30/4=; h=DKIM-Signature:Date:To:From:Subject:Message-ID:MIME-Version; b=TBsIooooS8djhlYCi9ZrSmwePjzMQo7arpEyuRinEnvwVLJWsMc8iIEzXe5eHFaU8sLFVUF3EDJ+DgnbtoHAXKTNNnSSbu+l6bsZCPdOdwVhfdzF5AvCKXRo4KMuX8bYT6uJoNkqXYm+PvyZkYQHzPhYKBQlyMU29xmAcKEkr8w= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=proton.me; s=protonmail; t=1719995133; x=1720254333; bh=kfjpZy6XdmWt15L9HqOJ8MPoOYpi909MCCrKqwd30/4=; h=Date:To:From:Cc:Subject:Message-ID:Feedback-ID:From:To:Cc:Date: Subject:Reply-To:Feedback-ID:Message-ID:BIMI-Selector; b=NyTtRBJytqVAfRuZi3vy/CxnQRmwLX/LgwVbxvMJ6mYmVbbLadeJkYHWbYrKExrQZ NIDBUQ3rtZNZELp4ZmWXRbruWX/8cfOrNirDfyJMx4tDXFxUGD50XLjFXSl+8HD2KK UD5ZONYdpZQUtHFgpRNPl+XU2sI+yk0UhuUdHJ+s4qjtcjpRWbAwM08eBI4TAOaPCv LcLaMXKxxkALw1lpwCrAYmEw5fCE8dytA3kwA6TQXtsA2A/TQ/6IcSmWKKtMQTRqUQ 2kEnBge1HB44iQfjwaL7pi5EMt7X8yEKtN11HwKNl36NElYmJLfec0SNeDoPZUPDe2 xuLx8mUDkLB7A== Date: Wed, 03 Jul 2024 08:25:28 +0000 To: "libc-alpha@sourceware.org" From: k4lizen Cc: "fweimer@redhat.com" , DJ Delorie Subject: [PATCH] malloc: send freed small chunks to smallbin Message-ID: Feedback-ID: 102038742:user:proton X-Pm-Message-ID: 8eb3603b773fdb5d4617a0a024b7d9108b03abf3 MIME-Version: 1.0 X-Spam-Status: No, score=-9.6 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, HTML_MESSAGE, 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. See discussion: https://sourceware.org/pipermail/libc-alpha/2024-July/157922.html --- malloc/malloc.c | 49 +++++++++++++++++++++++++++++++++---------------- 1 file changed, 33 insertions(+), 16 deletions(-) -- 2.45.2 From 39bbbafd2cbc926864486a6e58876b7b000940ea Mon Sep 17 00:00:00 2001 From: k4lizen <124312252+k4lizen@users.noreply.github.com> Date: Wed, 3 Jul 2024 06:01:51 +0200 Subject: [PATCH] malloc: send freed small chunks to smallbin To: libc-alpha@sourceware.org Cc: fweimer@redhat.com, dj@redhat.com 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. See discussion: https://sourceware.org/pipermail/libc-alpha/2024-July/157922.html --- malloc/malloc.c | 49 +++++++++++++++++++++++++++++++++---------------- 1 file changed, 33 insertions(+), 16 deletions(-) diff --git a/malloc/malloc.c b/malloc/malloc.c index bcb6e5b83c..8c6155d8f3 100644 --- a/malloc/malloc.c +++ b/malloc/malloc.c @@ -4156,8 +4156,8 @@ _int_malloc (mstate av, size_t bytes) #endif } - /* place chunk in bin */ - + /* Place chunk in bin. malloc_consolidate() could create + small chunks. */ if (in_smallbin_range (size)) { victim_index = smallbin_index (size); @@ -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, -- 2.45.2