[Zope-CVS] CVS: Packages/pypes/pypes - identity.py:1.13
Casey Duncan
casey at zope.com
Wed Mar 3 23:48:27 EST 2004
Update of /cvs-repository/Packages/pypes/pypes
In directory cvs.zope.org:/tmp/cvs-serv16088
Modified Files:
identity.py
Log Message:
Use BTree.Length to optimize IdentitySet __len__
=== Packages/pypes/pypes/identity.py 1.12 => 1.13 ===
--- Packages/pypes/pypes/identity.py:1.12 Mon Mar 1 23:14:26 2004
+++ Packages/pypes/pypes/identity.py Wed Mar 3 23:47:56 2004
@@ -145,6 +145,7 @@
objs -- An iterable sequence of id-registered objects.
"""
self._idset = IITreeSet()
+ self._length = Length()
if objs:
self.update(objs)
@@ -163,6 +164,7 @@
set._idset = idset
if dbconn is not None:
dbconn.add(set)
+ set._length = Length(len(idset))
return set
fromIdSet = classmethod(fromIdSet)
@@ -175,11 +177,13 @@
if ident is None:
raise IdentityError, obj
isnew = self._idset.insert(ident)
- if isnew and not isinstance(self._idset, IITreeSet):
- # Set operations may result in _idset being an IISet
- # For better database efficiency we upgrade it to an IITreeSet
- # When we are mutated in place
- self._idset = IITreeSet(self._idset)
+ if isnew:
+ self._length.change(1)
+ if not isinstance(self._idset, IITreeSet):
+ # Set operations may result in _idset being an IISet
+ # For better database efficiency we upgrade it to an IITreeSet
+ # When we are mutated in place
+ self._idset = IITreeSet(self._idset)
return bool(isnew)
def remove(self, obj):
@@ -191,6 +195,7 @@
except KeyError:
raise SetLookupError, obj
else:
+ self._length.change(-1)
if not isinstance(self._idset, IITreeSet):
# Upgrade to IITreeSet for storage efficiency when mutated
self._idset = IITreeSet(self._idset)
@@ -206,9 +211,11 @@
return
self.add(first)
insert = self._idset.insert
+ lenChange = self._length.change
try:
for ob in objs:
- insert(ob._pypes_id_)
+ if insert(ob._pypes_id_):
+ lenChange(1)
except AttributeError:
raise IdentityError, 'Set update received unregistered object(s)'
@@ -223,7 +230,7 @@
return bool(self._idset)
def __len__(self):
- return len(self._idset) # XXX keep a length for efficiency?
+ return self._length()
def __iter__(self):
# If an object in the set is no longer registered None is substituted
@@ -313,7 +320,10 @@
if not idsect:
# Short circuit on empty intersect
return IdentitySet()
- return IdentitySet.fromIdSet(idsect, dbconn=dbconn)
+ if idsect is not None:
+ return IdentitySet.fromIdSet(idsect, dbconn=dbconn)
+ else:
+ return IdentitySet()
## Id Event Messsage Types ##
More information about the Zope-CVS
mailing list