Skip to content

Commit 530e88b

Browse files
committed
don't use global lru_cache
1 parent 6d00972 commit 530e88b

File tree

1 file changed

+5
-3
lines changed

1 file changed

+5
-3
lines changed

src/zarr/codecs/sharding.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -333,6 +333,11 @@ def __init__(
333333
object.__setattr__(self, "index_codecs", index_codecs_parsed)
334334
object.__setattr__(self, "index_location", index_location_parsed)
335335

336+
# Use instance-local lru_cache to avoid memory leaks
337+
object.__setattr__(self, "_get_chunk_spec", lru_cache()(self._get_chunk_spec))
338+
object.__setattr__(self, "_get_index_chunk_spec", lru_cache()(self._get_index_chunk_spec))
339+
object.__setattr__(self, "_get_chunks_per_shard", lru_cache()(self._get_chunks_per_shard))
340+
336341
@classmethod
337342
def from_dict(cls, data: Dict[str, JSON]) -> Self:
338343
_, configuration_parsed = parse_named_configuration(data, "sharding_indexed")
@@ -609,7 +614,6 @@ def _shard_index_size(self, chunks_per_shard: ChunkCoords) -> int:
609614
16 * product(chunks_per_shard), self._get_index_chunk_spec(chunks_per_shard)
610615
)
611616

612-
@lru_cache
613617
def _get_index_chunk_spec(self, chunks_per_shard: ChunkCoords) -> ArraySpec:
614618
return ArraySpec(
615619
shape=chunks_per_shard + (2,),
@@ -618,7 +622,6 @@ def _get_index_chunk_spec(self, chunks_per_shard: ChunkCoords) -> ArraySpec:
618622
order="C", # Note: this is hard-coded for simplicity -- it is not surfaced into user code
619623
)
620624

621-
@lru_cache
622625
def _get_chunk_spec(self, shard_spec: ArraySpec) -> ArraySpec:
623626
return ArraySpec(
624627
shape=self.chunk_shape,
@@ -627,7 +630,6 @@ def _get_chunk_spec(self, shard_spec: ArraySpec) -> ArraySpec:
627630
order=shard_spec.order,
628631
)
629632

630-
@lru_cache
631633
def _get_chunks_per_shard(self, shard_spec: ArraySpec) -> ChunkCoords:
632634
return tuple(
633635
s // c

0 commit comments

Comments
 (0)