www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Array assigns performance

reply bearophile <bearophileHUGS lycos.com> writes:
In some (not benchmark) D2 code I have this (variable names changed):

int[2][8] array;
...
array[i] = [x, y];


Such lines of code are not inside the inner loop, that's in the "..." part.
Despite they are not in the inner loop I've seen that replacing this line:
array[i] = [x, y];

with:
array[i][0] = x;
array[i][1] = y;

reduces the run time from about 1.8 seconds to about 1.05 seconds :-)

If I change the matrix type like this:
int[][8] array;

The runtime goes to about 1.55 seconds, because using dynamic arrays just for
two integers is not so efficient for other parts of the code.

(I have also tried to replace the int[2] with a Tuple!(int,int) but this seems
to give small problems elsewhere in my code, I have not tried this hard enough
yet.)

I'd like DMD to compile and perform this very simple line of code in a much
more efficient way:
array[i] = [x, y];

Bye,
bearophile
Jun 02 2011
parent reply "Robert Jacques" <sandford jhu.edu> writes:
On Thu, 02 Jun 2011 10:36:54 -0400, bearophile <bearophileHUGS lycos.com>  
wrote:
 In some (not benchmark) D2 code I have this (variable names changed):

 int[2][8] array;
 ...
 array[i] = [x, y];


 Such lines of code are not inside the inner loop, that's in the "..."  
 part. Despite they are not in the inner loop I've seen that replacing  
 this line:
 array[i] = [x, y];

 with:
 array[i][0] = x;
 array[i][1] = y;

 reduces the run time from about 1.8 seconds to about 1.05 seconds :-)

 If I change the matrix type like this:
 int[][8] array;

 The runtime goes to about 1.55 seconds, because using dynamic arrays  
 just for two integers is not so efficient for other parts of the code.

 (I have also tried to replace the int[2] with a Tuple!(int,int) but this  
 seems to give small problems elsewhere in my code, I have not tried this  
 hard enough yet.)

 I'd like DMD to compile and perform this very simple line of code in a  
 much more efficient way:
 array[i] = [x, y];

 Bye,
 bearophile
bearophile, array literals default to dynamic arrays, which require an allocation, etc. IIRC, originally, literals specified fixed sized arrays, but was considered unexpected behavior and reduced usability and was changed.
Jun 02 2011
parent bearophile <bearophileHUGS lycos.com> writes:
Robert Jacques:

 bearophile, array literals default to dynamic arrays, which require an  
 allocation, etc. IIRC, originally, literals specified fixed sized arrays,  
 but was considered unexpected behavior and reduced usability and was  
 changed.
Here I'm asking for an optimization. This means adding some new extra code to the front-end to recognize a common pattern, and compile (replace) it with something simpler and more efficient. Bye, bearophile
Jun 02 2011