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

PERF-#6876: Skip the masking stage on 'iloc' where beneficial #6878

Merged
merged 2 commits into from
Jan 24, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
53 changes: 44 additions & 9 deletions modin/core/dataframe/pandas/dataframe/dataframe.py
Original file line number Diff line number Diff line change
Expand Up @@ -1161,12 +1161,22 @@
extra_log="Mask takes only list-like numeric indexers, "
+ f"received: {type(indexer)}",
)
if isinstance(indexer, list):
indexer = np.array(indexer, dtype=np.int64)
indexers.append(indexer)
row_positions, col_positions = indexers

if col_positions is None and row_positions is None:
return self.copy()

# quite fast check that allows skip sorting
must_sort_row_pos = row_positions is not None and not np.all(
row_positions[1:] >= row_positions[:-1]
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

this check was copied from _maybe_reorder_labels and it indeed works pretty fast:

import numpy as np
from timeit import default_timer as timer

arr = np.random.randint(0, 100_000_000, size=100_000_000)

t1 = timer()
must_sort_col_pos = np.all(arr[1:] >= arr[:-1])
print(timer() - t1) # 0.09s

)
must_sort_col_pos = col_positions is not None and not np.all(
col_positions[1:] >= col_positions[:-1]
)

if col_positions is None and row_positions is not None:
# Check if the optimization that first takes part of the data using the mask
# operation so that later less data is concatenated into a whole column is useful.
Expand All @@ -1175,18 +1185,40 @@
all_rows = None
if self.has_materialized_index:
all_rows = len(self.index)
elif self._row_lengths_cache:
elif self._row_lengths_cache or must_sort_row_pos:

Check warning on line 1188 in modin/core/dataframe/pandas/dataframe/dataframe.py

View check run for this annotation

Codecov / codecov/patch

modin/core/dataframe/pandas/dataframe/dataframe.py#L1188

Added line #L1188 was not covered by tests
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

we will have to trigger row_lengths anyway in that case

all_rows = sum(self._row_lengths_cache)
dchigarev marked this conversation as resolved.
Show resolved Hide resolved
if all_rows:
if len(row_positions) > 0.9 * all_rows:
return self._reorder_labels(
row_positions=row_positions, col_positions=col_positions
)

# 'base_num_cols' specifies the number of columns that the dataframe should have
# in order to jump to 'reordered_labels' in case of len(row_positions) / len(self) >= base_ratio;
# these variables may be a subject to change in order to tune performance more accurately
base_num_cols = 10
base_ratio = 0.2
# Example:
# len(self.columns): 10 == base_num_cols -> min ratio to jump to reorder_labels: 0.2 == base_ratio
# len(self.columns): 15 -> min ratio to jump to reorder_labels: 0.3
# len(self.columns): 20 -> min ratio to jump to reorder_labels: 0.4
# ...
# len(self.columns): 49 -> min ratio to jump to reorder_labels: 0.98
# len(self.columns): 50 -> min ratio to jump to reorder_labels: 1.0
# len(self.columns): 55 -> min ratio to jump to reorder_labels: 1.0
# ...
if (all_rows and len(row_positions) > 0.9 * all_rows) or (
must_sort_row_pos
and len(row_positions) * base_num_cols
>= min(
all_rows * len(self.columns) * base_ratio,
Copy link
Collaborator

Choose a reason for hiding this comment

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

Is it performance-wise safe to materialize columns here? (Are they already materializing somewhere nearby or not?)

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

no, they're not materialized explicitly anywhere nearby, but this value is required to properly branch here, so I guess I have no good choices here

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

my assumption is that columns are more likely to be pre-computed than indices, so accessing them shouldn't always trigger computations

Copy link
Collaborator

Choose a reason for hiding this comment

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

Ok, we can try to use self.column_widths in the future if needed

len(row_positions) * base_num_cols,
)
):
return self._reorder_labels(
row_positions=row_positions, col_positions=col_positions
)
sorted_row_positions = sorted_col_positions = None

if row_positions is not None:
sorted_row_positions = self._get_sorted_positions(row_positions)
if must_sort_row_pos:
sorted_row_positions = self._get_sorted_positions(row_positions)
else:
sorted_row_positions = row_positions
# Get dict of row_parts as {row_index: row_internal_indices}
row_partitions_dict = self._get_dict_of_block_index(
0, sorted_row_positions, are_indices_sorted=True
Expand All @@ -1201,7 +1233,10 @@
new_index = self.copy_index_cache(copy_lengths=True)

if col_positions is not None:
sorted_col_positions = self._get_sorted_positions(col_positions)
if must_sort_col_pos:
sorted_col_positions = self._get_sorted_positions(col_positions)
else:
sorted_col_positions = col_positions
# Get dict of col_parts as {col_index: col_internal_indices}
col_partitions_dict = self._get_dict_of_block_index(
1, sorted_col_positions, are_indices_sorted=True
Expand Down