]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
gh-143445: Optimize deepcopy for 1.04x speedup (#143449)
authorHeikki Toivonen <308110+heikkitoivonen@users.noreply.github.com>
Thu, 8 Jan 2026 15:28:02 +0000 (07:28 -0800)
committerGitHub <noreply@github.com>
Thu, 8 Jan 2026 15:28:02 +0000 (16:28 +0100)
Gains according to pyperformance:

```
deepcopy:
Mean +- std dev: 411 us +- 2 us -> 396 us +- 3 us: 1.04x faster
Significant (t=28.94)

deepcopy_reduce:
Mean +- std dev: 4.38 us +- 0.05 us -> 4.23 us +- 0.04 us: 1.04x faster
Significant (t=20.05)
```

Co-authored-by: Bénédikt Tran <10796600+picnixz@users.noreply.github.com>
Lib/copy.py
Misc/ACKS
Misc/NEWS.d/next/Library/2026-01-05-12-20-42.gh-issue-143445.rgxnbL.rst [new file with mode: 0644]

index fff7e93c2a1b893fa9adf65eb76953b513baf5e7..4c024ab5311d2ddcde0391f05bfcdcd5cd6668cd 100644 (file)
@@ -230,7 +230,7 @@ def _reconstruct(x, memo, func, args,
                  *, deepcopy=deepcopy):
     deep = memo is not None
     if deep and args:
-        args = (deepcopy(arg, memo) for arg in args)
+        args = [deepcopy(arg, memo) for arg in args]
     y = func(*args)
     if deep:
         memo[id(x)] = y
index 671fcf88c75af9c20aa45487a5a07e55a82a34e0..63ddfb89071c0b42835353b04ffc6476f27b4b70 100644 (file)
--- a/Misc/ACKS
+++ b/Misc/ACKS
@@ -1931,6 +1931,7 @@ James Tocknell
 Bennett Todd
 R Lindsay Todd
 Eugene Toder
+Heikki Toivonen
 Erik Tollerud
 Stephen Tonkin
 Matias Torchinsky
diff --git a/Misc/NEWS.d/next/Library/2026-01-05-12-20-42.gh-issue-143445.rgxnbL.rst b/Misc/NEWS.d/next/Library/2026-01-05-12-20-42.gh-issue-143445.rgxnbL.rst
new file mode 100644 (file)
index 0000000..f5dea2e
--- /dev/null
@@ -0,0 +1 @@
+Speed up :func:`copy.deepcopy` by 1.04x.