www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - writing efficient struct operators

reply blevin <brett.levin gmail.com> writes:
I noticed that dmd generates implicit checks for a NULL "this" argument 
in overloaded struct operators.  Assuming my operator does not modify 
its arguments, including "this", is there a way to avoid that cost?

Putting the equivalent code in a static member function with "in" 
arguments avoids the cost but gives up the nice syntax.  The struct in 
question is a vector, so it'd be nice to provide operators.

Perhaps D2.0's invariant keyword address this?  (There doesn't seem to 
be a released D2.0 compiler for linux yet, so I haven't checked.)

   struct Vec3d
   {
       double x=0, y=0, z=0;

       Vec3d opAdd(Vec3d rhs) {
           // Note: implicit assert(this != NULL) inserted here.
           // Would marking this method 'invariant' help?
           return Vec3d(x+rhs.x, y+rhs.y, z+rhs.z);
       }

       static Vec3d Add(in Vec3d a, in Vec3d b) {
           // Static method avoids the assert
           return Vec3d(a.x+b.x, a.y+b.y, a.z+b.z);
       }

       static Vec3d opCall(double x, double y, double z) {
           Vec3d v;
           v.x = x;
           v.y = y;
           v.z = z;
           return v;
       }
   }

thanks,
--
Brett.
Nov 04 2007
next sibling parent Bill Baxter <dnewsgroup billbaxter.com> writes:
blevin wrote:
 I noticed that dmd generates implicit checks for a NULL "this" argument 
 in overloaded struct operators.  Assuming my operator does not modify 
 its arguments, including "this", is there a way to avoid that cost?
 
 Putting the equivalent code in a static member function with "in" 
 arguments avoids the cost but gives up the nice syntax.  The struct in 
 question is a vector, so it'd be nice to provide operators.
 
 Perhaps D2.0's invariant keyword address this?  (There doesn't seem to 
 be a released D2.0 compiler for linux yet, so I haven't checked.)
 
   struct Vec3d
   {
       double x=0, y=0, z=0;
 
       Vec3d opAdd(Vec3d rhs) {

It'll run faster if you make this a "ref Vec3d rhs". Don't know about the rest of your question, but benchmarking was done on someone's ray tracer a while back and changing the Vec's to ref Vecs definitely made a difference. --bb
Nov 04 2007
prev sibling parent reply "Jarrett Billingsley" <kb3ctd2 yahoo.com> writes:
"blevin" <brett.levin gmail.com> wrote in message 
news:fglb8v$2tr4$1 digitalmars.com...
I noticed that dmd generates implicit checks for a NULL "this" argument in 
overloaded struct operators.  Assuming my operator does not modify its 
arguments, including "this", is there a way to avoid that cost?

This is a debug check and disappears with the -release flag.
Nov 04 2007
parent blevin <brett.levin gmail.com> writes:
Jarrett Billingsley wrote:
 "blevin" <brett.levin gmail.com> wrote in message 
 news:fglb8v$2tr4$1 digitalmars.com...
 I noticed that dmd generates implicit checks for a NULL "this" argument in 
 overloaded struct operators.  Assuming my operator does not modify its 
 arguments, including "this", is there a way to avoid that cost?

This is a debug check and disappears with the -release flag.

Aha! Indeed it does -- thank you! -- Brett.
Nov 04 2007