Skip to content

Conversation

@jorenham
Copy link
Member

This adds __slots__ to the (private) classes in numpy.lib._index_tricks_impl and their subclasses, improving the performance of:

  • numpy.mgrid
  • numpy.ogrid
  • numpy.r_
  • numpy.c_
  • numpy.index_exp
  • numpy.s_
  • numpy.ma.mr_

Note

I initially wanted to add __slots__ to ndenumerate and ndindex as well, but decided not to, as there's a (small) chance that it will be backwards compatible.:
Because they are part of the public API, and not marked as @final, it's possible that > they're used as base-class for user-defined types. And while that, on its own, is not a problem here, it could become one if such a user-defined type has more than one base class.

But if this isn't a problem, I'd be happy to add __slots__ to these classes, too :)

@eendebakpt
Copy link
Contributor

@jorenham Do you have some benchmarks for the methods affected by these changes? I suspect this will mostly impact operations involving small arrays

@jorenham jorenham force-pushed the index_tricks-__slots__ branch from 5619df9 to 240e828 Compare July 21, 2024 04:27
@jorenham
Copy link
Member Author

jorenham commented Jul 21, 2024

@eendebakpt A quick %timeit comparison on Python 3.12 gave the following results:

%timeit numpy/numpy@main jorenham/numpy@index_tricks-__slots__ Speedup
np.mgrid[0:2] 3.74 μs ± 10.1 ns 3.18 μs ± 7.03 ns + 17.6 %
np.mgrid[0:1000] 4.78 μs ± 7.69 ns 4.12 μs ± 10.4 ns + 16.0 %
np.mgrid[0:2, 0:2] 23.1 μs ± 49.3 ns 20.4 μs ± 43.3 ns + 14.9 %
np.r_[0, 1] 6.65 μs ± 9.88 ns 5.61 μs ± 8.72 ns + 18.5 %
np.r_[0, 1, 2, 3] 8.34 μs ± 8.4 ns 7.15 μs ± 15 ns + 16.6 %
np.r_[[[0]], [[1]]] 20.1 μs ± 31 ns 17.5 μs ± 30.3 ns + 14.9 %
np.s_[2::2] 95.4 ns ± 0.0396 ns 45.7 ns ± 0.0559 ns + 108 %

The remaining ogrid, c_/ma.mr_, and index_exp should have similar results as mgrid, r_, and s_, respectively.

Copy link
Contributor

@eendebakpt eendebakpt left a comment

Choose a reason for hiding this comment

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

Thanks for the benchmarks, looks like a nice improvement with no regressions.

Normally we leave whitespace fixes out of the PR to make reviews easier, but it is ok to keep them in afaic.

@mattip mattip merged commit 5f70a25 into numpy:main Jul 22, 2024
@mattip
Copy link
Member

mattip commented Jul 22, 2024

Thanks @jorenham

@jorenham jorenham deleted the index_tricks-__slots__ branch July 22, 2024 12:23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants