Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[RFC-002] Standardize where to return or pass outward by reference #36

Open
TheCurle opened this issue Aug 17, 2024 · 4 comments
Open
Labels
RFC Request for Comments

Comments

@TheCurle
Copy link
Member

Currently the engine uses a large mixture of the two.

We should standardize where to:

bool ProcessStuff(const Input& in, Output& out);

or:

Output ProcessStuff(const Input& in);

because the current system just doesn't work.

@dpeter99
Copy link
Member

I haven't written any code that does pass out by reference. I think we should return by value as that is easier to understand. And where we do want to pass back values (multiple outputs or allocation avoidance) we should clearly mark the parameter as an out perameter

@dpeter99 dpeter99 added the RFC Request for Comments label Aug 18, 2024
@TheCurle
Copy link
Member Author

I use pass out-by-reference for instances where allocating and copying the necessary data multiple times isn't feasible, or where allocating, passing and then immediately destroying the data would be too heavy on heap allocations. For example, the shader compiler:

/**
     * Compile the shader module from the given source input, storing into the given output.
     * @param input source input, data and metadata, and compiler options
     * @param out the binary data if successful, errors if unsuccessful
     */
    void Compile(const CompilerInput& input, CompilerOutput& out);

This could return a CompilerOutput*, but the lifetime of the object is literally the caller + the line after the caller, and it will never be used again. That's a heavy allocation.

Pass out-by-reference allows stack memory of the call site to be used for the allocation, which is better on memory, and doesn't involve a heap allocate and delete, which may even be faster for a hot path like the shader compilation (which is done lazily)

@dpeter99
Copy link
Member

Yeah that sounds fine to me

@TheCurle
Copy link
Member Author

Herein lies the dilemma. We need to be sure between the two of us where the line that makes this okay lies.

If it comes down to developer discretion, then that's fair enough - as long as we're on the same page.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
RFC Request for Comments
Projects
None yet
Development

No branches or pull requests

2 participants