writing efficient struct operators

Bill Baxter dnewsgroup at billbaxter.com
Sun Nov 4 13:48:50 PST 2007


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


More information about the Digitalmars-d-learn mailing list