Skip to content

Commit 14788aa

Browse files
d-v-bclaude
andcommitted
perf: ChunksTuple uses 1D int64 arrays per axis
Switch normalize_chunks_1d to return np.ndarray[tuple[int], np.dtype[np.int64]] instead of tuple[int, ...]. The uniform-chunks branch now constructs in O(1) via np.full, recovering the single-allocation fast path that regressed when the canonical ChunksTuple representation was introduced. Update create_chunk_grid_metadata in v3.py to convert arrays to tuples of ints before passing to is_regular_nd and RectilinearChunkGridMetadata, keeping those downstream functions' signatures unchanged. Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
1 parent f6fc818 commit 14788aa

2 files changed

Lines changed: 19 additions & 13 deletions

File tree

‎src/zarr/core/chunk_grids.py‎

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,8 @@
4343
are not affected by this value.
4444
"""
4545

46-
ChunksTuple = NewType("ChunksTuple", tuple[tuple[int, ...], ...])
47-
"""Normalized chunk specification: one tuple of chunk sizes per dimension.
46+
ChunksTuple = NewType("ChunksTuple", tuple[np.ndarray[tuple[int], np.dtype[np.int64]], ...])
47+
"""Normalized chunk specification: one 1D int64 array of chunk sizes per dimension.
4848
4949
Produced exclusively by `normalize_chunks_nd` and `guess_chunks`.
5050
Consumers should use this type to ensure they receive validated,
@@ -682,9 +682,11 @@ def _guess_regular_chunks(
682682
return tuple(int(x) for x in chunks)
683683

684684

685-
def normalize_chunks_1d(chunks: int | Iterable[int], span: int) -> tuple[int, ...]:
685+
def normalize_chunks_1d(
686+
chunks: int | Iterable[int], span: int
687+
) -> np.ndarray[tuple[int], np.dtype[np.int64]]:
686688
"""
687-
Normalize a one-dimensional chunk specification into a tuple of logical
689+
Normalize a one-dimensional chunk specification into a 1D int64 array of
688690
chunk sizes that cover the span.
689691
690692
`-1` means "one chunk covering the entire span."
@@ -693,14 +695,14 @@ def normalize_chunks_1d(chunks: int | Iterable[int], span: int) -> tuple[int, ..
693695
by the chunk grid at runtime, not by this function.
694696
"""
695697
if chunks == -1:
696-
return (span,)
698+
return np.array([span], dtype=np.int64)
697699
if isinstance(chunks, int):
698700
if chunks <= 0:
699701
raise ValueError(f"Chunk size must be positive, got {chunks}")
700702
if span == 0:
701-
return (chunks,)
703+
return np.array([chunks], dtype=np.int64)
702704
n = ceildiv(span, chunks)
703-
return tuple(chunks for _ in range(n))
705+
return np.full(n, chunks, dtype=np.int64)
704706
else:
705707
chunk_list = list(chunks)
706708
if not chunk_list:
@@ -709,7 +711,7 @@ def normalize_chunks_1d(chunks: int | Iterable[int], span: int) -> tuple[int, ..
709711
raise ValueError(f"All chunk sizes must be positive, got {chunk_list}")
710712
if sum(chunk_list) != span:
711713
raise ValueError(f"Chunk sizes {chunk_list} do not sum to span {span}")
712-
return tuple(chunk_list)
714+
return np.asarray(chunk_list, dtype=np.int64)
713715

714716

715717
def normalize_chunks_nd(
@@ -733,7 +735,7 @@ def normalize_chunks_nd(
733735

734736
# handle no chunking
735737
if chunks is False:
736-
return ChunksTuple(tuple((s,) for s in shape))
738+
return ChunksTuple(tuple(np.array([s], dtype=np.int64) for s in shape))
737739

738740
# handle 1D convenience form
739741
if isinstance(chunks, numbers.Integral):
@@ -853,7 +855,7 @@ def resolve_outer_and_inner_chunks(
853855
return ResolvedChunking(outer_chunks=outer, inner=ResolvedChunking(outer_chunks=chunks))
854856

855857
# Extract the flat chunk shape (first size per dimension) for arithmetic.
856-
chunk_shape_flat = tuple(dim[0] for dim in chunks)
858+
chunk_shape_flat = tuple(int(dim[0]) for dim in chunks)
857859

858860
if shard_shape == "auto":
859861
warnings.warn(

‎src/zarr/core/metadata/v3.py‎

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -411,13 +411,17 @@ def create_chunk_grid_metadata(
411411
--------
412412
parse_chunk_grid : Deserialize a chunk grid from stored JSON metadata.
413413
"""
414-
if is_regular_nd(chunks):
414+
# Convert arrays to tuples of ints for downstream functions that expect Sequence[int].
415+
chunks_as_tuples: tuple[tuple[int, ...], ...] = tuple(
416+
tuple(int(c) for c in dim) for dim in chunks
417+
)
418+
if is_regular_nd(chunks_as_tuples):
415419
# If we know the chunks specification is regular, then we can take the first
416420
# chunk size for each dimension as the chunk shape.
417-
chunk_shape = tuple(dim_chunks[0] for dim_chunks in chunks)
421+
chunk_shape = tuple(dim_chunks[0] for dim_chunks in chunks_as_tuples)
418422
return RegularChunkGridMetadata(chunk_shape=chunk_shape)
419423
else:
420-
return RectilinearChunkGridMetadata(chunk_shapes=chunks)
424+
return RectilinearChunkGridMetadata(chunk_shapes=chunks_as_tuples)
421425

422426

423427
def parse_chunk_grid(

0 commit comments

Comments
 (0)