Avoid unneeded copies from flatten(). #13381
Merged
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.
flatten() always returns a new copy, but nearly all places that use
flatten() in the codebase don't need to pay that cost; the only place
that needed it did an additional copy. Fix them.
(To be honest it's not clear this will make a measurable difference, but heh.)
As a replacement, there's ravel(), which only makes a copy if the input
is not C-contiguous to start with, and reshape(..., -1) which never
makes a copy (as long as the input is already an ndarray), so prefer the
latter.
Note that in the changed snippet in mplot3d, the AttributeError would
never be raised, as x/y/z are converted to arrays and thus always have a
flatten() method.
PR Summary
PR Checklist