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

Some alignment optimizations #7382

Merged
merged 6 commits into from
Jan 5, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 27 additions & 2 deletions xarray/core/alignment.py
Original file line number Diff line number Diff line change
Expand Up @@ -343,8 +343,33 @@ def _need_reindex(self, dims, cmp_indexes) -> bool:
pandas). This is useful, e.g., for overwriting such duplicate indexes.

"""
has_unindexed_dims = any(dim in self.unindexed_dim_sizes for dim in dims)
return not (indexes_all_equal(cmp_indexes)) or has_unindexed_dims
if not indexes_all_equal(cmp_indexes):
# always reindex when matching indexes are not equal
return True

unindexed_dims_sizes = {}
for dim in dims:
if dim in self.unindexed_dim_sizes:
sizes = self.unindexed_dim_sizes[dim]
if len(sizes) > 1:
# reindex if different sizes are found for unindexed dims
return True
else:
unindexed_dims_sizes[dim] = next(iter(sizes))

if unindexed_dims_sizes:
indexed_dims_sizes = {}
for cmp in cmp_indexes:
index_vars = cmp[1]
for var in index_vars.values():
indexed_dims_sizes.update(var.sizes)

for dim, size in unindexed_dims_sizes.items():
if indexed_dims_sizes.get(dim, -1) != size:
# reindex if unindexed dimension size doesn't match
return True

return False

def _get_index_joiner(self, index_cls) -> Callable:
if self.join in ["outer", "inner"]:
Expand Down
5 changes: 5 additions & 0 deletions xarray/core/indexes.py
Original file line number Diff line number Diff line change
Expand Up @@ -1419,6 +1419,11 @@ def check_variables():
)

indexes = [e[0] for e in elements]

same_objects = all(indexes[0] is other_idx for other_idx in indexes[1:])
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
same_objects = all(indexes[0] is other_idx for other_idx in indexes[1:])
indexes_0 = indexes[0]
same_objects = all(indexes_0 is other_idx for other_idx in indexes[1:])

No need to use getitem several times for the same value. Similar thing can be done in other places in the function as well.

Copy link
Collaborator

Choose a reason for hiding this comment

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

List indexing is O(1), so should be only a marginal speedup :P

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 I think the gain in perf would be negligible.

if same_objects:
return True

same_type = all(type(indexes[0]) is type(other_idx) for other_idx in indexes[1:])
if same_type:
try:
Expand Down