Skip to content

add numpy array of dask keys to Array - #7922

Merged
jsignell merged 7 commits into
dask:mainfrom
d-v-b:cached-array-of-keys
Oct 11, 2021
Merged

jsignell merged 7 commits into
dask:mainfrom
d-v-b:cached-array-of-keys

Conversation

@d-v-b

@d-v-b d-v-b commented Jul 21, 2021 •

Copy link
Copy Markdown
Member

This PR adds the cached key_array property to the Array class. This property is just a cached numpy array of the Array.__dask_keys__() property with dtype=object. Adding this as a property of the Array class allows me to remove expensive numpy array creation in Array._blocks and thereby make iterating over Array.blocks more performant.

I benchmarked iteration over Array.blocks with the following code:

import timeit
result = timeit.timeit('[d for d in data.blocks]', setup='import dask.array as da; data = da.zeros((5000,), chunks=(1,))', number=3)
print(result)

Running this benchmark on main takes 17.60s; with the changes in this PR, the benchmark takes 4.58s.

As mentioned in #7832, another solution to this problem would be to implement the numpy array slicing interface for the Array.__dask_keys__ property. This might be a better approach (I don't like adding properties just to enhance performance of one method), but it would be much more work and require more test coverage (unless there's an existing test suite for "adapt numpy array slicing to lists-of-lists"). Additionally, the approach I took in this PR will organically track any changes in the numpy array slicing interface. But maybe that interface changes too slowly for this to be valuable.

edit: update that tests were added

@github-actions github-actions Bot added the array label Jul 21, 2021

@jsignell jsignell left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This approach seems good, but I think you will want to explicitly reset the cache when name or the chunks changes. There is a method called _reset_cache for that.

Comment thread dask/array/core.py Resolved
@d-v-b

d-v-b commented Jul 21, 2021

Copy link
Copy Markdown
Member Author

There weren't any tests ensuring that these cached array properties get reset after a change of chunks, so I added one.

@jsignell jsignell left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This is looking really nice! My last comments are:

  • maybe we should prefix the property with an underscore to denote that it's private
  • I still think that when the name is set the key_array needs to be reset. Here:

    dask/dask/array/core.py

    Lines 1486 to 1490 in 56d1891

    @_name.setter
    def _name(self, val):
    self.__name = val
    # Clear the key cache when the name is reset
    self._cached_keys = None

@d-v-b

d-v-b commented Jul 22, 2021

Copy link
Copy Markdown
Member Author

I still need to add tests for the cache invalidation after name change.

@jsignell

jsignell commented Oct 5, 2021

Copy link
Copy Markdown
Member

It seems like this is just missing a few tests @d-v-b when you get a chance :)

@d-v-b
d-v-b force-pushed the cached-array-of-keys branch from 99fd20f to adcf354 Compare October 8, 2021 16:30
@d-v-b

d-v-b commented Oct 8, 2021

Copy link
Copy Markdown
Member Author

@jsignell thanks for the nudge, I added the missing test :)

@jsignell jsignell left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Thanks @d-v-b this looks great!

@jsignell
jsignell merged commit b407597 into dask:main Oct 11, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants