From 35c3168b82f87cb55a7165f6e7ecc50152d05b67 Mon Sep 17 00:00:00 2001 From: Heikki Toivonen <308110+heikkitoivonen@users.noreply.github.com> Date: Mon, 5 Jan 2026 12:22:01 -0800 Subject: [PATCH 1/2] Optimize deepcopy for 1.04x speedup 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) ``` --- Lib/copy.py | 2 +- Misc/ACKS | 1 + .../next/Library/2026-01-05-12-20-42.gh-issue-143445.rgxnbL.rst | 1 + 3 files changed, 3 insertions(+), 1 deletion(-) create mode 100644 Misc/NEWS.d/next/Library/2026-01-05-12-20-42.gh-issue-143445.rgxnbL.rst diff --git a/Lib/copy.py b/Lib/copy.py index fff7e93c2a1b89..4c024ab5311d2d 100644 --- a/Lib/copy.py +++ b/Lib/copy.py @@ -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 diff --git a/Misc/ACKS b/Misc/ACKS index 671fcf88c75af9..63ddfb89071c0b 100644 --- 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 index 00000000000000..918d1a6dc38ed3 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2026-01-05-12-20-42.gh-issue-143445.rgxnbL.rst @@ -0,0 +1 @@ +Optimize deepcopy in :mod:`copy`, pyperformance reports 1.04x speedup. From ef2d3ea5bba0e984b23bdcec921f6bfdf131545e Mon Sep 17 00:00:00 2001 From: Heikki Toivonen <308110+heikkitoivonen@users.noreply.github.com> Date: Mon, 5 Jan 2026 15:18:53 -0800 Subject: [PATCH 2/2] Update Misc/NEWS.d/next/Library/2026-01-05-12-20-42.gh-issue-143445.rgxnbL.rst MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Bénédikt Tran <10796600+picnixz@users.noreply.github.com> --- .../next/Library/2026-01-05-12-20-42.gh-issue-143445.rgxnbL.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 index 918d1a6dc38ed3..f5dea2e49afe2a 100644 --- 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 @@ -1 +1 @@ -Optimize deepcopy in :mod:`copy`, pyperformance reports 1.04x speedup. +Speed up :func:`copy.deepcopy` by 1.04x.