Skip to content

Commit 4e2d838

Browse files
authored
cache several properties (dask#7104)
add a reset_cache function get self.chunks directly from self.__chunks because it's 2-3x faster.
1 parent 5c96d82 commit 4e2d838

File tree

1 file changed

+23
-10
lines changed

1 file changed

+23
-10
lines changed

dask/array/core.py

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1179,11 +1179,25 @@ def __dask_postcompute__(self):
11791179
def __dask_postpersist__(self):
11801180
return Array, (self.name, self.chunks, self.dtype, self._meta)
11811181

1182-
@property
1182+
def _reset_cache(self, key=None):
1183+
"""
1184+
Reset cached properties.
1185+
1186+
Parameters
1187+
----------
1188+
key : str, optional
1189+
Remove specified key. The default removes all items.
1190+
"""
1191+
if key is None:
1192+
self.__dict__.clear()
1193+
else:
1194+
self.__dict__.pop(key, None)
1195+
1196+
@cached_property
11831197
def numblocks(self):
11841198
return tuple(map(len, self.chunks))
11851199

1186-
@property
1200+
@cached_property
11871201
def npartitions(self):
11881202
return reduce(mul, self.numblocks, 1)
11891203

@@ -1257,16 +1271,15 @@ def _chunks(self):
12571271
def _chunks(self, chunks):
12581272
self.__chunks = chunks
12591273

1260-
# When the chunks changes the cached properties that was dependent
1261-
# on it needs to be deleted:
1262-
for v in ["shape"]:
1263-
if v in self.__dict__:
1264-
del self.__dict__[v]
1274+
# When the chunks changes the cached properties that was
1275+
# dependent on it needs to be deleted:
1276+
for key in ["numblocks", "npartitions", "shape", "ndim", "size"]:
1277+
self._reset_cache(key)
12651278

12661279
@property
12671280
def chunks(self):
12681281
"""Chunks property."""
1269-
return self._chunks
1282+
return self.__chunks
12701283

12711284
@chunks.setter
12721285
def chunks(self, chunks):
@@ -1397,11 +1410,11 @@ def _repr_html_table(self):
13971410
]
13981411
return "\n".join(table)
13991412

1400-
@property
1413+
@cached_property
14011414
def ndim(self):
14021415
return len(self.shape)
14031416

1404-
@property
1417+
@cached_property
14051418
def size(self):
14061419
""" Number of elements in array """
14071420
return reduce(mul, self.shape, 1)

0 commit comments

Comments
 (0)