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?

This isn't a question for you. it's a question for the compiler, let the compiler do its thing. Stick in a -O3 if you are using GCC or LDC and build in release more not debug mode. Make sure that the compiler can see the source code of the implementation of the constructor wherever it is used and it should just be inclined away to nonexistence. Your constructor should not even exist, if it is then you are looking at a false picture or a mistake where optimisation has been turned off for the sake of easy source-level debugging.

Post up the assembler language output for a routine where this code is used in some critical situation, and then we can help make sure that the code _generation_ is optimal.

I reiterate, unless something is badly wrong or you are seeing a red herring, no 'call' to the constructor code should even exist in the cases where it is actually 'called'. You may well see a useless copy of the constructor code because the compilers seem to generate such even though it is never called ans so is a waste of space. The compiler will analyse the constructor's instructions and just copy-and-paste them as assignment statements with that then getting thoroughly optimised down into something which may just be a memory write or a register-register copy that costs zero.

If you post up snippets of generated asm then U will be delighted to take a look.

Reply via email to