From cea2d2475d3eec9f4fd350ef9eb2ba43da1943a5 Mon Sep 17 00:00:00 2001 From: Heikki Toivonen <308110+heikkitoivonen@users.noreply.github.com> Date: Thu, 8 Jan 2026 07:28:02 -0800 Subject: [PATCH] gh-143445: Optimize deepcopy for 1.04x speedup (#143449) MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit 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 | 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 fff7e93c2a1b..4c024ab5311d 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 671fcf88c75a..63ddfb89071c 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 000000000000..f5dea2e49afe --- /dev/null +++ b/Misc/NEWS.d/next/Library/2026-01-05-12-20-42.gh-issue-143445.rgxnbL.rst @@ -0,0 +1 @@ +Speed up :func:`copy.deepcopy` by 1.04x. -- 2.47.3