-
-
Notifications
You must be signed in to change notification settings - Fork 2.7k
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
result locations: unwrap optional and error unions so that the payload can be non-copied #2761
Comments
Does this also covers other forms of payload capture? That is, will this allow copy elision for capturing loops, switch and if statements? |
Is there any plan for how to actually implement this? My guess would be that a function with return type, say, |
This issue is split from #287. What the result location mechanism has accomplished is that expressions do not introduce copies for intermediate values. For example:
Previously, the code
return bar()
would introduce an extra copy, so the body of the functionfoo
would needlessly copy the point before returning it. This copying would happen at every expression, recursively when the type is an aggregate type (such asstruct
). Now that the result location mechanism is merged into master, you can see that thefoo
function does not introduce an extra copy:However, if the expression unwraps an optional:
Now there must be a temporary stack variable and a memcpy:
The equivalent is true for the zig code with
catch
:This means that when writing a function that returns type
?T
or!T
, one cannot be sure that the return result location will not be copied.That's what this issue is meant to address. A solution to this issue will mean that calling such functions will not require a temporary variable or memcpy the result.
One thing to note is that wrapping an optional or error union does not have a copy; it is only unwrapping that is in question here:
You can see there is no copy here:
Related issue: #2765
The text was updated successfully, but these errors were encountered: