From dc3a3639f76a428215b54fd40c9d2929a7792b01 Mon Sep 17 00:00:00 2001 From: Nishant Date: Thu, 1 Oct 2026 03:53:00 +0530 Subject: [PATCH 1/4] gh-158525: Avoid reacquiring dict lock when copying --- .../2026-10-01-00-00-00.gh-issue-158525.Xw3K9a.rst | 2 ++ Objects/dictobject.c | 9 ++++++++- 2 files changed, 10 insertions(+), 1 deletion(-) create mode 100644 Misc/NEWS.d/next/Core_and_Builtins/2026-10-01-00-00-00.gh-issue-158525.Xw3K9a.rst diff --git a/Misc/NEWS.d/next/Core_and_Builtins/2026-10-01-00-00-00.gh-issue-158525.Xw3K9a.rst b/Misc/NEWS.d/next/Core_and_Builtins/2026-10-01-00-00-00.gh-issue-158525.Xw3K9a.rst new file mode 100644 index 00000000000000..43e5bcbf1817b0 --- /dev/null +++ b/Misc/NEWS.d/next/Core_and_Builtins/2026-10-01-00-00-00.gh-issue-158525.Xw3K9a.rst @@ -0,0 +1,2 @@ +Avoid reacquiring the source dictionary lock when copying a sparse dictionary +in the free-threaded build. diff --git a/Objects/dictobject.c b/Objects/dictobject.c index 15377ac083c2b2..d57f63512df832 100644 --- a/Objects/dictobject.c +++ b/Objects/dictobject.c @@ -4575,7 +4575,14 @@ copy_lock_held_untracked(PyObject *o, int as_frozendict) } if (copy == NULL) return NULL; - if (dict_merge(copy, o, 1, NULL) < 0) { + int res; + if (!PyFrozenDict_Check(o) && Py_TYPE(mp)->tp_iter == dict_iter) { + res = dict_dict_merge((PyDictObject *)copy, mp, 1, NULL); + } + else { + res = dict_merge(copy, o, 1, NULL); + } + if (res < 0) { Py_DECREF(copy); return NULL; } From eff9e945d8667b9c4cd790972a7cad61cdd071a3 Mon Sep 17 00:00:00 2001 From: Nishant Date: Tue, 20 Oct 2026 15:06:01 +0530 Subject: [PATCH 2/4] gh-158525: Allow frozendicts in the direct copy merge path Skip the source lock assertion for immutable frozendicts, matching the other copy helpers. Keep the generic merge path for subclasses that override iteration and test that their copy order is preserved. --- Lib/test/test_dict.py | 14 ++++++++++++++ Objects/dictobject.c | 6 ++++-- 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/Lib/test/test_dict.py b/Lib/test/test_dict.py index 3b9ff672a1aa03..9ab0f994045ca7 100644 --- a/Lib/test/test_dict.py +++ b/Lib/test/test_dict.py @@ -1894,6 +1894,20 @@ def test_copy(self): self.assertEqual(d2, frozendict(x=1, y=2)) self.assertEqual(type(d2), frozendict) + def test_copy_iter_override(self): + class CustomReversedDict(frozendict): + def keys(self): + return reversed(list(frozendict.keys(self))) + + __iter__ = keys + + pairs = [('a', 1), ('b', 2), ('c', 3)] + d = CustomReversedDict(pairs) + d2 = d.copy() + self.assertIsNot(d2, d) + self.assertEqual(type(d2), frozendict) + self.assertEqual(list(d2.items()), pairs[::-1]) + def test_merge(self): # test "a | b" operator self.assertEqual(frozendict(x=1) | frozendict(y=2), diff --git a/Objects/dictobject.c b/Objects/dictobject.c index d57f63512df832..e5bf7c0da4b05d 100644 --- a/Objects/dictobject.c +++ b/Objects/dictobject.c @@ -4209,7 +4209,9 @@ static int dict_dict_merge(PyDictObject *mp, PyDictObject *other, int override, PyObject **dupkey) { assert(can_modify_dict(mp)); - ASSERT_DICT_LOCKED(other); + if (!PyFrozenDict_Check(other)) { + ASSERT_DICT_LOCKED(other); + } if (other == mp || other->ma_used == 0) /* a.update(a) or a.update({}); nothing to do */ @@ -4576,7 +4578,7 @@ copy_lock_held_untracked(PyObject *o, int as_frozendict) if (copy == NULL) return NULL; int res; - if (!PyFrozenDict_Check(o) && Py_TYPE(mp)->tp_iter == dict_iter) { + if (Py_TYPE(mp)->tp_iter == dict_iter) { res = dict_dict_merge((PyDictObject *)copy, mp, 1, NULL); } else { From 37ea286433838be9243923f7c9f8bd3a9db8a106 Mon Sep 17 00:00:00 2001 From: Kumar Aditya Date: Mon, 9 Nov 2026 19:05:46 +0530 Subject: [PATCH 3/4] Apply suggestion from @kumaraditya303 --- .../2026-10-01-00-00-00.gh-issue-158525.Xw3K9a.rst | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/Misc/NEWS.d/next/Core_and_Builtins/2026-10-01-00-00-00.gh-issue-158525.Xw3K9a.rst b/Misc/NEWS.d/next/Core_and_Builtins/2026-10-01-00-00-00.gh-issue-158525.Xw3K9a.rst index 43e5bcbf1817b0..ae7e4a0faf1c38 100644 --- a/Misc/NEWS.d/next/Core_and_Builtins/2026-10-01-00-00-00.gh-issue-158525.Xw3K9a.rst +++ b/Misc/NEWS.d/next/Core_and_Builtins/2026-10-01-00-00-00.gh-issue-158525.Xw3K9a.rst @@ -1,2 +1 @@ -Avoid reacquiring the source dictionary lock when copying a sparse dictionary -in the free-threaded build. +Fix slow `dict.copy()` for dicts with many deleted entries in the free-threaded build. From b662b62d71cb8189d06ab2baa8dc96e696affa89 Mon Sep 17 00:00:00 2001 From: Pieter Eendebak Date: Mon, 9 Nov 2026 15:07:48 +0100 Subject: [PATCH 4/4] Update Misc/NEWS.d/next/Core_and_Builtins/2026-10-01-00-00-00.gh-issue-158525.Xw3K9a.rst --- .../2026-10-01-00-00-00.gh-issue-158525.Xw3K9a.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Misc/NEWS.d/next/Core_and_Builtins/2026-10-01-00-00-00.gh-issue-158525.Xw3K9a.rst b/Misc/NEWS.d/next/Core_and_Builtins/2026-10-01-00-00-00.gh-issue-158525.Xw3K9a.rst index ae7e4a0faf1c38..18ab4418020d1f 100644 --- a/Misc/NEWS.d/next/Core_and_Builtins/2026-10-01-00-00-00.gh-issue-158525.Xw3K9a.rst +++ b/Misc/NEWS.d/next/Core_and_Builtins/2026-10-01-00-00-00.gh-issue-158525.Xw3K9a.rst @@ -1 +1 @@ -Fix slow `dict.copy()` for dicts with many deleted entries in the free-threaded build. +Fix slow ``dict.copy()`` for dicts with many deleted entries in the free-threaded build.