4646 BasicIndexer ,
4747 ChunkProjection ,
4848 SelectorTuple ,
49- c_order_iter ,
49+ _lexicographic_order ,
50+ colexicographic_order_coords ,
5051 get_indexer ,
51- morton_order_iter ,
52+ lexicographic_order_coords ,
53+ morton_order_coords ,
5254)
5355from zarr .core .metadata .v3 import (
5456 ChunkGridMetadata ,
@@ -261,31 +263,42 @@ def __len__(self) -> int:
261263 return int (self .index .offsets_and_lengths .size / 2 )
262264
263265 def __iter__ (self ) -> Iterator [tuple [int , ...]]:
264- return c_order_iter ( self .index .chunks_per_shard )
266+ return iter ( lexicographic_order_coords ( self .index .chunks_per_shard ) )
265267
266- def to_dict_vectorized (
267- self ,
268- chunk_coords_array : npt .NDArray [np .integer [Any ]],
269- ) -> dict [tuple [int , ...], Buffer | None ]:
268+ def to_dict_vectorized (self ) -> dict [tuple [int , ...], Buffer | None ]:
270269 """Build a dict of chunk coordinates to buffers using vectorized lookup.
271270
272- Parameters
273- ----------
274- chunk_coords_array : ndarray of shape (n_chunks, n_dims)
275- Array of chunk coordinates for vectorized index lookup.
271+ The full per-shard chunk coordinate grid (both the array used for the
272+ vectorized index lookup and the plain tuples used as dict keys) is
273+ cached on `chunks_per_shard`, so neither is rebuilt on every call. For a
274+ shard with tens of thousands of chunks this avoids reconstructing that
275+ many tuples on every partial write.
276276
277277 Returns
278278 -------
279279 dict mapping chunk coordinate tuples to Buffer or None
280280 """
281+ chunks_per_shard = self .index .chunks_per_shard
282+ # The same chunk-grid coordinates are needed in two forms, and neither can
283+ # stand in for the other:
284+ # - `chunk_coords_array`: an (n_chunks, n_dims) numpy array, fed to the
285+ # vectorized index lookup, which does modulo + advanced indexing on it.
286+ # A list of tuples can't be used for that without first being arrayified.
287+ # - `chunk_coords_keys`: the same coordinates as hashable Python tuples,
288+ # used as the result dict's keys. numpy array rows are unhashable
289+ # (mutable), so they can't key a dict.
290+ # Both are cached per shape (see indexing.py), so neither is rebuilt here;
291+ # row i of the array and key i refer to the same chunk.
292+ chunk_coords_array = _lexicographic_order (chunks_per_shard )
293+ chunk_coords_keys = lexicographic_order_coords (chunks_per_shard )
281294 starts , ends , valid = self .index .get_chunk_slices_vectorized (chunk_coords_array )
282295
283296 result : dict [tuple [int , ...], Buffer | None ] = {}
284- for i , coords in enumerate (chunk_coords_array ):
297+ for i , coords in enumerate (chunk_coords_keys ):
285298 if valid [i ]:
286- result [tuple ( coords . ravel ()) ] = self .buf [int (starts [i ]) : int (ends [i ])]
299+ result [coords ] = self .buf [int (starts [i ]) : int (ends [i ])]
287300 else :
288- result [tuple ( coords . ravel ()) ] = None
301+ result [coords ] = None
289302
290303 return result
291304
@@ -533,13 +546,14 @@ async def _decode_partial_single(
533546 def _subchunk_order_iter (
534547 self , chunks_per_shard : tuple [int , ...], subchunk_write_order : SubchunkWriteOrder
535548 ) -> Iterable [tuple [int , ...]]:
549+ subchunk_iter : Iterable [tuple [int , ...]]
536550 match subchunk_write_order :
537551 case "morton" :
538- subchunk_iter = morton_order_iter (chunks_per_shard )
552+ subchunk_iter = morton_order_coords (chunks_per_shard )
539553 case "lexicographic" :
540- subchunk_iter = np . ndindex (chunks_per_shard )
554+ subchunk_iter = lexicographic_order_coords (chunks_per_shard )
541555 case "colexicographic" :
542- subchunk_iter = ( c [:: - 1 ] for c in np . ndindex ( chunks_per_shard [:: - 1 ]) )
556+ subchunk_iter = colexicographic_order_coords ( chunks_per_shard )
543557 case "unordered" :
544558 # "unordered" promises no particular layout; today it happens to be
545559 # lexicographic, but callers must not rely on that.
@@ -565,9 +579,7 @@ async def _encode_single(
565579 chunk_grid = ChunkGrid .from_sizes (shard_shape , chunk_shape ),
566580 )
567581 )
568- # The key order of this intermediate dict is immaterial; the physical layout is
569- # decided later by the `subchunk_write_order` loop in `_encode_shard_dict`.
570- shard_builder = dict .fromkeys (np .ndindex (chunks_per_shard ))
582+ shard_builder = dict .fromkeys (lexicographic_order_coords (chunks_per_shard ))
571583
572584 await self .codec_pipeline .write (
573585 [
@@ -610,19 +622,18 @@ async def _encode_partial_single(
610622 )
611623
612624 if self ._is_complete_shard_write (indexer , chunks_per_shard ):
613- # Intermediate key order is immaterial (see `_encode_single`).
614- shard_dict = dict .fromkeys (np .ndindex (chunks_per_shard ))
625+ shard_dict = dict .fromkeys (lexicographic_order_coords (chunks_per_shard ))
615626 else :
616627 shard_reader = await self ._load_full_shard_maybe (
617628 byte_getter = byte_setter ,
618629 prototype = chunk_spec .prototype ,
619630 chunks_per_shard = chunks_per_shard ,
620631 )
621632 shard_reader = shard_reader or _ShardReader .create_empty (chunks_per_shard )
622- # Use vectorized lookup for better performance
623- shard_dict = shard_reader . to_dict_vectorized (
624- np . array ( list ( np . ndindex ( chunks_per_shard )))
625- )
633+ # Use vectorized lookup for better performance. The lexicographic
634+ # coordinate array and keys are cached, so neither is rebuilt on
635+ # every write.
636+ shard_dict = shard_reader . to_dict_vectorized ( )
626637
627638 await self .codec_pipeline .write (
628639 [
@@ -692,9 +703,13 @@ async def _encode_shard_dict(
692703 def _is_total_shard (
693704 self , all_chunk_coords : set [tuple [int , ...]], chunks_per_shard : tuple [int , ...]
694705 ) -> bool :
695- return len (all_chunk_coords ) == product (chunks_per_shard ) and all (
696- chunk_coords in all_chunk_coords for chunk_coords in c_order_iter (chunks_per_shard )
697- )
706+ # `all_chunk_coords` comes from an indexer over this shard's chunk grid, so
707+ # it is always a subset of that grid (`validate` requires the shard shape to
708+ # be divisible by the inner chunk shape, so the indexer cannot produce an
709+ # out-of-grid coordinate). A subset whose size equals the grid's is the
710+ # whole grid, so the count check alone proves totality — no need to build
711+ # and membership-test the full coordinate set on this hot path.
712+ return len (all_chunk_coords ) == product (chunks_per_shard )
698713
699714 def _is_complete_shard_write (
700715 self ,
0 commit comments