[PATCH v2 2/5] malloc: Avoid func call for tcache quick path in free()

Florian Weimer fweimer@redhat.com
Wed Aug 28 08:26:55 GMT 2024


* Wangyang Guo:

> Tcache is an important optimzation to accelerate memory free(), things
> within this code path should be kept as simple as possible. This commit
> try to remove the function call when free() invokes tcache code path.
>
> Result of bench-malloc-thread benchmark
>
> Test Platform: Xeon-8380
> Ratio: New / Original time_per_iteration (Lower is Better)
>
> Threads#   | Ratio
> -----------|------
> 1 thread   | 0.904
> 4 threads  | 0.919
>
> The performance data shows it can improve bench-malloc-thread benchmark
> by ~10% in single thread and ~8% in multi-thread scenario.

Does the speedup come from bypassing these checks at the start of
_int_free?

  size = chunksize (p);

  /* Little security check which won't hurt performance: the
     allocator never wraps around at the end of the address space.
     Therefore we can exclude some size values which might appear
     here by accident or by "design" from some intruder.  */
  if (__builtin_expect ((uintptr_t) p > (uintptr_t) -size, 0)
      || __builtin_expect (misaligned_chunk (p), 0))
    malloc_printerr ("free(): invalid pointer");
  /* We know that each chunk is at least MINSIZE bytes in size or a
     multiple of MALLOC_ALIGNMENT.  */
  if (__glibc_unlikely (size < MINSIZE || !aligned_OK (size)))
    malloc_printerr ("free(): invalid size");

  check_inuse_chunk(av, p);

I wonder if it's due to to that, or indeed due to inlining.

Thanks,
Florian



More information about the Libc-alpha mailing list