Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit ea99c5c

Browse files
committed
Issue #9410: Various optimizations to the pickle module, leading to
speedups up to 4x (depending on the benchmark). Mostly ported from Unladen Swallow; initial patch by Alexandre Vassalotti.
1 parent 350c722 commit ea99c5c

5 files changed

Lines changed: 1863 additions & 526 deletions

File tree

Lib/pickle.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1287,12 +1287,6 @@ def decode_long(data):
12871287
"""
12881288
return int.from_bytes(data, byteorder='little', signed=True)
12891289

1290-
# Use the faster _pickle if possible
1291-
try:
1292-
from _pickle import *
1293-
except ImportError:
1294-
Pickler, Unpickler = _Pickler, _Unpickler
1295-
12961290
# Shorthands
12971291

12981292
def dump(obj, file, protocol=None, *, fix_imports=True):
@@ -1316,6 +1310,12 @@ def loads(s, *, fix_imports=True, encoding="ASCII", errors="strict"):
13161310
return Unpickler(file, fix_imports=fix_imports,
13171311
encoding=encoding, errors=errors).load()
13181312

1313+
# Use the faster _pickle if possible
1314+
try:
1315+
from _pickle import *
1316+
except ImportError:
1317+
Pickler, Unpickler = _Pickler, _Unpickler
1318+
13191319
# Doctest
13201320
def _test():
13211321
import doctest

Lib/test/pickletester.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1068,6 +1068,15 @@ def test_pickle_to_2x(self):
10681068
dumped = self.dumps(set([3]), 2)
10691069
self.assertEqual(dumped, DATA6)
10701070

1071+
def test_large_pickles(self):
1072+
# Test the correctness of internal buffering routines when handling
1073+
# large data.
1074+
for proto in protocols:
1075+
data = (1, b'x' * (256 * 1024))
1076+
dumped = self.dumps(data, proto)
1077+
loaded = self.loads(dumped)
1078+
self.assertEqual(loaded, data)
1079+
10711080

10721081
# Test classes for reduce_ex
10731082

Lib/test/test_pickle.py

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,18 @@ def loads(self, buf):
3737
return u.load()
3838

3939

40+
class InMemoryPickleTests(AbstractPickleTests):
41+
42+
pickler = pickle._Pickler
43+
unpickler = pickle._Unpickler
44+
45+
def dumps(self, arg, proto=None):
46+
return pickle.dumps(arg, proto)
47+
48+
def loads(self, buf):
49+
return pickle.loads(buf)
50+
51+
4052
class PyPersPicklerTests(AbstractPersistentPicklerTests):
4153

4254
pickler = pickle._Pickler
@@ -95,7 +107,8 @@ def test_main():
95107
tests.extend([CPicklerTests, CPersPicklerTests,
96108
CDumpPickle_LoadPickle, DumpPickle_CLoadPickle,
97109
PyPicklerUnpicklerObjectTests,
98-
CPicklerUnpicklerObjectTests])
110+
CPicklerUnpicklerObjectTests,
111+
InMemoryPickleTests])
99112
support.run_unittest(*tests)
100113
support.run_doctest(pickle)
101114

Misc/NEWS

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,10 @@ Core and Builtins
2020
Library
2121
-------
2222

23+
- Issue #9410: Various optimizations to the pickle module, leading to
24+
speedups up to 4x (depending on the benchmark). Mostly ported from
25+
Unladen Swallow; initial patch by Alexandre Vassalotti.
26+
2327
- The pprint module now supports printing OrderedDicts in their given
2428
order (formerly, it would sort the keys).
2529

0 commit comments

Comments
 (0)