Skip to content

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.

@codecov
Copy link

codecov bot commented Oct 1, 2025

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 94.49%. Comparing base (a7080a3) to head (b997202).
⚠️ Report is 127 commits behind head on main.

Additional details and impacted files
@@           Coverage Diff           @@
##             main    #3191   +/-   ##
=======================================
  Coverage   94.48%   94.49%           
=======================================
  Files          78       78           
  Lines        8609     8623   +14     
=======================================
+ Hits         8134     8148   +14     
  Misses        475      475           
Files with missing lines Coverage Δ
src/zarr/core/buffer/core.py 82.57% <100.00%> (+0.40%) ⬆️
src/zarr/core/buffer/cpu.py 100.00% <100.00%> (ø)
src/zarr/core/buffer/gpu.py 89.87% <100.00%> (+0.39%) ⬆️
src/zarr/testing/buffer.py 100.00% <100.00%> (ø)
src/zarr/testing/utils.py 91.66% <100.00%> (+0.75%) ⬆️
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

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