[Zodb-checkins] SVN: ZODB/trunk/src/ Use the cPickle inst_persistent_id rather than the persistent_id hook

Jim Fulton jim at zope.com
Thu Nov 6 17:52:24 EST 2008


Log message for revision 92821:
  Use the cPickle inst_persistent_id rather than the persistent_id hook
  to avoid unnecessary calls to the persistent_id hook for objects that
  we know can't be persistent.
  

Changed:
  U   ZODB/trunk/src/CHANGES.txt
  U   ZODB/trunk/src/ZODB/ConflictResolution.py
  U   ZODB/trunk/src/ZODB/ExportImport.py
  U   ZODB/trunk/src/ZODB/serialize.py
  U   ZODB/trunk/src/ZODB/tests/PackableStorage.py
  U   ZODB/trunk/src/ZODB/tests/StorageTestBase.py

-=-
Modified: ZODB/trunk/src/CHANGES.txt
===================================================================
--- ZODB/trunk/src/CHANGES.txt	2008-11-06 22:33:55 UTC (rev 92820)
+++ ZODB/trunk/src/CHANGES.txt	2008-11-06 22:52:23 UTC (rev 92821)
@@ -22,6 +22,14 @@
   XXX There are known issues with this implementation that need to be
   sorted out before it is "released".
 
+3.9.0a5 (2008-11-??)
+====================
+
+New Features
+------------
+
+- Object saves are a little faster
+
 3.9.0a4 (2008-11-06)
 ====================
 

Modified: ZODB/trunk/src/ZODB/ConflictResolution.py
===================================================================
--- ZODB/trunk/src/ZODB/ConflictResolution.py	2008-11-06 22:33:55 UTC (rev 92820)
+++ ZODB/trunk/src/ZODB/ConflictResolution.py	2008-11-06 22:52:23 UTC (rev 92821)
@@ -208,7 +208,7 @@
 
         file = StringIO()
         pickler = Pickler(file,1)
-        pickler.persistent_id = persistent_id
+        pickler.inst_persistent_id = persistent_id
         pickler.dump(meta)
         pickler.dump(resolved)
         return file.getvalue(1)

Modified: ZODB/trunk/src/ZODB/ExportImport.py
===================================================================
--- ZODB/trunk/src/ZODB/ExportImport.py	2008-11-06 22:33:55 UTC (rev 92820)
+++ ZODB/trunk/src/ZODB/ExportImport.py	2008-11-06 22:52:23 UTC (rev 92821)
@@ -170,7 +170,7 @@
 
             newp = StringIO()
             pickler = Pickler(newp, 1)
-            pickler.persistent_id = persistent_id
+            pickler.inst_persistent_id = persistent_id
 
             pickler.dump(unpickler.load())
             pickler.dump(unpickler.load())

Modified: ZODB/trunk/src/ZODB/serialize.py
===================================================================
--- ZODB/trunk/src/ZODB/serialize.py	2008-11-06 22:33:55 UTC (rev 92820)
+++ ZODB/trunk/src/ZODB/serialize.py	2008-11-06 22:52:23 UTC (rev 92821)
@@ -173,7 +173,7 @@
     def __init__(self, obj=None):
         self._file = cStringIO.StringIO()
         self._p = cPickle.Pickler(self._file, 1)
-        self._p.persistent_id = self.persistent_id
+        self._p.inst_persistent_id = self.persistent_id
         self._stack = []
         if obj is not None:
             self._stack.append(obj)

Modified: ZODB/trunk/src/ZODB/tests/PackableStorage.py
===================================================================
--- ZODB/trunk/src/ZODB/tests/PackableStorage.py	2008-11-06 22:33:55 UTC (rev 92820)
+++ ZODB/trunk/src/ZODB/tests/PackableStorage.py	2008-11-06 22:52:23 UTC (rev 92821)
@@ -13,18 +13,9 @@
 ##############################################################################
 """Run some tests relevant for storages that support pack()."""
 
-try:
-    import cPickle
-    pickle = cPickle
-    #import cPickle as pickle
-except ImportError:
-    import pickle
+import cPickle
+from cStringIO import StringIO
 
