From 4970f5d6590cda345ee286ced85871532aa27846 Mon Sep 17 00:00:00 2001 From: AlexWaygood Date: Wed, 10 May 2023 19:36:05 +0100 Subject: [PATCH 1/3] Optimise `dataclasses.asdict` for the common case Co-authored-by: David Ellis --- Lib/dataclasses.py | 17 ++++++++++++----- ...23-05-10-19-33-36.gh-issue-103000.j0KSfD.rst | 2 ++ 2 files changed, 14 insertions(+), 5 deletions(-) create mode 100644 Misc/NEWS.d/next/Library/2023-05-10-19-33-36.gh-issue-103000.j0KSfD.rst diff --git a/Lib/dataclasses.py b/Lib/dataclasses.py index b0b8a773b7594f..3eacba840db426 100644 --- a/Lib/dataclasses.py +++ b/Lib/dataclasses.py @@ -1324,11 +1324,18 @@ def _asdict_inner(obj, dict_factory): if type(obj) in _ATOMIC_TYPES: return obj elif _is_dataclass_instance(obj): - result = [] - for f in fields(obj): - value = _asdict_inner(getattr(obj, f.name), dict_factory) - result.append((f.name, value)) - return dict_factory(result) + # fast path for the common case + if dict_factory is dict: + return { + f.name: _asdict_inner(getattr(obj, f.name), dict) + for f in fields(obj) + } + else: + result = [] + for f in fields(obj): + value = _asdict_inner(getattr(obj, f.name), dict_factory) + result.append((f.name, value)) + return dict_factory(result) elif isinstance(obj, tuple) and hasattr(obj, '_fields'): # obj is a namedtuple. Recurse into it, but the returned # object is another namedtuple of the same type. This is diff --git a/Misc/NEWS.d/next/Library/2023-05-10-19-33-36.gh-issue-103000.j0KSfD.rst b/Misc/NEWS.d/next/Library/2023-05-10-19-33-36.gh-issue-103000.j0KSfD.rst new file mode 100644 index 00000000000000..e88481495c2208 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2023-05-10-19-33-36.gh-issue-103000.j0KSfD.rst @@ -0,0 +1,2 @@ +Improve performance of :func:`dataclasses.asdict` for the common case where +`dict_factory` is `dict`. From 29231d5a6537a499e605f497754d55b074c2a0d3 Mon Sep 17 00:00:00 2001 From: Alex Waygood Date: Wed, 10 May 2023 19:45:15 +0100 Subject: [PATCH 2/3] Update 2023-05-10-19-33-36.gh-issue-103000.j0KSfD.rst --- .../next/Library/2023-05-10-19-33-36.gh-issue-103000.j0KSfD.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Misc/NEWS.d/next/Library/2023-05-10-19-33-36.gh-issue-103000.j0KSfD.rst b/Misc/NEWS.d/next/Library/2023-05-10-19-33-36.gh-issue-103000.j0KSfD.rst index e88481495c2208..aeaba2b55e5661 100644 --- a/Misc/NEWS.d/next/Library/2023-05-10-19-33-36.gh-issue-103000.j0KSfD.rst +++ b/Misc/NEWS.d/next/Library/2023-05-10-19-33-36.gh-issue-103000.j0KSfD.rst @@ -1,2 +1,2 @@ Improve performance of :func:`dataclasses.asdict` for the common case where -`dict_factory` is `dict`. +`dict_factory` is `dict`. Patch by David C Ellis. From 4e01d46cfaf9c797a940efc48c29db11a1bf7fcd Mon Sep 17 00:00:00 2001 From: Alex Waygood Date: Wed, 10 May 2023 19:48:02 +0100 Subject: [PATCH 3/3] this isn't markdown --- .../next/Library/2023-05-10-19-33-36.gh-issue-103000.j0KSfD.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Misc/NEWS.d/next/Library/2023-05-10-19-33-36.gh-issue-103000.j0KSfD.rst b/Misc/NEWS.d/next/Library/2023-05-10-19-33-36.gh-issue-103000.j0KSfD.rst index aeaba2b55e5661..f84ec5c8b3caf6 100644 --- a/Misc/NEWS.d/next/Library/2023-05-10-19-33-36.gh-issue-103000.j0KSfD.rst +++ b/Misc/NEWS.d/next/Library/2023-05-10-19-33-36.gh-issue-103000.j0KSfD.rst @@ -1,2 +1,2 @@ Improve performance of :func:`dataclasses.asdict` for the common case where -`dict_factory` is `dict`. Patch by David C Ellis. +*dict_factory* is ``dict``. Patch by David C Ellis. pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy