Skip dropping array elements that don't need drop #802
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.
While this sounds tautological; to be careful, set the vector length to
zero before dropping the vector of the internal storage of an array.
In some places in ndarray where A: Copy (hence the element does not need drop) we use uninitialized elements in vectors. Setting the length to 0 avoids that the vector tries to drop, slice or otherwise produce values of these elements. (The details of the validity letting this happen with nonzero len, are under discussion as of this writing.)
Related to #796, specifically around details of
Array::uninitialized
safety.The argumentation used is that we want the following to be safe:
We want this to be safe even if the array has not been filled - initialized.
Under the hood we have the following operations when the array is created and dropped:
Out of this sequence, only the
drop_in_place
seems problematic, and setting the vector length to 0 will avoid it. Of course, there aren't complete guarantees about Vec's behavior, but with this change we avoid a problem that has been explicitly flagged. Previously discussed in #685.