[PATCH v7] malloc: Optimize small memory clearing for calloc

H.J. Lu hjl.tools@gmail.com
Wed Dec 4 00:16:37 GMT 2024


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.

-- 
H.J.


More information about the Libc-alpha mailing list