Skip to content

Commit 5d4cb54

Browse files
bpo-34141: Optimized pickling simple non-recursive values. (GH-8318)
1 parent feabae9 commit 5d4cb54

File tree

2 files changed

+17
-20
lines changed

2 files changed

+17
-20
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Optimized pickling atomic types (None, bool, int, float, bytes, str).

Modules/_pickle.c

Lines changed: 16 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -3940,9 +3940,6 @@ save(PicklerObject *self, PyObject *obj, int pers_save)
39403940
if (_Pickler_OpcodeBoundary(self) < 0)
39413941
return -1;
39423942

3943-
if (Py_EnterRecursiveCall(" while pickling an object"))
3944-
return -1;
3945-
39463943
/* The extra pers_save argument is necessary to avoid calling save_pers()
39473944
on its returned object. */
39483945
if (!pers_save && self->pers_func) {
@@ -3952,7 +3949,7 @@ save(PicklerObject *self, PyObject *obj, int pers_save)
39523949
1 if a persistent id was saved.
39533950
*/
39543951
if ((status = save_pers(self, obj)) != 0)
3955-
goto done;
3952+
return status;
39563953
}
39573954

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

39673964
if (obj == Py_None) {
3968-
status = save_none(self, obj);
3969-
goto done;
3965+
return save_none(self, obj);
39703966
}
39713967
else if (obj == Py_False || obj == Py_True) {
3972-
status = save_bool(self, obj);
3973-
goto done;
3968+
return save_bool(self, obj);
39743969
}
39753970
else if (type == &PyLong_Type) {
3976-
status = save_long(self, obj);
3977-
goto done;
3971+
return save_long(self, obj);
39783972
}
39793973
else if (type == &PyFloat_Type) {
3980-
status = save_float(self, obj);
3981-
goto done;
3974+
return save_float(self, obj);
39823975
}
39833976

39843977
/* Check the memo to see if it has the object. If so, generate
39853978
a GET (or BINGET) opcode, instead of pickling the object
39863979
once again. */
39873980
if (PyMemoTable_Get(self->memo, obj)) {
3988-
if (memo_get(self, obj) < 0)
3989-
goto error;
3990-
goto done;
3981+
return memo_get(self, obj);
39913982
}
39923983

39933984
if (type == &PyBytes_Type) {
3994-
status = save_bytes(self, obj);
3995-
goto done;
3985+
return save_bytes(self, obj);
39963986
}
39973987
else if (type == &PyUnicode_Type) {
3998-
status = save_unicode(self, obj);
3999-
goto done;
3988+
return save_unicode(self, obj);
40003989
}
4001-
else if (type == &PyDict_Type) {
3990+
3991+
/* We're only calling Py_EnterRecursiveCall here so that atomic
3992+
types above are pickled faster. */
3993+
if (Py_EnterRecursiveCall(" while pickling an object")) {
3994+
return -1;
3995+
}
3996+
3997+
if (type == &PyDict_Type) {
40023998
status = save_dict(self, obj);
40033999
goto done;
40044000
}

0 commit comments

Comments
 (0)