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: ~40x speedup in sparse init and ops by using numpy in check_integrity #24985

Merged
merged 1 commit into from
Jan 30, 2019
Merged
Show file tree
Hide file tree
Changes from all 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
9 changes: 8 additions & 1 deletion doc/source/whatsnew/v0.25.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,13 @@ Other Enhancements
-
-

.. _whatsnew_0250.performance:

Performance Improvements
~~~~~~~~~~~~~~~~~~~~~~~~
- Significant speedup in `SparseArray` initialization that benefits most operations, fixing performance regression introduced in v0.20.0 (:issue:`24985`)



.. _whatsnew_0250.api_breaking:

Expand Down Expand Up @@ -187,7 +194,7 @@ Reshaping
Sparse
^^^^^^

-
- Significant speedup in `SparseArray` initialization that benefits most operations, fixing performance regression introduced in v0.20.0 (:issue:`24985`)
-
-

Expand Down
15 changes: 5 additions & 10 deletions pandas/_libs/sparse.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,6 @@ cdef class IntIndex(SparseIndex):
A ValueError is raised if any of these conditions is violated.
"""

cdef:
int32_t index, prev = -1

if self.npoints > self.length:
msg = ("Too many indices. Expected "
"{exp} but found {act}").format(
Expand All @@ -86,17 +83,15 @@ cdef class IntIndex(SparseIndex):
if self.npoints == 0:
return

if min(self.indices) < 0:
if self.indices.min() < 0:
raise ValueError("No index can be less than zero")

if max(self.indices) >= self.length:
if self.indices.max() >= self.length:
raise ValueError("All indices must be less than the length")

for index in self.indices:
Copy link
Member

Choose a reason for hiding this comment

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

The buffer access may need to be optimized, but in principle this should be faster in many cases because it can short-circuit.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It will only short-circuit in the case of an integrity failure, which should be the minority of cases.

if prev != -1 and index <= prev:
raise ValueError("Indices must be strictly increasing")

prev = index
monotonic = np.all(self.indices[:-1] < self.indices[1:])
if not monotonic:
raise ValueError("Indices must be strictly increasing")

def equals(self, other):
if not isinstance(other, IntIndex):
Expand Down