[Checkins] SVN: Products.ZCatalog/trunk/ Optimized length calculation of Lazy classes.
Hanno Schlichting
hannosch at hannosch.eu
Sun Dec 26 13:38:13 EST 2010
Log message for revision 119143:
Optimized length calculation of Lazy classes.
Changed:
U Products.ZCatalog/trunk/CHANGES.txt
U Products.ZCatalog/trunk/src/Products/ZCatalog/Lazy.py
-=-
Modified: Products.ZCatalog/trunk/CHANGES.txt
===================================================================
--- Products.ZCatalog/trunk/CHANGES.txt 2010-12-26 16:53:37 UTC (rev 119142)
+++ Products.ZCatalog/trunk/CHANGES.txt 2010-12-26 18:38:12 UTC (rev 119143)
@@ -4,6 +4,8 @@
2.13.2 (unreleased)
-------------------
+- Optimized length calculation of Lazy classes.
+ [hannosch]
2.13.1 (2010-12-25)
-------------------
Modified: Products.ZCatalog/trunk/src/Products/ZCatalog/Lazy.py
===================================================================
--- Products.ZCatalog/trunk/src/Products/ZCatalog/Lazy.py 2010-12-26 16:53:37 UTC (rev 119142)
+++ Products.ZCatalog/trunk/src/Products/ZCatalog/Lazy.py 2010-12-26 18:38:12 UTC (rev 119143)
@@ -13,22 +13,22 @@
from itertools import islice, count
+_marker = object()
+
class Lazy(object):
# Allow (reluctantly) access to unprotected attributes
__allow_access_to_unprotected_subobjects__=1
+ _len = _marker
def __repr__(self):
return repr(list(self))
def __len__(self):
# This is a worst-case len, subclasses should try to do better
- try:
+ if self._len is not _marker:
return self._len
- except AttributeError:
- pass
-
l = len(self._data)
while 1:
try:
@@ -127,17 +127,16 @@
def __len__(self):
# Make len of LazyCat only as expensive as the lens
# of its underlying sequences
+ if self._len is not _marker:
+ return self._len
+ l = 0
try:
- return self._len
- except Exception:
- try:
- l = 0
- for s in self._seq:
- l += len(s)
- except AttributeError:
- l = len(self._data)
- self._len = l
- return l
+ for s in self._seq:
+ l += len(s)
+ except AttributeError:
+ l = len(self._data)
+ self._len = l
+ return l
class LazyMap(Lazy):
@@ -267,7 +266,10 @@
self._seq = seq
def __len__(self):
- return len(self._seq)
+ if self._len is not _marker:
+ return self._len
+ self._len = len(self._seq)
+ return self._len
def __getitem__(self, index):
return self._seq[index][1]
More information about the checkins
mailing list