Skip to content

Commit 3af359c

Browse files
committed
Cache lists of column attributes in ColDefs to avoid many calls to __getattr__
For a table with 720 columns, __getattr__ was called 722 times for 'format' and 1441 times for 'name', which is slow. So this commit caches in the ColDefs object these attributes, and updates the attributes when the column attributes are changed. Before: ❯ python -m timeit -n 5 -r 5 -s "from astropy.table import Table" "Table.read('table.fits')" 5 loops, best of 5: 1.15 sec per loop After: ❯ python -m timeit -n 5 -r 5 -s "from astropy.table import Table" "Table.read('table.fits')" 5 loops, best of 5: 470 msec per loop
1 parent 850494a commit 3af359c

1 file changed

Lines changed: 15 additions & 1 deletion

File tree

‎astropy/io/fits/column.py‎

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1225,6 +1225,8 @@ def __init__(self, input, tbtype=None, ascii=False):
12251225
raise TypeError('Input to ColDefs must be a table HDU, a list '
12261226
'of Columns, or a record/field array.')
12271227

1228+
self._update_attrs()
1229+
12281230
# Listen for changes on all columns
12291231
for col in self.columns:
12301232
col._add_listener(self)
@@ -1398,6 +1400,12 @@ def __getattr__(self, name):
13981400
return attr
13991401
raise AttributeError(name)
14001402

1403+
def _update_attrs(self, name=None):
1404+
"""Get or update local lists of column attributes"""
1405+
attrs = [name] if name is not None else KEYWORD_ATTRIBUTES
1406+
for name in attrs:
1407+
setattr(self, name + 's', self.__getattr__(name+'s'))
1408+
14011409
@lazyproperty
14021410
def dtype(self):
14031411
# Note: This previously returned a dtype that just used the raw field
@@ -1510,6 +1518,7 @@ def _update_column_attribute_changed(self, column, attr, old_value,
15101518
if col is column:
15111519
break
15121520

1521+
self._update_attrs(name=attr)
15131522
self._notify('column_attribute_changed', column, idx, attr, old_value,
15141523
new_value)
15151524

@@ -1527,6 +1536,7 @@ def add_col(self, column):
15271536
del self._dims
15281537

15291538
self.columns.append(column)
1539+
self._update_attrs()
15301540

15311541
# Listen for changes on the new column
15321542
column._add_listener(self)
@@ -1554,6 +1564,7 @@ def del_col(self, col_name):
15541564
del self._dims
15551565

15561566
del self.columns[indx]
1567+
self._update_attrs()
15571568

15581569
col._remove_listener(self)
15591570

@@ -1581,6 +1592,7 @@ def change_attrib(self, col_name, attrib, new_value):
15811592
"""
15821593

15831594
setattr(self[col_name], attrib, new_value)
1595+
self._update_attrs()
15841596

15851597
def change_name(self, col_name, new_name):
15861598
"""
@@ -1599,6 +1611,7 @@ def change_name(self, col_name, new_name):
15991611
raise ValueError('New name %s already exists.' % new_name)
16001612
else:
16011613
self.change_attrib(col_name, 'name', new_name)
1614+
self._update_attrs(name='name')
16021615

16031616
def change_unit(self, col_name, new_unit):
16041617
"""
@@ -1614,6 +1627,7 @@ def change_unit(self, col_name, new_unit):
16141627
"""
16151628

16161629
self.change_attrib(col_name, 'unit', new_unit)
1630+
self._update_attrs(name='unit')
16171631

16181632
def info(self, attrib='all', output=None):
16191633
"""
@@ -1688,7 +1702,6 @@ def __init__(self, input, tbtype=None, ascii=True):
16881702

16891703
@lazyproperty
16901704
def dtype(self):
1691-
_itemsize = self.spans[-1] + self.starts[-1] - 1
16921705
dtype = {}
16931706

16941707
for j in range(len(self)):
@@ -1745,6 +1758,7 @@ def _update_field_metrics(self):
17451758

17461759
self._spans = spans
17471760
self._width = end_col
1761+
self._update_attrs(name='start')
17481762

17491763

17501764
# Utilities

0 commit comments

Comments
 (0)