[PATCH v2] malloc: move tcache_init out of hot tcache paths

Cupertino Miranda cupertino.miranda@oracle.com
Mon Apr 14 13:41:50 GMT 2025


This patch moves any calls of tcache_init away after tcache hot paths.
Since there is no reason to initialize tcaches in the hot path and
since we need to be able to check tcache != NULL in any case, because of
tcache_thread_shutdown function, moving tcache_init away from hot path
can only be beneficial.
The patch also removes the initialization of tcaches within the
__libc_free call. It only makes sense to initialize tcaches for the
thread after it calls one of the allocation functions.
---
 malloc/malloc.c | 16 ++++++++--------
 1 file changed, 8 insertions(+), 8 deletions(-)

diff --git a/malloc/malloc.c b/malloc/malloc.c
index a0bc733482..2dcb8abeab 100644
--- a/malloc/malloc.c
+++ b/malloc/malloc.c
@@ -3361,13 +3361,16 @@ tcache_try_malloc (size_t bytes, void **memptr)
 
   size_t tc_idx = csize2tidx (tbytes);
 
-  MAYBE_INIT_TCACHE ();
 
   if (tcache_available (tc_idx))
-    *memptr = tcache_get (tc_idx);
+    {
+      *memptr = tcache_get (tc_idx);
+      return false;
+    }
   else
     *memptr = NULL;
 
+  MAYBE_INIT_TCACHE ();
   return false;
 }
 
@@ -3393,8 +3396,6 @@ __libc_malloc2 (size_t bytes)
   if (!__malloc_initialized)
     ptmalloc_init ();
 
-  MAYBE_INIT_TCACHE ();
-
   if (SINGLE_THREAD_P)
     {
       victim = tag_new_usable (_int_malloc (&main_arena, bytes));
@@ -3433,6 +3434,8 @@ __libc_malloc (size_t bytes)
 
   if (tcache_available (tc_idx))
     return tag_new_usable (tcache_get (tc_idx));
+  else
+    MAYBE_INIT_TCACHE ();
 #endif
 
   return __libc_malloc2 (bytes);
@@ -3474,8 +3477,6 @@ __libc_free (void *mem)
     }
   else
     {
-      MAYBE_INIT_TCACHE ();
-
       /* Mark the chunk as belonging to the library again.  */
       (void)tag_region (chunk2mem (p), memsize (p));
 
@@ -3696,8 +3697,6 @@ _mid_memalign (size_t alignment, size_t bytes, void *address)
       }
     size_t tc_idx = csize2tidx (tbytes);
 
-    MAYBE_INIT_TCACHE ();
-
     if (tcache_available (tc_idx))
       {
 	/* The tcache itself isn't encoded, but the chain is.  */
@@ -3715,6 +3714,7 @@ _mid_memalign (size_t alignment, size_t bytes, void *address)
 	  }
       }
   }
+  MAYBE_INIT_TCACHE ();
 #endif
 
   if (SINGLE_THREAD_P)
-- 
2.39.5



More information about the Libc-alpha mailing list