Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings
Open
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,3 @@
Replace :c:func:`PyTuple_Pack` calls with :c:func:`PyTuple_FromArray` in

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This is probably not necessary since nothing really changes from the user's perspective. Just mention that performance improved, similar to the NEWS entry for #140010

:file:`Objects/codeobject.c` and :file:`Modules/_pickle.c` to avoid
variadic argument overhead.
9 changes: 6 additions & 3 deletions 9 Modules/_pickle.c
Original file line number Diff line number Diff line change
Expand Up @@ -3757,7 +3757,8 @@ fix_imports(PickleState *st, PyObject **module_name, PyObject **global_name)
PyObject *key;
PyObject *item;

key = PyTuple_Pack(2, *module_name, *global_name);
PyObject *key_items[] = {*module_name, *global_name};
key = PyTuple_FromArray(key_items, 2);
if (key == NULL)
return -1;
item = PyDict_GetItemWithError(st->name_mapping_3to2, key);
Expand Down Expand Up @@ -3863,7 +3864,8 @@ save_global(PickleState *st, PicklerObject *self, PyObject *obj,
char pdata[5];
Py_ssize_t n;

extension_key = PyTuple_Pack(2, module_name, global_name);
PyObject *ext_key_items[] = {module_name, global_name};
extension_key = PyTuple_FromArray(ext_key_items, 2);
if (extension_key == NULL) {
goto error;
}
Expand Down Expand Up @@ -7300,7 +7302,8 @@ _pickle_Unpickler_find_class_impl(UnpicklerObject *self, PyTypeObject *cls,

/* Check if the global (i.e., a function or a class) was renamed
or moved to another module. */
key = PyTuple_Pack(2, module_name, global_name);
PyObject *find_key_items[] = {module_name, global_name};
key = PyTuple_FromArray(find_key_items, 2);
if (key == NULL)
return NULL;
item = PyDict_GetItemWithError(st->name_mapping_2to3, key);
Expand Down
39 changes: 26 additions & 13 deletions 39 Objects/codeobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -3050,17 +3050,22 @@ _PyCode_ConstantKey(PyObject *op)
else if (PyBool_Check(op) || PyBytes_CheckExact(op)) {
/* Make booleans different from integers 0 and 1.
* Avoid BytesWarning from comparing bytes with strings. */
key = PyTuple_Pack(2, Py_TYPE(op), op);
PyObject *items[] = {(PyObject *)Py_TYPE(op), op};
key = PyTuple_FromArray(items, 2);
}
else if (PyFloat_CheckExact(op)) {
double d = PyFloat_AS_DOUBLE(op);
/* all we need is to make the tuple different in either the 0.0
* or -0.0 case from all others, just to avoid the "coercion".
*/
if (d == 0.0 && copysign(1.0, d) < 0.0)
key = PyTuple_Pack(3, Py_TYPE(op), op, Py_None);
else
key = PyTuple_Pack(2, Py_TYPE(op), op);
if (d == 0.0 && copysign(1.0, d) < 0.0) {
PyObject *items[] = {(PyObject *)Py_TYPE(op), op, Py_None};
key = PyTuple_FromArray(items, 3);
}
else {
PyObject *items[] = {(PyObject *)Py_TYPE(op), op};
key = PyTuple_FromArray(items, 2);
}
}
else if (PyComplex_CheckExact(op)) {
Py_complex z;
Expand All @@ -3075,16 +3080,20 @@ _PyCode_ConstantKey(PyObject *op)
/* use True, False and None singleton as tags for the real and imag
* sign, to make tuples different */
if (real_negzero && imag_negzero) {
key = PyTuple_Pack(3, Py_TYPE(op), op, Py_True);
PyObject *items[] = {(PyObject *)Py_TYPE(op), op, Py_True};
key = PyTuple_FromArray(items, 3);
}
else if (imag_negzero) {
key = PyTuple_Pack(3, Py_TYPE(op), op, Py_False);
PyObject *items[] = {(PyObject *)Py_TYPE(op), op, Py_False};
key = PyTuple_FromArray(items, 3);
}
else if (real_negzero) {
key = PyTuple_Pack(3, Py_TYPE(op), op, Py_None);
PyObject *items[] = {(PyObject *)Py_TYPE(op), op, Py_None};
key = PyTuple_FromArray(items, 3);
}
else {
key = PyTuple_Pack(2, Py_TYPE(op), op);
PyObject *items[] = {(PyObject *)Py_TYPE(op), op};
key = PyTuple_FromArray(items, 2);
}
}
else if (PyTuple_CheckExact(op)) {
Expand All @@ -3109,7 +3118,8 @@ _PyCode_ConstantKey(PyObject *op)
PyTuple_SET_ITEM(tuple, i, item_key);
}

key = PyTuple_Pack(2, tuple, op);
PyObject *tuple_items[] = {tuple, op};
key = PyTuple_FromArray(tuple_items, 2);
Py_DECREF(tuple);
}
else if (PyFrozenSet_CheckExact(op)) {
Expand Down Expand Up @@ -3143,7 +3153,8 @@ _PyCode_ConstantKey(PyObject *op)
if (set == NULL)
return NULL;

key = PyTuple_Pack(2, set, op);
PyObject *set_items[] = {set, op};
key = PyTuple_FromArray(set_items, 2);
Py_DECREF(set);
return key;
}
Expand Down Expand Up @@ -3174,7 +3185,8 @@ _PyCode_ConstantKey(PyObject *op)
goto slice_exit;
}

key = PyTuple_Pack(2, slice_key, op);
PyObject *slice_items[] = {slice_key, op};
key = PyTuple_FromArray(slice_items, 2);
Py_DECREF(slice_key);
slice_exit:
Py_XDECREF(start_key);
Expand All @@ -3188,7 +3200,8 @@ _PyCode_ConstantKey(PyObject *op)
if (obj_id == NULL)
return NULL;

key = PyTuple_Pack(2, obj_id, op);
PyObject *id_items[] = {obj_id, op};
key = PyTuple_FromArray(id_items, 2);
Py_DECREF(obj_id);
}
return key;
Expand Down
Loading
Morty Proxy This is a proxified and sanitized view of the page, visit original site.