New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Optimize unpickling list-like objects: use extend() instead of append() #73554
Comments
According to PEP-307 the extend method can be used for appending list items to the object.
Proposed patch makes the extend method be used in the APPENDS opcode. To avoid breaking existing code the use of the extend method is optional. Microbenchmark: $ ./python -m timeit -s "import pickle, collections; p = pickle.dumps(collections.deque([None]*10000), 4)" -- "pickle.loads(p)"
Unpatched: 100 loops, best of 5: 2.02 msec per loop
Patched: 500 loops, best of 5: 833 usec per loop |
What is the cost of adding an extra isinstance(d, collections.Sequence) check? It would be closer the current design ("white list" of types), safer and avoid bad surprises. But Python has a long tradition of duck typing, so maybe isinstance() can be seen as overkill :-) |
collections.Sequence has no relation to the pickle protocol. Every object that return listitems from the __reduce__() method must support the extend() method according to the specification. |
Hum ok. I don't know well the pickle module, but according to your quote, yeah, the patch is valid. pickle-appends-extend.patch LGTM, except minor comments. It would be nice to get a feedback from Alexandre, but I'm not sure that he is still around :-/ What about Antoine Pitrou, are you around? :-) |
See bpo-17720 for a feedback from Alexandre. |
This code looks correct and reasonable. The tests all pass for me. I think you can go forward and apply the patch. |
Updated patch addresses Antoine's comment. It adds the comment explaining a fallback. |
Could anyone please make a review of my explanation comment? I have doubts about a wording. |
I'm not fluent in english, so I'm not the best for this task. But I reviewed your patch ;-) |
Thank you Victor. Your wording looks simpler to me. |
pickle-appends-extend-3.patch LGTM. Even if I don't see any refleak, you might just run "./python -m test -R 3:3 test_pickle" just to be sure :-) |
The wording is correct and clear. |
New changeset 94d630a02a81 by Serhiy Storchaka in branch 'default': |
New changeset 328147c0edc3 by Victor Stinner in branch 'default': |
Change 94d630a02a81 introduced a crash in test_pickle: Fatal Python error: ..\Modules\_pickle.c:5847 object at 000002B7F7BED2F8 has negative ref count -1 Seen on buildbots, but can always be reproduce on Linux as well. It seems like you was biten by the surprising _Pickle_FastCall() API which decreases the reference counter of its second parameter. Don't ask me why it does that :-) (I don't know.) I fixed the bug in the change 328147c0edc3 to repair buildbots. |
Thanks Victor. |
The PyList_Check -> PyList_CheckExact change led to bugs where subclasses of list that override extend can no longer be unpickled. |
New changeset f89fdc29937139b55dd68587759cadb8468d0190 by Serhiy Storchaka in branch 'master': New changeset 4d7e63d9773a766358294593fc00b1f8c8f41b5d by Victor Stinner in branch 'master': |
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: