Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changes/3082.feature.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Use :py:func:`numpy.zeros` instead of :py:func:`np.full` for a performance speedup when creating a `zarr.core.buffer.NDBuffer` with `fill_value=0`.
3 changes: 2 additions & 1 deletion src/zarr/core/buffer/cpu.py
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,8 @@
order: Literal["C", "F"] = "C",
fill_value: Any | None = None,
) -> Self:
if fill_value is None:
# np.zeros is much faster than np.full, and therefore using it when possible is better.
if fill_value is None or (isinstance(fill_value, int) and fill_value == 0):

Check warning on line 158 in src/zarr/core/buffer/cpu.py

View check run for this annotation

Codecov / codecov/patch

src/zarr/core/buffer/cpu.py#L158

Added line #L158 was not covered by tests
return cls(np.zeros(shape=tuple(shape), dtype=dtype, order=order))
else:
return cls(np.full(shape=tuple(shape), fill_value=fill_value, dtype=dtype, order=order))
Expand Down