Skip to content

Commit 6a7529d

Browse files
committed
Switch deepcopy memo check to d in memo
pochmann benchmarked this issue's candidate fixes on gh-154594 and found the try/except form is ~3.5x slower on a memo miss (the common case in real deepcopy workloads) despite being fastest on a hit. `d in memo` ties or beats the current `is not None` check on both hit and miss, and matches where the competing PR (gh-154595) landed after the same discussion.
1 parent 3660617 commit 6a7529d

1 file changed

Lines changed: 2 additions & 5 deletions

File tree

Lib/copy.py

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -121,11 +121,8 @@ def deepcopy(x, memo=None):
121121
d = id(x)
122122
if memo is None:
123123
memo = {}
124-
else:
125-
try:
126-
return memo[d]
127-
except KeyError:
128-
pass
124+
elif d in memo:
125+
return memo[d]
129126

130127
copier = _deepcopy_dispatch.get(cls)
131128
if copier is not None:

0 commit comments

Comments
 (0)