From: Heikki Toivonen <308110+heikkitoivonen@users.noreply.github.com> Date: Thu, 8 Jan 2026 15:28:02 +0000 (-0800) Subject: gh-143445: Optimize deepcopy for 1.04x speedup (#143449) X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=cea2d2475d3eec9f4fd350ef9eb2ba43da1943a5;p=thirdparty%2FPython%2Fcpython.git gh-143445: Optimize deepcopy for 1.04x speedup (#143449) 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> --- 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.