Suboptimal array copy in druntime?

Guillaume Chatelet via Digitalmars-d digitalmars-d at puremagic.com
Sun Apr 16 03:08:22 PDT 2017


I was looking at the _d_arrayassign family functions in druntime:
https://github.com/dlang/druntime/blob/master/src/rt/arrayassign.d#L47
https://github.com/dlang/druntime/blob/master/src/rt/arrayassign.d#L139

The code seems suboptimal for several reasons:

1. memcpy is more efficient on big arrays than iterating on a few 
bytes because it can use mmx/sse/avx. I would naturally memcpy 
the whole array and postblit/destroy individual elements 
separately.

2. ti.destroy and ti.postblit are always called but they might do 
nothing, since the code is not templated the compiler can't 
eliminate the calls. How about caching in TypeInfo if the type 
has a non empty destructor / postblit and do:

   if(ti.hasDestroy)
     for(element : dst_array)
       ti.destroy(element);
   memcpy(dst_array, src_array);
   if(ti.hasPostBlit)
     for(element : dst_array)
       ti.postblit(element);

Granted that worse case we iterate the array several time, we 
could fallback to the current implementation if both are set.

Did I miss something?


More information about the Digitalmars-d mailing list