Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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
Reduce uses of uninitialized arrays #3261
Reduce uses of uninitialized arrays #3261
Changes from 5 commits
8ceb4e9
9f5657d
ce8281f
13e98f2
36da899
9b7cb1e
File filter
Filter by extension
Conversations
Jump to
There are no files selected for viewing
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If all the changes in this PR were like this file, I would approve it in a heartbeat.
This is the pattern we should be following to avoid undefined behavior and double initialization.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't understand the comment. Does that mean other files than inverse.rs are not acceptable? What do I need to change?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
BTW, I've wanted to use a pattern of:
but that required reliably tracking the length inside the function, which would regress performance. This lead me to try some loop optimizations like #3262 but I found it wasn't always possible to have guaranteed length for free. Plus there's A LOT of uninitialized to deal with.
There are more functions using which I haven't converted yet, because I'm not actually sure they fully initialize their data!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Firstly, thank you for detailing the current sites of potential undefined behavior. Everything that maintains the current intent can be merged. Any changes that replace
uninitialized()
with zero-fill should be dropped and can be addressed in a follow-up PR.I have recently tackled this issue in a toy project:
https://github.com/barrbrain/sudoku/blob/main/src/lib.rs#L27-L40
Some care is required with ownership semantics when crossing the
unsafe
boundary. This pattern is well defined:https://github.com/barrbrain/sudoku/blob/main/src/lib.rs#L355-L356
I will see if we can add some infrastructure to ease converting the current code to well-defined.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Once
MaybeUninit::uninit_array
hits stable I guess a lot would be easier.Probably we could change some
Vec
in Box<[]> to make more evident that the size is fixed meanwhile.(also, thank you again for taking care of this :))
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The only cases of zero-fill (I assume you mean
from_fn(|| 0)
) are in benchmarks, outside of the benchmarked section. I assume they're not important, since they're neither in the library, nor performance-critical. So I've just changed them for simplicity and determinism.