-try:
-    from cStringIO import StringIO
-except ImportError:
-    from StringIO import StringIO
-
 import time
 
 from persistent import Persistent
@@ -59,7 +50,7 @@
 # persistent pickling machinery -- in the dumps() function below -- will
 # pickle the oid string instead of the object's actual state.  Yee haw, this
 # stuff is deep. ;)
-class Object:
+class Object(object):
     def __init__(self, oid):
         self._oid = oid
 
@@ -86,15 +77,15 @@
             return obj.getoid()
         return None
     s = StringIO()
-    p = pickle.Pickler(s, 1)
-    p.persistent_id = getpersid
+    p = cPickle.Pickler(s, 1)
+    p.inst_persistent_id = getpersid
     p.dump(obj)
     p.dump(None)
     return s.getvalue()
 
 def pdumps(obj):
     s = StringIO()
-    p = pickle.Pickler(s)
+    p = cPickle.Pickler(s)
     p.dump(obj)
     p.dump(None)
     return s.getvalue()
@@ -124,12 +115,12 @@
         # with an argument bound to an instance attribute method, we do it
         # this way because it makes the code in the tests more succinct.
         #
-        # BUT!  Be careful in your use of loads() vs. pickle.loads().  loads()
+        # BUT!  Be careful in your use of loads() vs. cPickle.loads().  loads()
         # should only be used on the Root object's pickle since it's the only
-        # special one.  All the Object instances should use pickle.loads().
+        # special one.  All the Object instances should use cPickle.loads().
         def loads(str, persfunc=self._cache.get):
             fp = StringIO(str)
-            u = pickle.Unpickler(fp)
+            u = cPickle.Unpickler(fp)
             u.persistent_load = persfunc
             return u.load()
         return loads
@@ -339,15 +330,15 @@
         revid3 = self._dostoreNP(oid, revid=revid2, data=pdumps(obj))
         # Now make sure all three revisions can be extracted
         data = self._storage.loadSerial(oid, revid1)
-        pobj = pickle.loads(data)
+        pobj = cPickle.loads(data)
         eq(pobj.getoid(), oid)
         eq(pobj.value, 1)
         data = self._storage.loadSerial(oid, revid2)
-        pobj = pickle.loads(data)
+        pobj = cPickle.loads(data)
         eq(pobj.getoid(), oid)
         eq(pobj.value, 2)
         data = self._storage.loadSerial(oid, revid3)
-        pobj = pickle.loads(data)
+        pobj = cPickle.loads(data)
         eq(pobj.getoid(), oid)
         eq(pobj.value, 3)
         # Now pack all transactions; need to sleep a second to make
@@ -392,15 +383,15 @@
         revid3 = self._dostoreNP(oid, revid=revid2, data=pdumps(obj))
         # Now make sure all three revisions can be extracted
         data = self._storage.loadSerial(oid, revid1)
-        pobj = pickle.loads(data)
+        pobj = cPickle.loads(data)
         eq(pobj.getoid(), oid)
         eq(pobj.value, 1)
         data = self._storage.loadSerial(oid, revid2)
-        pobj = pickle.loads(data)
+        pobj = cPickle.loads(data)
         eq(pobj.getoid(), oid)
         eq(pobj.value, 2)
         data = self._storage.loadSerial(oid, revid3)
-        pobj = pickle.loads(data)
+        pobj = cPickle.loads(data)
         eq(pobj.getoid(), oid)
         eq(pobj.value, 3)
         # Now pack just revisions 1 and 2.  The object's current revision
@@ -417,12 +408,12 @@
         raises(KeyError, self._storage.loadSerial, oid, revid1)
         raises(KeyError, self._storage.loadSerial, oid, revid2)
         data = self._storage.loadSerial(oid, revid3)
-        pobj = pickle.loads(data)
+        pobj = cPickle.loads(data)
         eq(pobj.getoid(), oid)
         eq(pobj.value, 3)
         data, revid = self._storage.load(oid, '')
         eq(revid, revid3)
