March 05, 2014
On Wednesday, 5 March 2014 at 22:46:40 UTC, sclytrack wrote:
> Are there any disadvantages of using a fixed size array for fixed size
> coordinates and vectors, over creating an actual typedef or struct Vec3?

Don't know what's the current situation in druntime, but when I tried static arrays a while ago in my engine every second there were megabytes of garbage generated. I ended up using structs with fields.
March 05, 2014
On Wed, Mar 05, 2014 at 11:31:12PM +0000, develop32 wrote:
> On Wednesday, 5 March 2014 at 22:46:40 UTC, sclytrack wrote:
> >Are there any disadvantages of using a fixed size array for fixed size coordinates and vectors, over creating an actual typedef or struct Vec3?
> 
> Don't know what's the current situation in druntime, but when I tried static arrays a while ago in my engine every second there were megabytes of garbage generated. I ended up using structs with fields.

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.


T

-- 
A computer doesn't mind if its programs are put to purposes that don't match their names. -- D. Knuth
March 06, 2014
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];
}

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.

March 06, 2014
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
1 2 3 4 5 6
Next ›   Last »