Skip to content

Commit 6ef6714

Browse files
dcherianjhamman
andauthored
Optimize Array.info and Group.info (#1733)
* Optimize Array.info. Avoid repeated computes of the same value (getsize) * Don't have InfoReporter query items twice. Apparently IPython will run both __repr__ and _repr_html_ so we were calling `getsize` twice. * Group too * Apply suggestions from code review Co-authored-by: Joe Hamman <[email protected]> --------- Co-authored-by: Joe Hamman <[email protected]>
1 parent 97cb3a7 commit 6ef6714

File tree

4 files changed

+13
-12
lines changed

4 files changed

+13
-12
lines changed

docs/release.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ Unreleased
2121
Enhancements
2222
~~~~~~~~~~~~
2323

24+
* Optimize ``Array.info`` so that it calls `getsize` only once.
25+
By :user:`Deepak Cherian <dcherian>`.
2426
* Override IPython ``_repr_*_`` methods to avoid expensive lookups against object stores.
2527
By :user:`Deepak Cherian <dcherian>` :issue:`1716`.
2628

zarr/core.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,6 @@ def __init__(
176176
)
177177

178178
# initialize info reporter
179-
self._info_reporter = InfoReporter(self)
180179

181180
# initialize indexing helpers
182181
self._oindex = OIndex(self)
@@ -2429,7 +2428,7 @@ def info(self):
24292428
Chunks initialized : 0/10
24302429
24312430
"""
2432-
return self._info_reporter
2431+
return InfoReporter(self)
24332432

24342433
def info_items(self):
24352434
return self._synchronized_op(self._info_items_nosync)
@@ -2471,14 +2470,16 @@ def bytestr(n):
24712470
items += [("Synchronizer type", typestr(self._synchronizer))]
24722471

24732472
# storage info
2473+
nbytes = self.nbytes
2474+
nbytes_stored = self.nbytes_stored
24742475
items += [("Store type", typestr(self._store))]
24752476
if self._chunk_store is not None:
24762477
items += [("Chunk store type", typestr(self._chunk_store))]
2477-
items += [("No. bytes", bytestr(self.nbytes))]
2478-
if self.nbytes_stored > 0:
2478+
items += [("No. bytes", bytestr(nbytes))]
2479+
if nbytes_stored > 0:
24792480
items += [
2480-
("No. bytes stored", bytestr(self.nbytes_stored)),
2481-
("Storage ratio", f"{self.nbytes / self.nbytes_stored:.1f}"),
2481+
("No. bytes stored", bytestr(nbytes_stored)),
2482+
("Storage ratio", f"{nbytes / nbytes_stored:.1f}"),
24822483
]
24832484
items += [("Chunks initialized", f"{self.nchunks_initialized}/{self.nchunks}")]
24842485

zarr/hierarchy.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,6 @@ def __init__(
211211
)
212212

213213
# setup info
214-
self._info = InfoReporter(self)
215214

216215
@property
217216
def store(self):
@@ -266,7 +265,7 @@ def attrs(self):
266265
@property
267266
def info(self):
268267
"""Return diagnostic information about the group."""
269-
return self._info
268+
return InfoReporter(self)
270269

271270
@property
272271
def meta_array(self):

zarr/util.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -408,14 +408,13 @@ def info_html_report(items) -> str:
408408
class InfoReporter:
409409
def __init__(self, obj):
410410
self.obj = obj
411+
self.items = self.obj.info_items()
411412

412413
def __repr__(self):
413-
items = self.obj.info_items()
414-
return info_text_report(items)
414+
return info_text_report(self.items)
415415

416416
def _repr_html_(self):
417-
items = self.obj.info_items()
418-
return info_html_report(items)
417+
return info_html_report(self.items)
419418

420419

421420
class TreeNode:

0 commit comments

Comments
 (0)