From 174a82b575f91ec17d879707b33f4c62d8be8905 Mon Sep 17 00:00:00 2001 From: Rashesh Tripathy Date: Fri, 6 Feb 2026 04:06:33 +0530 Subject: [PATCH 1/2] gh-140009: Optimize dict object by replacing PyTuple_Pack with PyTuple_FromArray --- .../2026-02-06-03-40-59.gh-issue-140009.a1b2c3.rst | 1 + Objects/dictobject.c | 6 ++++-- 2 files changed, 5 insertions(+), 2 deletions(-) create mode 100644 Misc/NEWS.d/next/Core_and_Builtins/2026-02-06-03-40-59.gh-issue-140009.a1b2c3.rst diff --git a/Misc/NEWS.d/next/Core_and_Builtins/2026-02-06-03-40-59.gh-issue-140009.a1b2c3.rst b/Misc/NEWS.d/next/Core_and_Builtins/2026-02-06-03-40-59.gh-issue-140009.a1b2c3.rst new file mode 100644 index 00000000000000..77b0aba14548a6 --- /dev/null +++ b/Misc/NEWS.d/next/Core_and_Builtins/2026-02-06-03-40-59.gh-issue-140009.a1b2c3.rst @@ -0,0 +1 @@ +Improve performance of dict object by replacing PyTuple_Pack with PyTuple_FromArray for small tuples. diff --git a/Objects/dictobject.c b/Objects/dictobject.c index c1584be3f0ed4a..907d1b59e1d8e8 100644 --- a/Objects/dictobject.c +++ b/Objects/dictobject.c @@ -5083,7 +5083,8 @@ dictiter_new(PyDictObject *dict, PyTypeObject *itertype) } if (itertype == &PyDictIterItem_Type || itertype == &PyDictRevIterItem_Type) { - di->di_result = PyTuple_Pack(2, Py_None, Py_None); + PyObject *items[] = {Py_None, Py_None}; + di->di_result = PyTuple_FromArray(items, 2); if (di->di_result == NULL) { Py_DECREF(di); return NULL; @@ -6284,7 +6285,8 @@ dictitems_xor_lock_held(PyObject *d1, PyObject *d2) } } else { - PyObject *pair = PyTuple_Pack(2, key, val2); + PyObject *items[] = {key, val2}; + PyObject *pair = PyTuple_FromArray(items, 2); if (pair == NULL) { goto error; } From df80780056449e5f05faaf6dec78592c0e67e6a8 Mon Sep 17 00:00:00 2001 From: Rashesh Tripathy Date: Fri, 6 Feb 2026 04:16:22 +0530 Subject: [PATCH 2/2] Restart CI tests after PR title update