Skip to content

Support uninitialized elements in .assign() and .fill() #798

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

Closed
wants to merge 1 commit into from

Conversation

bluss
Copy link
Member

@bluss bluss commented Apr 13, 2020

The trick is simply to traverse the target array using a raw view / raw
pointers; this means we don't create any references to
potentially-uninitialized values.

Assignment *x = y.clone() is used where x is a *mut A, y is a &A;
this will work as intended when A: Copy; assignment should not need that
the value being overwritten, when Copy, is initialized.

Unfortunately we use Zip here instead of the older methods
(zip_mut_with, unordered_foreach_mut); because Zip implements equivalent
functionality but in a generic way. The access pattern and performance
is not guaranteed to be identical.

Related to issue #685

The trick is simply to traverse the target array using a raw view / raw
pointers; this means we don't create any references to
potentially-uninitialized values.

Assignment `*x = y.clone()` is used where x is a *mut A, y is a &A;
this will work as intended when A: Copy; assignment should not need that
the value being overwritten, when Copy, is initialized.

Unfortunately we use Zip here instead of the older methods
(zip_mut_with, unordered_foreach_mut); because Zip implemnets equivalent
functionality but in a generic way. The access pattern and performance
is not guaranteed to be identical.
@bluss
Copy link
Member Author

bluss commented Apr 13, 2020

Benchmarks suggest .fill() is unchanged. Looks like we don't have an assign benchmark.

@bluss
Copy link
Member Author

bluss commented Apr 13, 2020

Source to back us up on that assignment through *x on raw pointer will have the intended effect https://old.reddit.com/r/rust/comments/cefgec/howto_replace_memuninitialized_with_memmaybeuninit/eu87689/

@bluss bluss marked this pull request as draft April 15, 2020 22:18
@bluss
Copy link
Member Author

bluss commented Apr 23, 2020

Thoughts for next time: Rethink - we introduced maybe_uninit instead. But is this still worth doing? Maybe, but need to include new trait AssignElem in the design. Additional trait for overwriting?

@bluss bluss closed this Apr 23, 2020
@bluss bluss deleted the assign-fill-uninit branch April 23, 2020 20:20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant