Thread overview | ||||||
---|---|---|---|---|---|---|
|
November 04, 2007 writing efficient struct operators | ||||
---|---|---|---|---|
| ||||
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. |
November 04, 2007 Re: writing efficient struct operators | ||||
---|---|---|---|---|
| ||||
Posted in reply to blevin | 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
|
November 04, 2007 Re: writing efficient struct operators | ||||
---|---|---|---|---|
| ||||
Posted in reply to blevin | "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. |
November 05, 2007 Re: writing efficient struct operators | ||||
---|---|---|---|---|
| ||||
Posted in reply to Jarrett Billingsley | 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.
|
Copyright © 1999-2021 by the D Language Foundation