Issue #395 resolved

sessionFilter stringIO and pickle should use cPickle and cStringIO if available

Anonymous created an issue
'''motivation:''' From the python docs. "cPickle can be up to 1000 times faster than pickle because the former is implemented in C" and "The module cStringIO provides an interface similar to that of the StringIO module. Heavy use of StringIO.StringIO objects can be made more efficient by using the function StringIO() from this module " {{{ karl@ubuntu-vaio:~/projects/turbogears/thirdparty/cherrypy$ svn diff Index: cherrypy/lib/filter/sessionfilter.py =================================================================== --- cherrypy/lib/filter/sessionfilter.py (revision 825) +++ cherrypy/lib/filter/sessionfilter.py (working copy) @@ -53,17 +53,25 @@ import datetime import os -import pickle import random import sha -import StringIO import time import threading import types import basefilter +# Try to get the C implementations for StingIO anb pickle. +try: + from cStringIO import StringIO +except ImportError: + from StringIO import StringIO +try: + import cPickle as pickle +except: + import pickle + class EmptyClass: """ An empty class """ pass @@ -382,7 +390,7 @@ return None data, expirationTime = rows[0] # Unpickle data - f = StringIO.StringIO(data) + f = StringIO(data) data = pickle.load(f) return (data, expirationTime) @@ -392,7 +400,7 @@ 'delete from session where id=%s', (id,)) # Pickle data - f = StringIO.StringIO() + f = StringIO() pickle.dump(data, f) # Insert new session data self.cursor.execute( }}} '''Or the alternative''' without StringIO at all. {{{ karl@ubuntu-vaio:~/projects/turbogears/thirdparty/cherrypy$ svn diff Index: cherrypy/lib/filter/sessionfilter.py =================================================================== --- cherrypy/lib/filter/sessionfilter.py (revision 825) +++ cherrypy/lib/filter/sessionfilter.py (working copy) @@ -53,17 +53,23 @@ import datetime import os -import pickle import random import sha -import StringIO import time import threading import types import basefilter +# Try to get the C implementations for pickle. +# StringIO or cStringIO is not needed. +# Use pickle.loads pickle.dumps +try: + import cPickle as pickle +except: + import pickle + class EmptyClass: """ An empty class """ pass @@ -382,8 +388,7 @@ return None data, expirationTime = rows[0] # Unpickle data - f = StringIO.StringIO(data) - data = pickle.load(f) + data = pickle.loads(data) return (data, expirationTime) def save(self, id, data, expirationTime): @@ -392,12 +397,11 @@ 'delete from session where id=%s', (id,)) # Pickle data - f = StringIO.StringIO() - pickle.dump(data, f) + data = pickle.dumps(data) # Insert new session data self.cursor.execute( 'insert into session (id, data, expiration_time) values (%s, %s, %s)', - (id, f.getvalue(), expirationTime)) + (id, data, expirationTime)) def acquireLock(self): # We use the "for update" clause to lock the row }}} Not sure which one would be faster in the end. My gut says it is the one without StringIO. Reported by karl@rockynet.com

Comments (2)

  1. Log in to comment