From 883f19629ed554dbc1c493b4f3a7bcc525fedf30 Mon Sep 17 00:00:00 2001 From: ARF Date: Wed, 6 May 2015 11:18:54 +0200 Subject: [PATCH] cache ctable.dtype Among others, this allow improving the performance of custom implementations of the outflavor abstraction layer. --- bcolz/ctable.py | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/bcolz/ctable.py b/bcolz/ctable.py index 97d7bb89..459ff578 100644 --- a/bcolz/ctable.py +++ b/bcolz/ctable.py @@ -142,6 +142,9 @@ def cparams(self): @property def dtype(self): "The data type of this object (numpy dtype)." + if self._dtype: + return self._dtype + names, cols = self.names, self.cols l = [] for name in names: @@ -150,7 +153,8 @@ def dtype(self): t = (name, col.dtype) if col.ndim == 1 else \ (name, (col.dtype, col.shape[1:])) l.append(t) - return np.dtype(l) + self._dtype = np.dtype(l) + return self._dtype @property def names(self): @@ -222,6 +226,8 @@ def __init__(self, columns=None, names=None, **kwargs): # Attach the attrs to this object self.attrs = attrs.attrs(self.rootdir, self.mode, _new=_new) + # Trigger update of dtype cache + self._dtype = None # Cache a structured array of len 1 for ctable[int] acceleration self._arr1 = np.empty(shape=(1,), dtype=self.dtype) @@ -487,6 +493,8 @@ def addcol(self, newcol, name=None, pos=None, move=False, **kwargs): # Insert the column self.cols.insert(name, pos, newcol) + # Trigger update of dtype cache + self._dtype = None # Update _arr1 self._arr1 = np.empty(shape=(1,), dtype=self.dtype) @@ -540,6 +548,8 @@ def delcol(self, name=None, pos=None, keep=False): if not keep: col.purge() + # Trigger update of dtype cache + self._dtype = None # Update _arr1 self._arr1 = np.empty(shape=(1,), dtype=self.dtype)