Skip to content

Small buffer optimization for InvertedAxes #275

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

Merged
merged 2 commits into from
Feb 15, 2022
Merged

Conversation

adamreichold
Copy link
Member

Since InvertedAxes is basically a set of small integers, we can avoid dynamic allocations by using a word-sized bitmap in the common case of up to 32 axes.

This should also not unduly increase code slice as there is no dynamic spilling into the large representation, but the decision is rather taken once based on the dimensionality of the array.

Copy link
Member

@davidhewitt davidhewitt left a comment

Choose a reason for hiding this comment

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

👍 looks like a nice optimisation to me!

src/array.rs Outdated
if len < 32 {
Self::Short(0)
} else {
Self::Long(Vec::new())
Copy link
Member

Choose a reason for hiding this comment

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

Is it worth perhaps adding a test with a 40-dimension array, to exercise this code path?

Copy link
Member Author

Choose a reason for hiding this comment

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

I tried and actually can't as NumPy seems to be limited to at most 32 dimensions:

In [12]: a = np.zeros(sum(((1,) for _ in range(32)), ()) + (3,))
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
Input In [12], in <module>
----> 1 a = np.zeros(sum(((1,) for _ in range(32)), ()) + (3,))

ValueError: maximum supported dimension for an ndarray is 32, found 33

So it would seem that I do not need the Long representation at all?

Since InvertedAxes is basically a set of small integers, we can avoid dynamic
allocations by using a word-sized bitmap in the common case of up to 32 axes.

This should also not unduly increase code slice as there is no dynamic spilling
into the large representation, but the decision is rather taken once based on
the dimensionality of the array.
…r InvertedAxes as NumPy does not support it in the first place.
Copy link
Member

@davidhewitt davidhewitt left a comment

Choose a reason for hiding this comment

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

Haha even better to find only 32 dims supported!

@adamreichold adamreichold merged commit 61882e3 into main Feb 15, 2022
@adamreichold adamreichold deleted the sbo-inverted-axes branch February 15, 2022 08:15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants