Optimize my code =)

Nick Sabalausky SeeWebsiteToContactMe at semitwist.com
Mon Feb 17 15:10:23 PST 2014


On 2/17/2014 4:56 PM, Robin wrote:
>
> And here is the complete and improved code:
> http://dpaste.dzfl.pl/7f8610efa82b
>

You should get rid of the .dup's in the constructors and postblits. You 
don't want big arrays ever getting accidentally allocated and copied 
behind your back when you're only trying to pass things around. Better 
to provide an explicit .dup function in your Matrix class (just like how 
D's dynamic arrays work) for when you *intentionally* want a full duplicate.

Also, unlike classes, when you're copying a struct there's no need to 
copy each field individually. Just copy the struct as a whole. In fact, 
copy constructors and overloading assignments from the same type are 
generally not needed at all: They aren't going to be any faster than 
just using D's default behavior of memory-blitting the struct to the new 
location *if* a copy is actually even needed at all. By providing 
explicit copy constructors and overloading assignments from the same 
type, you're forcing the compiler to use a potentially-slower method 
every time.

Finally, and I could be wrong on this part, but I doubt those move 
constructors are really needed in D. See the top answer here: 
http://stackoverflow.com/questions/4200190/does-d-have-something-akin-to-c0xs-move-semantics



More information about the Digitalmars-d-learn mailing list