Why is this D code slower than C++?

Bill Baxter dnewsgroup at billbaxter.com
Thu Jan 18 11:41:58 PST 2007


Bradley Smith wrote:
> 
> 
> Bill Baxter wrote:
>> %u wrote:
>>> Bill Baxter Wrote:

>> Material is a by-value member of Primitive in the C++ version.  This 
>> means it acts more like a D struct than a D class.  GetMaterial calls 
>> return a pointer to the Material that's part of the class, and it will 
>> have been initialized implicitly by the Primitive constructor using 
>> whatever Material's default constructor does.
>>
>> So the C++ code is ok.  But it's not clear why Material became a class 
>> in the D version rather than a struct.
> 
> Because in the C++, GetMaterial returns a pointer. Since other objects 
> can use the pointer to change the value of the Material contained within 
> a Primitive, the same behavior was used in the D code by using a class. 
> If a struct had been used, a copy of Material would be returned, and 
> changing the Material would have no effect on the Primitive.
> 
> Also, because GetMaterial is called very often, I assume that making 
> lots of copies of it would decrease performance. Presumably, that is why 
> the C++ code returns a pointer.

You can return pointers in D too.  But anyway, I don't think the change 
from by-value class in C++ to a by-reference class in D made any 
difference in the runtime.  I wasn't saying that it was wrong that you 
changed Material to a D class or anything.  It's a valid approach and 
certainly more D-ish than returning a pointer to a struct.

--bb


More information about the Digitalmars-d-learn mailing list