1. Pypy
  2. Untitled project
  3. pypy

Commits

Konstantin Lopuhin  committed 15467e5

speed up pickle.dumps - use StringBuilder instead of StringIO - see issue #979

  • Participants
  • Parent commits d572df7
  • Branches pickle-dumps

Comments (0)

Files changed (2)

File lib-python/2/pickle.py

View file
 import struct
 import re
 
+from __pypy__.builders import StringBuilder
+
 __all__ = ["PickleError", "PicklingError", "UnpicklingError", "Pickler",
            "Unpickler", "dump", "dumps", "load", "loads"]
 
 except ImportError:
     from StringIO import StringIO
 
+
+class StringBuilderFile(object):
+    ''' pickle uses only file.write - provide this method, 
+    use StringBuilder for speed
+    '''
+    def __init__(self):
+        self.builder = StringBuilder()
+
+    def write(self, data):
+        for x in data:
+            self.builder.append(data)
+
+    def getvalue(self):
+        return self.builder.build()
+
+
 def dump(obj, file, protocol=None):
     Pickler(file, protocol).dump(obj)
 
 def dumps(obj, protocol=None):
-    file = StringIO()
+    file = StringBuilderFile()
     Pickler(file, protocol).dump(obj)
     return file.getvalue()
 

File lib_pypy/cPickle.py

View file
 # Reimplementation of cPickle, mostly as a copy of pickle.py
 #
 
-from pickle import Pickler, dump, dumps, PickleError, PicklingError, UnpicklingError, _EmptyClass
+from pickle import Pickler, dump, dumps, PickleError, PicklingError, UnpicklingError, _EmptyClass, StringBuilderFile
 from pickle import __doc__, __version__, format_version, compatible_formats
 from types import *
 from copy_reg import dispatch_table
 
 @builtinify
 def dumps(obj, protocol=None):
-    file = StringIO()
+    file = StringBuilderFile()
     Pickler(file, protocol).dump(obj)
     return file.getvalue()