On Tuesday, 2 January 2018 at 18:21:13 UTC, Tim Hsu wrote:
I am creating Vector3 structure. I use struct to avoid GC. However, struct will be copied when passed as parameter to function


struct Ray {
    Vector3f origin;
    Vector3f dir;

    @nogc @system
    this(Vector3f *origin, Vector3f *dir) {
        this.origin = *origin;
        this.dir = *dir;
    }
}

How can I pass struct more efficiently?


If you want the compiler to ensure that a struct doesn't get copied, you can disable its postblit:

 @disable this(this);

Now, there are a couple of goodies in std.typecons like RefCounted or Unique that allow you to pass struct around without needing to worry about memory allocation:

https://dlang.org/phobos/std_typecons.html#RefCounted
https://dlang.org/phobos/std_typecons.html#Unique

Example: https://run.dlang.io/is/3rbqpn

Of course, you can always roll your own allocator:

https://run.dlang.io/is/uNmn0d

Reply via email to