Skip to content

Commit e46d403

Browse files
authored
gh-132657: improve deepcopy and copy scaling on free-threading (#138429)
1 parent 06f8d7a commit e46d403

File tree

2 files changed

+9
-8
lines changed

2 files changed

+9
-8
lines changed

Lib/copy.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -100,14 +100,14 @@ def copy(x):
100100
return _reconstruct(x, None, *rv)
101101

102102

103-
_copy_atomic_types = {types.NoneType, int, float, bool, complex, str, tuple,
103+
_copy_atomic_types = frozenset({types.NoneType, int, float, bool, complex, str, tuple,
104104
bytes, frozenset, type, range, slice, property,
105105
types.BuiltinFunctionType, types.EllipsisType,
106106
types.NotImplementedType, types.FunctionType, types.CodeType,
107-
weakref.ref, super}
108-
_copy_builtin_containers = {list, dict, set, bytearray}
107+
weakref.ref, super})
108+
_copy_builtin_containers = frozenset({list, dict, set, bytearray})
109109

110-
def deepcopy(x, memo=None, _nil=[]):
110+
def deepcopy(x, memo=None):
111111
"""Deep copy operation on arbitrary Python objects.
112112
113113
See the module's __doc__ string for more info.
@@ -122,8 +122,8 @@ def deepcopy(x, memo=None, _nil=[]):
122122
if memo is None:
123123
memo = {}
124124
else:
125-
y = memo.get(d, _nil)
126-
if y is not _nil:
125+
y = memo.get(d, None)
126+
if y is not None:
127127
return y
128128

129129
copier = _deepcopy_dispatch.get(cls)
@@ -162,9 +162,9 @@ def deepcopy(x, memo=None, _nil=[]):
162162
_keep_alive(x, memo) # Make sure x lives at least as long as d
163163
return y
164164

165-
_atomic_types = {types.NoneType, types.EllipsisType, types.NotImplementedType,
165+
_atomic_types = frozenset({types.NoneType, types.EllipsisType, types.NotImplementedType,
166166
int, float, bool, complex, bytes, str, types.CodeType, type, range,
167-
types.BuiltinFunctionType, types.FunctionType, weakref.ref, property}
167+
types.BuiltinFunctionType, types.FunctionType, weakref.ref, property})
168168

169169
_deepcopy_dispatch = d = {}
170170

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Improve the scaling of :func:`copy.copy` and :func:`copy.deepcopy` in the free-threading build.

0 commit comments

Comments
 (0)