optimize vector code

Bill Baxter dnewsgroup at billbaxter.com
Fri Nov 30 04:28:58 PST 2007


Sascha Katzner wrote:
> Hi,
> 
> I'm currently trying to optimize my vector/matrix code.
> 
> the relevant section:
>> struct Vector3(T) {
>>     T x, y, z;
>>     void opAddAssign(Vector3 v) {
>>         x += v.x;
>>         y += v.y;
>>         z += v.z;
>>     }
>>     Vector3 opMul(T s) {
>>         return Vector3(x * s, y * s, z * s);
>>     }
>> }
> 
> If you compare the resulting code from this two examples:
> 
> first:
>>     v1 += v2 * 3.0f;
> => 0x59 bytes
> 
> second:
>>     v1.x += v2.x * 3.0f;
>>     v1.y += v2.y * 3.0f;
>>     v1.z += v2.z * 3.0f;
> => 0x36 bytes
> 
> ...it is rather obvious that this is not very good optimized, because 
> opMul() creates a new struct in the first example. Is there any Way to 
> guide the compiler in the first example to create more efficient code?
> 
> LLAP,
> Sascha
> 
> P.S. attached a complete example of this source
> 

Pass big structs by reference.
      void opAddAssign(ref Vector3 v) {...

--bb


More information about the Digitalmars-d-learn mailing list