D performance: union vs @property
Asked Answered
I

1

7

I'm in the process of porting, enhancing, and D-atizing our reign SDK from C# to D. Currently working on the Vector2 math module.

Will there be any performance difference between the two structs below? My benchmarks show identical performance, but I'd like to gain a bit of expert insight :)

struct Vector2(T)
{
    T x, y;
    @property T u() { return x; }
    @property T v() { return y; }
    @property void u(T value) { x = value; }
    @property void v(T value) { y = value; }
}

struct Vector2(T)
{
    union { T x, u; }
    union { T y, v; }
}

Obviously I'd like to use the unions for syntactical simplicity. But is there any unforeseen pitfalls with using them? I'm unfamiliar with their low-level details.

On a side note, I'm adding in vector property syntax similar to HLSL/GLSL, e.g., vec1.yxz += vec2.xyz; There's... no.. possible way to do that with unions instead of @property is there?

Indole answered 31/12, 2011 at 9:5 Comment(2)
You'll need opDispatch for vec1.yxz += vec2.xyz, and I believe someone has done this already.Joeljoela
@KennyTM - you are correct - bitbucket.org/dav1d/gljm and bitbucket.org/dav1d/gl3nIngeborgingelbert
W
13

Use alias!

struct Vector2(T)
{
    T x, y;
    alias x u;
    alias y v;
}
Witherite answered 31/12, 2011 at 9:14 Comment(1)
Doh! ... I should have known that. Thanks!Indole

© 2022 - 2024 — McMap. All rights reserved.