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

Paul Eggert eggert@cs.ucla.edu
Tue Dec 3 23:32:27 GMT 2024


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;



More information about the Libc-alpha mailing list