-        pobj = pickle.loads(data)
+        pobj = cPickle.loads(data)
         eq(pobj.getoid(), oid)
         eq(pobj.value, 3)
 
@@ -460,15 +451,15 @@
         revid3 = self._dostoreNP(oid1, revid=revid2, data=pdumps(obj1))
         # Now make sure all three revisions can be extracted
         data = self._storage.loadSerial(oid1, revid1)
-        pobj = pickle.loads(data)
+        pobj = cPickle.loads(data)
         eq(pobj.getoid(), oid1)
         eq(pobj.value, 1)
         data = self._storage.loadSerial(oid1, revid2)
-        pobj = pickle.loads(data)
+        pobj = cPickle.loads(data)
         eq(pobj.getoid(), oid1)
         eq(pobj.value, 2)
         data = self._storage.loadSerial(oid1, revid3)
-        pobj = pickle.loads(data)
+        pobj = cPickle.loads(data)
         eq(pobj.getoid(), oid1)
         eq(pobj.value, 3)
         # Now commit a revision of the second object
@@ -476,7 +467,7 @@
         revid4 = self._dostoreNP(oid2, data=pdumps(obj2))
         # And make sure the revision can be extracted
         data = self._storage.loadSerial(oid2, revid4)
-        pobj = pickle.loads(data)
+        pobj = cPickle.loads(data)
         eq(pobj.getoid(), oid2)
         eq(pobj.value, 11)
         # Now pack just revisions 1 and 2 of object1.  Object1's current
@@ -494,19 +485,19 @@
         raises(KeyError, self._storage.loadSerial, oid1, revid1)
         raises(KeyError, self._storage.loadSerial, oid1, revid2)
         data = self._storage.loadSerial(oid1, revid3)
-        pobj = pickle.loads(data)
+        pobj = cPickle.loads(data)
         eq(pobj.getoid(), oid1)
         eq(pobj.value, 3)
         data, revid = self._storage.load(oid1, '')
         eq(revid, revid3)
-        pobj = pickle.loads(data)
+        pobj = cPickle.loads(data)
         eq(pobj.getoid(), oid1)
         eq(pobj.value, 3)
         data, revid = self._storage.load(oid2, '')
         eq(revid, revid4)
         eq(loads(data).value, 11)
         data = self._storage.loadSerial(oid2, revid4)
-        pobj = pickle.loads(data)
+        pobj = cPickle.loads(data)
         eq(pobj.getoid(), oid2)
         eq(pobj.value, 11)
 
@@ -528,15 +519,15 @@
         revid3 = self._dostoreNP(oid, revid=revid2, data=pdumps(obj))
         # Now make sure all three revisions can be extracted
         data = self._storage.loadSerial(oid, revid1)
-        pobj = pickle.loads(data)
+        pobj = cPickle.loads(data)
         eq(pobj.getoid(), oid)
         eq(pobj.value, 1)
         data = self._storage.loadSerial(oid, revid2)
-        pobj = pickle.loads(data)
+        pobj = cPickle.loads(data)
         eq(pobj.getoid(), oid)
         eq(pobj.value, 2)
         data = self._storage.loadSerial(oid, revid3)
-        pobj = pickle.loads(data)
+        pobj = cPickle.loads(data)
         eq(pobj.getoid(), oid)
         eq(pobj.value, 3)
         # Now pack all transactions; need to sleep a second to make

Modified: ZODB/trunk/src/ZODB/tests/StorageTestBase.py
===================================================================
--- ZODB/trunk/src/ZODB/tests/StorageTestBase.py	2008-11-06 22:33:55 UTC (rev 92820)
+++ ZODB/trunk/src/ZODB/tests/StorageTestBase.py	2008-11-06 22:52:23 UTC (rev 92821)
@@ -52,7 +52,7 @@
     """Create a pickle in the format expected by ZODB."""
     f = StringIO()
     p = Pickler(f, 1)
-    p.persistent_id = _persistent_id
+    p.inst_persistent_id = _persistent_id
     klass = obj.__class__
     assert not hasattr(obj, '__getinitargs__'), "not ready for constructors"
     args = None



More information about the Zodb-checkins mailing list