Skip to content

Commit 6ceb409

Browse files
committed
Use the _atomic_types global directly without capturing it
The benchmark shows the speedup comes entirely from the inlined type(...) in _atomic_types check, not from binding the global to a local default argument, so drop the local capture for a minimal change.
1 parent 8b8b7e8 commit 6ceb409

1 file changed

Lines changed: 7 additions & 7 deletions

File tree

Lib/copy.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -169,20 +169,20 @@ def deepcopy(x, memo=None):
169169
_deepcopy_dispatch = d = {}
170170

171171

172-
def _deepcopy_list(x, memo, deepcopy=deepcopy, _atomic=_atomic_types):
172+
def _deepcopy_list(x, memo, deepcopy=deepcopy):
173173
y = []
174174
memo[id(x)] = y
175175
append = y.append
176176
for a in x:
177177
# Inline the atomic-type check so atomic elements (int, str, None, ...)
178178
# skip the deepcopy() call overhead entirely; deepcopy() would just
179179
# return them unchanged after the same check.
180-
append(a if type(a) in _atomic else deepcopy(a, memo))
180+
append(a if type(a) in _atomic_types else deepcopy(a, memo))
181181
return y
182182
d[list] = _deepcopy_list
183183

184-
def _deepcopy_tuple(x, memo, deepcopy=deepcopy, _atomic=_atomic_types):
185-
y = [a if type(a) in _atomic else deepcopy(a, memo) for a in x]
184+
def _deepcopy_tuple(x, memo, deepcopy=deepcopy):
185+
y = [a if type(a) in _atomic_types else deepcopy(a, memo) for a in x]
186186
# We're not going to put the tuple in the memo, but it's still important we
187187
# check for it, in case the tuple contains recursive mutable structures.
188188
try:
@@ -198,15 +198,15 @@ def _deepcopy_tuple(x, memo, deepcopy=deepcopy, _atomic=_atomic_types):
198198
return y
199199
d[tuple] = _deepcopy_tuple
200200

201-
def _deepcopy_dict(x, memo, deepcopy=deepcopy, _atomic=_atomic_types):
201+
def _deepcopy_dict(x, memo, deepcopy=deepcopy):
202202
y = {}
203203
memo[id(x)] = y
204204
for key, value in x.items():
205205
# Inline the atomic-type check for keys and values: atomic objects
206206
# (str keys, int/str/None values, ...) are returned as-is by
207207
# deepcopy(), so skip the per-item call in that common case.
208-
y[key if type(key) in _atomic else deepcopy(key, memo)] = (
209-
value if type(value) in _atomic else deepcopy(value, memo))
208+
y[key if type(key) in _atomic_types else deepcopy(key, memo)] = (
209+
value if type(value) in _atomic_types else deepcopy(value, memo))
210210
return y
211211
d[dict] = _deepcopy_dict
212212

0 commit comments

Comments
 (0)