Skip to content

Commit f674236

Browse files
authored
(feat): use np.zeros for buffer creation with fill_value=0 (#3082)
* (feat): use `np.zeros` for buffer creation when possible * (fix): add comment and check * (chore): relnote
1 parent 481550a commit f674236

File tree

2 files changed

+3
-1
lines changed

2 files changed

+3
-1
lines changed

changes/3082.feature.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
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`.

src/zarr/core/buffer/cpu.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,8 @@ def create(
154154
order: Literal["C", "F"] = "C",
155155
fill_value: Any | None = None,
156156
) -> Self:
157-
if fill_value is None:
157+
# np.zeros is much faster than np.full, and therefore using it when possible is better.
158+
if fill_value is None or (isinstance(fill_value, int) and fill_value == 0):
158159
return cls(np.zeros(shape=tuple(shape), dtype=dtype, order=order))
159160
else:
160161
return cls(np.full(shape=tuple(shape), fill_value=fill_value, dtype=dtype, order=order))

0 commit comments

Comments
 (0)