From patchwork Thu Dec 18 10:17:52 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Florian Weimer X-Patchwork-Id: 126769 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from vm01.sourceware.org (localhost [127.0.0.1]) by sourceware.org (Postfix) with ESMTP id 8D0144BA2E37 for ; Thu, 18 Dec 2025 10:19:08 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 8D0144BA2E37 Authentication-Results: sourceware.org; dkim=pass (1024-bit key, unprotected) header.d=redhat.com header.i=@redhat.com header.a=rsa-sha256 header.s=mimecast20190719 header.b=bvYa+Bul X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by sourceware.org (Postfix) with ESMTP id 900A54BA2E25 for ; Thu, 18 Dec 2025 10:17:58 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 900A54BA2E25 Authentication-Results: sourceware.org; dmarc=pass (p=quarantine dis=none) header.from=redhat.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=redhat.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 900A54BA2E25 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=170.10.129.124 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1766053078; cv=none; b=C1CzxTkuf4z7cr2UoHidLrG5dg7IaHzOl18aSj+PJhGUnBC8s4SPBsxAOjtW4Ea4loF0jmlhI89K5JW2spxQo0YnkI6RMmS/Bb7OcxfRcsQAQw3l0BhbOfA6ZE397ipfJ6q3QFj6EImVpbrTYKG0IfgM1dVh5/rp73WKimB5FzM= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1766053078; c=relaxed/simple; bh=goYBhFxyiIXci6huuIfSbxWZnhEUaEuUto1PqvmI/YM=; h=DKIM-Signature:From:To:Subject:Message-ID:Date:MIME-Version; b=MV3JGodplascz96Krdrd1me0v4atyrUTZ9AGa4GhBuHvUSFBpzEt1h8cALU4KkzFfLhyHso+f7uzBYBPfFgoC75kTiJPMbBfm+V9bpF43nhyw7YIePNigVFuh8pw1jB09IFe8Bp+722iVs+LLz4rkww2XoKWuCud1RrDzIVBLzA= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 900A54BA2E25 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1766053078; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=8QaJkGFaQhr+gicLSON/F0BqcIL5HHIdj/oqO+VKk9g=; b=bvYa+BulGHWLqboOPYAbeK+8RKdWLznKmn7CBWRqWSaJ30S9EA4tUaybGq4VMO7i5Y7Z4R s2vSu4l7Xos4haLdoTpPaopFQoy0L/+OOJ4Hc7Bc4LWRFEnfyjppveOJK5pV9ckn+dMKI7 bgJY1vJN8XRMPhbcyCSIlLw/K9yvd20= Received: from mx-prod-mc-03.mail-002.prod.us-west-2.aws.redhat.com (ec2-54-186-198-63.us-west-2.compute.amazonaws.com [54.186.198.63]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-389-sw9u239PMHqXlP_LQimw0Q-1; Thu, 18 Dec 2025 05:17:56 -0500 X-MC-Unique: sw9u239PMHqXlP_LQimw0Q-1 X-Mimecast-MFC-AGG-ID: sw9u239PMHqXlP_LQimw0Q_1766053076 Received: from mx-prod-int-03.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-03.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.12]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mx-prod-mc-03.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id 11A661956050 for ; Thu, 18 Dec 2025 10:17:56 +0000 (UTC) Received: from fweimer-oldenburg.csb.redhat.com (unknown [10.44.33.94]) by mx-prod-int-03.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id 5ACE71953986 for ; Thu, 18 Dec 2025 10:17:55 +0000 (UTC) From: Florian Weimer To: libc-alpha@sourceware.org Subject: [PATCH 2/3] malloc: Only fill half of the tcache during batch allocation In-Reply-To: Message-ID: <22984336264c0cb2bad3c2459631ac14d4a890ac.1766051058.git.fweimer@redhat.com> References: X-From-Line: 22984336264c0cb2bad3c2459631ac14d4a890ac Mon Sep 17 00:00:00 2001 Date: Thu, 18 Dec 2025 11:17:52 +0100 User-Agent: Gnus/5.13 (Gnus v5.13) MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.0 on 10.30.177.12 X-Mimecast-Spam-Score: 0 X-Mimecast-MFC-PROC-ID: 1l6-RAO_GNsVOQcXNmFKq_blCrC0H_V1sdFH-MHqBE8_1766053076 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-10.8 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, RCVD_IN_DNSWL_BLOCKED, RCVD_IN_MSPIKE_H2, RCVD_IN_VALIDITY_RPBL_BLOCKED, RCVD_IN_VALIDITY_SAFE_BLOCKED, SPF_HELO_PASS, SPF_NONE, TXREP autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on 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~patchwork=sourceware.org@sourceware.org This leaves room for subsequent frees. --- malloc/malloc.c | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/malloc/malloc.c b/malloc/malloc.c index d2184c8d7a..d42f0183e3 100644 --- a/malloc/malloc.c +++ b/malloc/malloc.c @@ -3940,8 +3940,11 @@ _int_malloc (mstate av, size_t bytes) if (__glibc_unlikely (tcache_inactive ())) tcache_init (av); - /* While bin not empty and tcache not full, copy chunks over. */ - while (tcache->num_slots[tc_idx] != 0 + /* While bin not empty and tcache not full, copy chunks over. + Only fill half of the tcache, so that subsequent frees + do not immediately flush the tcache. */ + unsigned int tcache_target_count = mp_.tcache_count / 2; + while (tcache->num_slots[tc_idx] > tcache_target_count && (tc_victim = last (bin)) != bin) { if (tc_victim != NULL) @@ -3988,6 +3991,7 @@ _int_malloc (mstate av, size_t bytes) if (tc_idx < mp_.tcache_small_bins) tcache_nb = nb; int return_cached = 0; + unsigned int tcache_target_count = mp_.tcache_count / 2; tcache_unsorted_count = 0; #endif @@ -4066,9 +4070,11 @@ _int_malloc (mstate av, size_t bytes) if (__glibc_unlikely (tcache_inactive ())) tcache_init (av); /* Fill cache first, return to user only if cache fills. - We may return one of these chunks later. */ + We may return one of these chunks later. Only fill + half of tcache, so that subsequent frees do not flush + it immediately. */ if (tcache_nb > 0 - && tcache->num_slots[tc_idx] != 0) + && tcache->num_slots[tc_idx] > tcache_target_count) { tcache_put (victim, tc_idx); return_cached = 1;