On Thu, Mar 06, 2014 at 12:02:42AM +0000, develop32 wrote: > On Wednesday, 5 March 2014 at 23:47:33 UTC, H. S. Teoh wrote: > >Whoa. What did you do with those arrays?? Either you did something > >wrong, or there's a nasty bug somewhere in the compiler/language; > >AFAIK static arrays are supposed to be value types so they shouldn't > >generate any garbage at all. > > I think it was the case of using array literals, like this (I didn't > know much about D back then) > > this(float x, float y, float z) > { > this.vector = [x, y, z]; > }
I assume this.vector is a float[3]? Yeah, this is one of the things in D that I find disappointing. Array literals are a minefield of hidden allocations and runtime performance hits. In theory, the compiler *should* realize that since this.vector is a static array, it should just assign x, y, z directly to the array elements. But IIRC (assuming the compiler hasn't changed in this respect) what it actually does is to construct a *dynamic* array [x, y, z] and then assign it to the static array. Which, of course, produces huge amounts of garbage. And which kinda defeats the purpose of using static arrays in the first place... :-( > And megabytes accumulated because there were hundreds of objects all > doing complicated stuff every frame, passing and constructing > vectors and matrices around. > > Memory leaks could have been avoided, but still, one should be > careful when using arrays. A recent idiom of mine: struct Vector(T, size_t n) { T[n] impl; alias impl this; this(Args...)(Args args) if (Args.length == n) { // This is compile-time unrolled foreach (i, arg; args) { impl[i] = arg; } } } T -- The two rules of success: 1. Don't tell everything you know. -- YHL