[PATCH] malloc: Simplify _int_free_chunk

Wilco Dijkstra Wilco.Dijkstra@arm.com
Thu Apr 2 12:20:39 GMT 2026


Hi,
 
>@@ -3472,7 +3472,7 @@ __libc_realloc (void *oldmem, size_t bytes)
>          size_t sz = memsize (oldp);
>          memcpy (newp, oldmem, sz);
>          (void) tag_region (chunk2mem (oldp), sz);
>-          _int_free_chunk (ar_ptr, oldp, chunksize (oldp), 0);
>+          _int_free_chunk (ar_ptr, oldp, chunksize (oldp));
>         }

> Could you add some workaround to realloc to put user requested chunk
> into tcache? For remainders, it's OK as user never requested. But for
> some expanding scenario, the old chunk is requested by user, thus
> put them into tcache may improve performance comparing to put them
> in bins?

It's possible - but note realloc() doesn't use tcache on allocation (and hasn't
historically). In general it seems like a bad idea to keep the arena lock while
doing memcpy (or tagging) of large blocks since that can block other threads.
If we moved the locking into _int_realloc(), we could unlock if we can't reuse
the existing/next block, and then use __libc_malloc/__libc_free so that it can
use tcache in both cases. We could even avoid locking by doing a speculative
check.

Do you see any performance issues with realloc()? There are lots of things
that could be improved - for example it should not split off tiny blocks when
shrinking, and similarly when growing, it should force a minimum percentage
of growth.

Cheers,
Wilco


More information about the Libc-alpha mailing list