diff options
author | Serhiy Storchaka <storchaka@gmail.com> | 2017-02-02 09:12:47 (GMT) |
---|---|---|
committer | Serhiy Storchaka <storchaka@gmail.com> | 2017-02-02 09:12:47 (GMT) |
commit | bee09aecc2c317fd3488b819ab88a13e0755189e (patch) | |
tree | 7a7efc93b0be362f0bace2153092feeb9b32204b /Lib/pickle.py | |
parent | bb19bf275b465b8a17da51b6365f9d2800921a05 (diff) | |
download | cpython-bee09aecc2c317fd3488b819ab88a13e0755189e.zip cpython-bee09aecc2c317fd3488b819ab88a13e0755189e.tar.gz cpython-bee09aecc2c317fd3488b819ab88a13e0755189e.tar.bz2 |
Issue #29368: The extend() method is now called instead of the append()
method when unpickle collections.deque and other list-like objects.
This can speed up unpickling to 2 times.
Diffstat (limited to 'Lib/pickle.py')
-rw-r--r-- | Lib/pickle.py | 17 |
1 files changed, 12 insertions, 5 deletions
diff --git a/Lib/pickle.py b/Lib/pickle.py index c8370c9..702b0b3 100644 --- a/Lib/pickle.py +++ b/Lib/pickle.py @@ -1464,12 +1464,19 @@ class _Unpickler: def load_appends(self): items = self.pop_mark() list_obj = self.stack[-1] - if isinstance(list_obj, list): - list_obj.extend(items) + try: + extend = list_obj.extend + except AttributeError: + pass else: - append = list_obj.append - for item in items: - append(item) + extend(items) + return + # Even if the PEP 307 requires extend() and append() methods, + # fall back on append() if the object has no extend() method + # for backward compatibility. + append = list_obj.append + for item in items: + append(item) dispatch[APPENDS[0]] = load_appends def load_setitem(self): |