[PATCH] Optimize double storing by memset; memcpy (Take two)

Duncan Sands baldrick at free.fr
Sun Mar 10 00:27:30 PST 2013


Hi Joel,

On 06/03/13 17:03, Joel Jones wrote:
> A common idiom in some code is to do the following:
>    memset(dest, 0, dest_size);
>    memcpy(dest, src, src_size);
>
> This patch implements a rewrite to avoid storing to the same location twice. The above code is rewritten as:
>
>   memcpy(dest, src, dest_size);
>   memset((char *)dest + dest_size, setVal,
>           src_size > dest_size ? src_size - dest_size : 0);

in your patch I think you forget to compute the correct alignment for the
new memset.  After all, if dest is 4 byte aligned but dest_size is 1 then
dest + dest_size is only 1 byte aligned.  That said, is it even a good idea
to do this transform if it results in a less aligned memset?  I can see a
compromise solution too: do the memset starting from dest+adjusted_dest_size
where adjusted_dest_size = dest_size rounded down to the nearest multiple of
the alignment.  I.e. in order to have a good alignment, be prepared to increase
the number of bytes being memset (the memset would then have to be placed
before the memcpy).  I'm not sure this is a good idea either, as it means that
the memset and memcpy would touch the same overlapping memory.

Ciao, Duncan.



More information about the llvm-commits mailing list