From d951b17e66032285b7418d233afee3aee6055922 Mon Sep 17 00:00:00 2001 From: Pieter Eendebak Date: Fri, 20 Dec 2024 09:45:08 +0100 Subject: [PATCH 1/4] Refactor copy.copy --- Lib/copy.py | 27 +++++++++------------------ 1 file changed, 9 insertions(+), 18 deletions(-) diff --git a/Lib/copy.py b/Lib/copy.py index f27e109973cfb7..628d0c7c6360f5 100644 --- a/Lib/copy.py +++ b/Lib/copy.py @@ -67,13 +67,15 @@ def copy(x): cls = type(x) - copier = _copy_dispatch.get(cls) - if copier: - return copier(x) + if cls in _copy_atomic_types: + return x + if cls in _copy_builtin_containers: + return cls.copy(x) + if issubclass(cls, type): # treat it as a regular class: - return _copy_immutable(x) + return x copier = getattr(cls, "__copy__", None) if copier is not None: @@ -98,23 +100,12 @@ def copy(x): return _reconstruct(x, None, *rv) -_copy_dispatch = d = {} - -def _copy_immutable(x): - return x -for t in (types.NoneType, int, float, bool, complex, str, tuple, +_copy_atomic_types = (types.NoneType, int, float, bool, complex, str, tuple, bytes, frozenset, type, range, slice, property, types.BuiltinFunctionType, types.EllipsisType, types.NotImplementedType, types.FunctionType, types.CodeType, - weakref.ref, super): - d[t] = _copy_immutable - -d[list] = list.copy -d[dict] = dict.copy -d[set] = set.copy -d[bytearray] = bytearray.copy - -del d, t + weakref.ref, super) +_copy_builtin_containers = (list, dict, set, bytearray) def deepcopy(x, memo=None, _nil=[]): """Deep copy operation on arbitrary Python objects. From 742aa885bd3176b8cf800504a93d9597a8c883c4 Mon Sep 17 00:00:00 2001 From: Pieter Eendebak Date: Fri, 20 Dec 2024 10:38:52 +0100 Subject: [PATCH 2/4] use set instead of tuple --- Lib/copy.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/Lib/copy.py b/Lib/copy.py index 628d0c7c6360f5..c64fc0761793f5 100644 --- a/Lib/copy.py +++ b/Lib/copy.py @@ -100,12 +100,12 @@ def copy(x): return _reconstruct(x, None, *rv) -_copy_atomic_types = (types.NoneType, int, float, bool, complex, str, tuple, +_copy_atomic_types = {types.NoneType, int, float, bool, complex, str, tuple, bytes, frozenset, type, range, slice, property, types.BuiltinFunctionType, types.EllipsisType, types.NotImplementedType, types.FunctionType, types.CodeType, - weakref.ref, super) -_copy_builtin_containers = (list, dict, set, bytearray) + weakref.ref, super} +_copy_builtin_containers = {list, dict, set, bytearray} def deepcopy(x, memo=None, _nil=[]): """Deep copy operation on arbitrary Python objects. From c2e6a4a378a0bcbef124c8641c3e942960ce31fd Mon Sep 17 00:00:00 2001 From: "blurb-it[bot]" <43283697+blurb-it[bot]@users.noreply.github.com> Date: Fri, 20 Dec 2024 10:57:14 +0000 Subject: [PATCH 3/4] =?UTF-8?q?=F0=9F=93=9C=F0=9F=A4=96=20Added=20by=20blu?= =?UTF-8?q?rb=5Fit.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../next/Library/2024-12-20-10-57-10.gh-issue-128118.mYak8i.rst | 1 + 1 file changed, 1 insertion(+) create mode 100644 Misc/NEWS.d/next/Library/2024-12-20-10-57-10.gh-issue-128118.mYak8i.rst diff --git a/Misc/NEWS.d/next/Library/2024-12-20-10-57-10.gh-issue-128118.mYak8i.rst b/Misc/NEWS.d/next/Library/2024-12-20-10-57-10.gh-issue-128118.mYak8i.rst new file mode 100644 index 00000000000000..2bd2f82ad423b7 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2024-12-20-10-57-10.gh-issue-128118.mYak8i.rst @@ -0,0 +1 @@ +Improve performance of :func:`copy.copy` by adding a fast path for atomic types and container types. From 9490a2383ad8971c21187f17397b27826e243e6b Mon Sep 17 00:00:00 2001 From: Pieter Eendebak Date: Thu, 26 Dec 2024 22:31:39 +0100 Subject: [PATCH 4/4] Update Misc/NEWS.d/next/Library/2024-12-20-10-57-10.gh-issue-128118.mYak8i.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> --- .../Library/2024-12-20-10-57-10.gh-issue-128118.mYak8i.rst | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Misc/NEWS.d/next/Library/2024-12-20-10-57-10.gh-issue-128118.mYak8i.rst b/Misc/NEWS.d/next/Library/2024-12-20-10-57-10.gh-issue-128118.mYak8i.rst index 2bd2f82ad423b7..bc2898edfda721 100644 --- a/Misc/NEWS.d/next/Library/2024-12-20-10-57-10.gh-issue-128118.mYak8i.rst +++ b/Misc/NEWS.d/next/Library/2024-12-20-10-57-10.gh-issue-128118.mYak8i.rst @@ -1 +1,2 @@ -Improve performance of :func:`copy.copy` by adding a fast path for atomic types and container types. +Improve performance of :func:`copy.copy` by 30% via +a fast path for atomic types and container types.