[Zope-Checkins] SVN: Zope/trunk/ - RAMCacheManager: opimized performance by using cPickle instead

Andreas Jung andreas at andreas-jung.com
Mon Nov 29 06:10:12 EST 2004


Log message for revision 28532:
  
        - RAMCacheManager: opimized performance by using cPickle instead
          of pickle and by using the highest pickle protocol available
          instead of using ASCII pickles (patch by Dieter Maurer)
  

Changed:
  U   Zope/trunk/doc/CHANGES.txt
  U   Zope/trunk/lib/python/Products/StandardCacheManagers/RAMCacheManager.py

-=-
Modified: Zope/trunk/doc/CHANGES.txt
===================================================================
--- Zope/trunk/doc/CHANGES.txt	2004-11-28 09:48:35 UTC (rev 28531)
+++ Zope/trunk/doc/CHANGES.txt	2004-11-29 11:10:12 UTC (rev 28532)
@@ -45,6 +45,10 @@
         text/<foo> types
 
     Bugs fixed
+
+      - RAMCacheManager: opimized performance by using cPickle instead
+        of pickle and by using the highest pickle protocol available
+        instead of using ASCII pickles (patch by Dieter Maurer)
         
       - Collector #631: Image URLs in StructuredText containing port
         numbers were not rendered correctly

Modified: Zope/trunk/lib/python/Products/StandardCacheManagers/RAMCacheManager.py
===================================================================
--- Zope/trunk/lib/python/Products/StandardCacheManagers/RAMCacheManager.py	2004-11-28 09:48:35 UTC (rev 28531)
+++ Zope/trunk/lib/python/Products/StandardCacheManagers/RAMCacheManager.py	2004-11-29 11:10:12 UTC (rev 28532)
@@ -26,12 +26,9 @@
 import Globals
 from Globals import DTMLFile
 
-try: from cPickle import Pickler
-except: from pickle import Pickler
+try: from cPickle import Pickler, HIGHEST_PROTOCOL
+except: from pickle import Pickler, HIGHEST_PROTOCOL
 
-try: from cStringIO import dumps
-except: from pickle import dumps
-
 _marker = []  # Create a new marker object.
 
 
@@ -52,7 +49,17 @@
             # us from caching something that might result in memory
             # leaks.  It's also convenient for determining the
             # approximate memory usage of the cache entry.
-            self.size = len(dumps(index)) + len(dumps(data))
+            # DM 2004-11-29: this code causes excessive time.
+            #   Note also that it does not prevent us from
+            #   caching objects with references to persistent objects
+            #   When we do, nasty persistency errors are likely
+            #   to occur ("shouldn't load data while connection is closed").
+            #self.size = len(dumps(index)) + len(dumps(data))
+            sizer = _ByteCounter()
+            pickler = Pickler(sizer, HIGHEST_PROTOCOL)
+            pickler.dump(index)
+            pickler.dump(data)
+            self.size = sizer.getCount()
         except:
             raise CacheException('The data for the cache is not pickleable.')
         self.created = time.time()
@@ -468,6 +475,16 @@
 Globals.default__class_init__(RAMCacheManager)
 
 
+class _ByteCounter:
+    '''auxiliary file like class which just counts the bytes written.'''
+    _count = 0
+
+    def write(self, bytes):
+        self._count += len(bytes)
+
+    def getCount(self):
+        return self._count
+
 manage_addRAMCacheManagerForm = DTMLFile('dtml/addRCM', globals())
 
 def manage_addRAMCacheManager(self, id, REQUEST=None):



More information about the Zope-Checkins mailing list