-
Notifications
You must be signed in to change notification settings - Fork 723
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
Improve conventions for returning arrays from functions #129
Comments
See also #128. |
I was curious about this, so I decided to look at this briefly. This doesn't necessarily help much (I'm mostly just speaking out loud), though might as well share what I've done related to this. Given this Rust code: fn foo(mut a: [u8; 1024]) -> [u8; 1024] {
let mut i = 0;
for b in a.iter_mut() {
*b = i;
i += 1;
}
a
}
fn main() {} I generated the MIR and here's a relevant section:
I'm going to assume that the LLVM IR:
This is first few lines for the I didn't have time tonight to look to closely at the ASM to see if LLVM is doing the optimization here. EDIT: Playpen link |
Thanks for looking at this. Your function
Indeed, rustc doesn't optimize away the copy of |
It seems rust-lang/rfcs#788 is already filed about implementing the expected optimization. |
Closing this. All of the API improvements of this sort will be handled on a case-by-case basis. |
In some cases, e.g. in the implementations of
ring::aead::seal_in_place
andring_aead::open_in_place
, we pass a function an uninitialized (actually, zero-initialized) buffer and then expect that function to have filled the buffer with data if it returnsOk(())
. Instead, we should consider changing the result type of these functions toResult<[u8; N], ()>
to better use the type system. Although it looks like this would result in more copying, I was told that the compiler actually optimizes the proposed new form into the current form. This needs to be verified.After that is done, we should also improve the case where a FFI function fills in the output array. In this case, I guess constructing an array using
[0; N]
and then calling the FFI function is definitely going to cause amemset
to occur before the call to the FFI function. We should probably just usemem::uninitialized
instead, to avoid that memset, in code where the performance of this memset might matter (e.g.ring::aead
andring::digest
).The text was updated successfully, but these errors were encountered: