[PATCH 1/3] malloc: Perform batched frees if tcache is full
Florian Weimer
fweimer@redhat.com
Thu Dec 18 10:17:32 GMT 2025
Batched frees amortize the locking overhead once the tcache is full.
Previously, once the tcache slot was full, each free acquired the
arena lock. With this change, neighboring tcache allocations in the
same arena re-use an arena lock that already exists.
Pass the tcache pointer to __libc_free_batch so that it is not
necessary to reload the tcache pointer after the _int_free_chunk
calls.
---
malloc/malloc.c | 66 +++++++++++++++++++++++++++++++++++++++++++------
1 file changed, 58 insertions(+), 8 deletions(-)
diff --git a/malloc/malloc.c b/malloc/malloc.c
index be29929993..d2184c8d7a 100644
--- a/malloc/malloc.c
+++ b/malloc/malloc.c
@@ -3037,7 +3037,8 @@ tcache_put_n (mchunkptr chunk, size_t tc_idx, tcache_entry **ep, bool mangled)
available chunks to remove. Removes chunk from the middle of the
list. */
static __always_inline void *
-tcache_get_n (size_t tc_idx, tcache_entry **ep, bool mangled)
+tcache_get_n (tcache_perthread_struct *tc, size_t tc_idx, tcache_entry **ep,
+ bool mangled)
{
tcache_entry *e;
if (!mangled)
@@ -3053,7 +3054,7 @@ tcache_get_n (size_t tc_idx, tcache_entry **ep, bool mangled)
else
*ep = PROTECT_PTR (ep, REVEAL_PTR (e->next));
- ++(tcache->num_slots[tc_idx]);
+ ++(tc->num_slots[tc_idx]);
e->key = 0;
return (void *) e;
}
@@ -3068,7 +3069,7 @@ tcache_put (mchunkptr chunk, size_t tc_idx)
static __always_inline void *
tcache_get (size_t tc_idx)
{
- return tcache_get_n (tc_idx, &tcache->entries[tc_idx], false);
+ return tcache_get_n (tcache, tc_idx, &tcache->entries[tc_idx], false);
}
static __always_inline tcache_entry **
@@ -3111,7 +3112,7 @@ tcache_get_large (size_t tc_idx, size_t nb)
if (te == NULL || nb != chunksize (mem2chunk (te)))
return NULL;
- return tcache_get_n (tc_idx, entry, mangled);
+ return tcache_get_n (tcache, tc_idx, entry, mangled);
}
static void tcache_init (mstate av);
@@ -3149,7 +3150,7 @@ tcache_get_align (size_t nb, size_t alignment)
if (te != NULL
&& csize == nb
&& PTR_IS_ALIGNED (te, alignment))
- return tag_new_usable (tcache_get_n (tc_idx, tep, mangled));
+ return tag_new_usable (tcache_get_n (tcache, tc_idx, tep, mangled));
DIAG_POP_NEEDS_COMMENT;
}
return NULL;
@@ -3332,6 +3333,49 @@ tcache_free_init (void *mem)
__libc_free (mem);
}
+/* Deallocate half of the tcache entries into arenas, to amortize the
+ locking overhead. */
+static __attribute_noinline__ void
+__libc_free_batched (mchunkptr p, INTERNAL_SIZE_T size,
+ tcache_perthread_struct *tc, size_t tc_idx)
+{
+ /* Check size >= MINSIZE and p + size does not overflow. */
+ if (__glibc_unlikely (INT_ADD_OVERFLOW ((uintptr_t) p,
+ size - MINSIZE)))
+ return malloc_printerr_tail ("free(): invalid size (batch)");
+
+ /* Empty half of the tcache, for a hysteresis effect. */
+ unsigned int to_free = mp_.tcache_count / 2;
+
+ /* If the arena does not change between chunks, keep the lock. */
+ mstate av = arena_for_chunk (p);
+ __libc_lock_lock (av->mutex);
+ _int_free_chunk (av, p, size, true);
+
+ while (tc->entries[tc_idx] != NULL && to_free > 0)
+ {
+ void *mem = tcache_get_n (tc, tc_idx, &tc->entries[tc_idx], false);
+ p = mem2chunk (mem);
+ size = chunksize (p);
+
+ /* Lock a different arena if necessary. */
+ {
+ mstate chunk_av = arena_for_chunk (p);
+ if (chunk_av != av)
+ {
+ __libc_lock_unlock (av->mutex);
+ av = chunk_av;
+ __libc_lock_lock (av->mutex);
+ }
+ }
+
+ _int_free_chunk (av, p, size, true);
+ to_free--;
+ }
+
+ __libc_lock_unlock (av->mutex);
+}
+
void
__libc_free (void *mem)
{
@@ -3370,6 +3414,13 @@ __libc_free (void *mem)
{
if (__glibc_likely (tcache->num_slots[tc_idx] != 0))
return tcache_put (p, tc_idx);
+ else
+ {
+ /* Perform batched freeing of tcache entries. */
+ if (__glibc_unlikely (tcache_inactive ()))
+ return tcache_free_init (mem);
+ return __libc_free_batched (p, size, tcache, tc_idx);
+ }
}
else
{
@@ -3377,10 +3428,9 @@ __libc_free (void *mem)
if (size >= MINSIZE
&& __glibc_likely (tcache->num_slots[tc_idx] != 0))
return tcache_put_large (p, tc_idx);
+ if (__glibc_unlikely (tcache_inactive ()))
+ return tcache_free_init (mem);
}
-
- if (__glibc_unlikely (tcache_inactive ()))
- return tcache_free_init (mem);
}
#endif
--
2.52.0
More information about the Libc-alpha
mailing list