digitalmars.D.learn - writing efficient struct operators
- blevin (31/31) Nov 04 2007 I noticed that dmd generates implicit checks for a NULL "this" argument
- Bill Baxter (6/22) Nov 04 2007 It'll run faster if you make this a "ref Vec3d rhs".
- Jarrett Billingsley (3/6) Nov 04 2007 This is a debug check and disappears with the -release flag.
- blevin (4/11) Nov 04 2007 Aha! Indeed it does -- thank you!
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
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
"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
Jarrett Billingsley wrote:"blevin" <brett.levin gmail.com> wrote in message news:fglb8v$2tr4$1 digitalmars.com...Aha! Indeed it does -- thank you! -- Brett.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









Bill Baxter <dnewsgroup billbaxter.com> 