[PATCH v7] malloc: Optimize small memory clearing for calloc
H.J. Lu
hjl.tools@gmail.com
Wed Dec 4 07:02:59 GMT 2024
On Wed, Dec 4, 2024 at 9:45 AM Guo, Wangyang <wangyang.guo@intel.com> wrote:
>
> On 12/4/2024 8:16 AM, H.J. Lu wrote:
>
> On Wed, Dec 4, 2024 at 7:32 AM Paul Eggert <eggert@cs.ucla.edu> wrote:
>
> On 2024-12-02 20:42, H.J. Lu wrote:
>
> + /* Use repeated stores with 1 branch, instead of up to 3. */
> + *(d + 0) = 0;
> + *(d + 1) = 0;
> + *(d + 2) = 0;
> + *(d + nclears - 2) = 0;
> + *(d + nclears - 2 + 1) = 0;
> + if (nclears > 5)
> + {
> + *(d + 3) = 0;
> + *(d + 3 + 1) = 0;
> + *(d + nclears - 4) = 0;
> + *(d + nclears - 4 + 1) = 0;
> + }
>
> The following alternative code has no branches, when I compile with GCC
> 14 -O2 on x86-64. Would that be a win?
>
> /* Use repeated stores with no branches. */
> int big = 3 * ((nclears + 1) >> 3);
> d[0] = 0;
> d[1] = 0;
> d[2] = 0;
> d[big] = 0;
> d[big + 1] = 0;
> d[nclears - big - 1] = 0;
> d[nclears - big] = 0;
> d[nclears - 2] = 0;
> d[nclears - 2 + 1] = 0;
>
> Wangyang, can you try this against the current master branch?
>
> Thanks.
>
> I try to apply the alternative code with attached patch, but meet coredump error when running bench-calloc-thread:
>
> Fatal glibc error: malloc.c:2601 (sysmalloc): assertion failed: (old_top == initial_top (av) && old_size == 0) || ((unsigned long) (old_size) >= MINSIZE && prev_inuse (old_top) && ((unsigned long) old_end & (pagesize - 1)) == 0)
Try this.
--
H.J.
-------------- next part --------------
A non-text attachment was scrubbed...
Name: p.diff
Type: text/x-patch
Size: 955 bytes
Desc: not available
URL: <https://sourceware.org/pipermail/libc-alpha/attachments/20241204/4c3d9fff/attachment.bin>
More information about the Libc-alpha
mailing list