[glibc/fw/malloc-batch-unsorted-basic] malloc: Only fill half of the tcache during batch allocation

Florian Weimer via Glibc-cvs <[email protected]> Tue, 30 Jun 2026 07:07:58 +0000 (GMT)
Newsgroups gmane.comp.lib.glibc.cvs
Message-ID <[email protected]>
https://sourceware.org/git/gitweb.cgi?p=glibc.git;h=b50e24aea36d4430eb12a4ffb2d2dad7c4747772

commit b50e24aea36d4430eb12a4ffb2d2dad7c4747772
Author: Florian Weimer <[email protected]>
Date:   Fri Nov 7 10:27:20 2025 +0100

    malloc: Only fill half of the tcache during batch allocation
    
    This leaves room for subsequent frees.

Diff:
---
 malloc/malloc.c | 7 +++++--
 1 file changed, 5 insertions(+), 2 deletions(-)

diff --git a/malloc/malloc.c b/malloc/malloc.c
index 99ba94454e..3505158ac0 100644
--- a/malloc/malloc.c
+++ b/malloc/malloc.c
@@ -3570,8 +3570,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)