Skip to content

Add NDBuffer.empty #3191

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
Jul 3, 2025
Merged

Conversation

TomAugspurger
Copy link
Contributor

In #2904, I've found that np.empty(...) can be quite a bit faster than the np.full we use in places
(

. Though note that np.empty and np.zeros are about the same for the cases where we use that.)

For the common case of chunk-aligned reads, the memset used by np.full or np.zeros should be unnecessary, because the codec pipeline will overwrite the memory anyway (or overwritten with fill_value if the store is missing the key).

This adds a new method NDBuffer.empty, mirroring np.empty. To preserve backwards compatibility, it's not abstract. It delegates to the less efficient NDBuffer.create. But I've implemented it for our gpu and cpu buffers.

@github-actions github-actions bot added the needs release notes Automatically applied to PRs which haven't added release notes label Jul 1, 2025
@github-actions github-actions bot removed the needs release notes Automatically applied to PRs which haven't added release notes label Jul 1, 2025
@TomAugspurger TomAugspurger marked this pull request as ready for review July 1, 2025 20:18
In zarr-developers#2904, I've
found that `np.empty(...)` can be quite a bit faster than the `np.full`
we use in places
(https://github.com/zarr-developers/zarr-python/blob/baabf08d07e8518e3d37bd83c493a1d46ea7ac1d/src/zarr/core/buffer/cpu.py#L149.
Though note that `np.empty` and `np.zeros` are about the same for the
cases where we use that.)

For the common case of chunk-aligned reads, the memset used by `np.full`
or `np.zeros` should be unnecessary, because the codec pipeline will
overwrite the memory anyway (or overwritten with `fill_value` if the
store is missing the key).

This adds a new method `NDBuffer.empty`, mirroring `np.empty`. To
preserve backwards compatibility, it's *not* abstract. It delegates to
the less efficient `NDBuffer.create`. But I've implemented it for our
`gpu` and `cpu` buffers.
dtype: npt.DTypeLike,
order: Literal["C", "F"] = "C",
) -> Self:
return super(cpu.NDBuffer, cls).empty(shape=shape, dtype=dtype, order=order)
Copy link
Contributor Author

@TomAugspurger TomAugspurger Jul 1, 2025

Choose a reason for hiding this comment

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

This is to test the base core.NDBuffer implementation. This class inherits from cpu.NDBuffer so we use super(cpu.NDBuffer, cls) to get its parent's impelentation (the base class).

Copy link
Contributor

@d-v-b d-v-b left a comment

Choose a reason for hiding this comment

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

This looks good to me! Thanks Tom.

@d-v-b d-v-b merged commit 9da38f7 into zarr-developers:main Jul 3, 2025
30 checks passed
@TomAugspurger TomAugspurger deleted the tom/ndarray-empty branch July 16, 2025 16:13
@TomAugspurger
Copy link
Contributor Author

Just a note: this might cause issues for people running the StoreTests. We needed rapidsai/kvikio@bdca513 in kvikio to avoid an error from pytest about an unregistered marker.

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