Skip to content

bpo-34141: Optimized pickling simple non-recursive values. #8318

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

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Optimized pickling atomic types (None, bool, int, float, bytes, str).
36 changes: 16 additions & 20 deletions Modules/_pickle.c
Original file line number Diff line number Diff line change
Expand Up @@ -3940,9 +3940,6 @@ save(PicklerObject *self, PyObject *obj, int pers_save)
if (_Pickler_OpcodeBoundary(self) < 0)
return -1;

if (Py_EnterRecursiveCall(" while pickling an object"))
return -1;

/* The extra pers_save argument is necessary to avoid calling save_pers()
on its returned object. */
if (!pers_save && self->pers_func) {
Expand All @@ -3952,7 +3949,7 @@ save(PicklerObject *self, PyObject *obj, int pers_save)
1 if a persistent id was saved.
*/
if ((status = save_pers(self, obj)) != 0)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can this trigger a recursive call?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

save_pers() calls save() with pers_save=1, and this stops the recursion via save_pers.

goto done;
return status;
}

type = Py_TYPE(obj);
Expand All @@ -3965,40 +3962,39 @@ save(PicklerObject *self, PyObject *obj, int pers_save)
/* Atom types; these aren't memoized, so don't check the memo. */

if (obj == Py_None) {
status = save_none(self, obj);
goto done;
return save_none(self, obj);
}
else if (obj == Py_False || obj == Py_True) {
status = save_bool(self, obj);
goto done;
return save_bool(self, obj);
}
else if (type == &PyLong_Type) {
status = save_long(self, obj);
goto done;
return save_long(self, obj);
}
else if (type == &PyFloat_Type) {
status = save_float(self, obj);
goto done;
return save_float(self, obj);
}

/* Check the memo to see if it has the object. If so, generate
a GET (or BINGET) opcode, instead of pickling the object
once again. */
if (PyMemoTable_Get(self->memo, obj)) {
if (memo_get(self, obj) < 0)
goto error;
goto done;
return memo_get(self, obj);
}

if (type == &PyBytes_Type) {
status = save_bytes(self, obj);
goto done;
return save_bytes(self, obj);
}
else if (type == &PyUnicode_Type) {
status = save_unicode(self, obj);
goto done;
return save_unicode(self, obj);
}
else if (type == &PyDict_Type) {

/* We're only calling Py_EnterRecursiveCall here so that atomic
types above are pickled faster. */
if (Py_EnterRecursiveCall(" while pickling an object")) {
return -1;
}

if (type == &PyDict_Type) {
status = save_dict(self, obj);
goto done;
}
Expand Down