Skip to content
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

ENH: avoid unnecessary array copies through np.ndarray.astype where possible #4496

Merged

Conversation

neutrinoceros
Copy link
Member

PR Summary

np.ndarray.astype returns a copy of an array by default. In many cases, copies can be avoided by using the target datatype from the start.
This is a thorough, yet conservative refactor: I've stayed away from cases where precision was truncated after a calculation, so as to not introduce new floating point errors.

Additionally, I've changed a couple places where we were casting to "int" or int to use "int64" instead, which is identical on POSIX platform but improve consistency on Windows, where astype("int") might be equivalent to astype("int32") (I couldn't find a reference to back this up, so I may be misremebering).

@neutrinoceros neutrinoceros added enhancement Making something better performance labels Jun 10, 2023
Copy link
Member

@matthewturk matthewturk left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This requires some careful review which I intend to give it. Thank you!

@@ -153,7 +153,7 @@ def _get_cut_mask(self, grid):
for mi, (i, pos) in enumerate(zip(pids, self.positions[points_in_grid])):
if not points_in_grid[i]:
continue
ci = ((pos - grid.LeftEdge) / grid.dds).astype("int")
ci = ((pos - grid.LeftEdge) / grid.dds).astype("int64")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This particular one doesn't necessarily need promotion to int64 (although I recognize numpy may do it internally) because it's a relative offset.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this doens't change behaviour, I'm just making the size explicit rather than implicit.

@neutrinoceros
Copy link
Member Author

No need to review this before I'm done stabilising tests, but thank you for showing interest in it !

@neutrinoceros neutrinoceros force-pushed the fewer_unecessary_array_copies branch 4 times, most recently from 33b3c1b to 949cd3e Compare June 10, 2023 17:53
@neutrinoceros neutrinoceros marked this pull request as ready for review June 10, 2023 18:43
@neutrinoceros neutrinoceros marked this pull request as draft June 11, 2023 06:20
@neutrinoceros neutrinoceros marked this pull request as ready for review June 11, 2023 09:05
@neutrinoceros
Copy link
Member Author

force-pushed to resolve merge conflicts

@matthewturk
Copy link
Member

I think this looks good, and I've read through it and think it's fine. I would like another pass at it before it gets merged, but I think it's on the right track.

@neutrinoceros
Copy link
Member Author

@yt-fido test this please

@neutrinoceros
Copy link
Member Author

@yt-fido test this please

@neutrinoceros
Copy link
Member Author

@matthewturk any chance you could have a look soon ?

Copy link
Member

@matthewturk matthewturk left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, and I don't see any behavior changes to worry about.

@@ -128,7 +128,7 @@ def check_tree(self):
nre = self.ds.arr(node.get_right_edge(), units="code_length")
li = np.rint((nle - gle) / dds).astype("int32")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

outside of scope for this PR but my eyebrows raised a little seeing this casting to int32 instead of int64.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah, fortunately these variables don't seem to be actually used (except for that one assert np.all(dims > 0) which is runtime-skippable)

@neutrinoceros neutrinoceros added this to the 4.3.0 milestone Sep 12, 2023
@neutrinoceros neutrinoceros merged commit 83c9621 into yt-project:main Sep 12, 2023
10 checks passed
@neutrinoceros neutrinoceros deleted the fewer_unecessary_array_copies branch September 12, 2023 10:09
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement Making something better performance
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants