]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
gh-132657: improve `deepcopy` and `copy` scaling on free-threading (#138429)
authorPieter Eendebak <pieter.eendebak@gmail.com>
Thu, 4 Sep 2025 07:50:23 +0000 (09:50 +0200)
committerGitHub <noreply@github.com>
Thu, 4 Sep 2025 07:50:23 +0000 (13:20 +0530)
Lib/copy.py
Misc/NEWS.d/next/Library/2025-09-03-09-03-11.gh-issue-132657.cbAIDh.rst [new file with mode: 0644]

index c64fc0761793f54847882d46a4368e3f34690592..fff7e93c2a1b893fa9adf65eb76953b513baf5e7 100644 (file)
@@ -100,14 +100,14 @@ def copy(x):
     return _reconstruct(x, None, *rv)
 
 
-_copy_atomic_types = {types.NoneType, int, float, bool, complex, str, tuple,
+_copy_atomic_types = frozenset({types.NoneType, int, float, bool, complex, str, tuple,
           bytes, frozenset, type, range, slice, property,
           types.BuiltinFunctionType, types.EllipsisType,
           types.NotImplementedType, types.FunctionType, types.CodeType,
-          weakref.ref, super}
-_copy_builtin_containers = {list, dict, set, bytearray}
+          weakref.ref, super})
+_copy_builtin_containers = frozenset({list, dict, set, bytearray})
 
-def deepcopy(x, memo=None, _nil=[]):
+def deepcopy(x, memo=None):
     """Deep copy operation on arbitrary Python objects.
 
     See the module's __doc__ string for more info.
@@ -122,8 +122,8 @@ def deepcopy(x, memo=None, _nil=[]):
     if memo is None:
         memo = {}
     else:
-        y = memo.get(d, _nil)
-        if y is not _nil:
+        y = memo.get(d, None)
+        if y is not None:
             return y
 
     copier = _deepcopy_dispatch.get(cls)
@@ -162,9 +162,9 @@ def deepcopy(x, memo=None, _nil=[]):
         _keep_alive(x, memo) # Make sure x lives at least as long as d
     return y
 
-_atomic_types =  {types.NoneType, types.EllipsisType, types.NotImplementedType,
+_atomic_types = frozenset({types.NoneType, types.EllipsisType, types.NotImplementedType,
           int, float, bool, complex, bytes, str, types.CodeType, type, range,
-          types.BuiltinFunctionType, types.FunctionType, weakref.ref, property}
+          types.BuiltinFunctionType, types.FunctionType, weakref.ref, property})
 
 _deepcopy_dispatch = d = {}
 
diff --git a/Misc/NEWS.d/next/Library/2025-09-03-09-03-11.gh-issue-132657.cbAIDh.rst b/Misc/NEWS.d/next/Library/2025-09-03-09-03-11.gh-issue-132657.cbAIDh.rst
new file mode 100644 (file)
index 0000000..8f07f8f
--- /dev/null
@@ -0,0 +1 @@
+Improve the scaling of :func:`copy.copy` and :func:`copy.deepcopy` in the free-threading build.