GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing github.com — Dataclasses - Improve the performance of asdict/astuple for common types and default values · Issue #103000 · python/cpython Closed Labels 3.12only security fixesonly security fixesperformancePerformance or resource usagePerformance or resource usagestdlibStandard Library Python modules in the Lib/ directoryStandard Library Python modules in the Lib/ directorytype-featureA feature request or enhancementA feature request or enhancement DescriptionFeature or enhancementImprove the performance of asdict/astuple in common cases by making a shortcut for common types that are unaffected by deepcopy in the inner loop. Also special casing for the default The goal here is to improve performance in common cases without significantly impacting less common cases, while not changing the API or output in any way. PitchIn cases where a dataclass contains a lot of data of common python types (eg: bool/str/int/float) currently the inner loops for It is much faster for these cases to instead check for them at the first opportunity and shortcut their return, skipping the recursive call and all of the other comparisons. In the case where this is being used to prepare an object to serialize to JSON this can be quite significant as this covers most of the remaining types handled by the stdlib Note: Anything that skips deepcopy with this alteration is already unchanged as Currently when constructing the Previous discussionDiscussed here with a few more details and earlier examples: https://discuss.python.org/t/dataclasses-make-asdict-astuple-faster-by-skipping-deepcopy-for-objects-where-deepcopy-obj-is-obj/24662 Code DetailsTypes to skip deepcopyThis is the current set of types to be checked for and shortcut returned, ordered in a way that I think makes more sense for # Types for which deepcopy(obj) is known to return obj unmodified # Used to skip deepcopy in asdict and astuple for performance _ATOMIC_TYPES = { # Common JSON Serializable types types.NoneType, bool, int, float, complex, bytes, str, # Other types that are also unaffected by deepcopy types.EllipsisType, types.NotImplementedType, types.CodeType, types.BuiltinFunctionType, types.FunctionType, type, range, property, # weakref.ref, # weakref is not currently imported by dataclasses directly } Function changesWith that added the change is essentially replacing each instance of _asdict_inner(v, dict_factory) inside v if type(v) in _ATOMIC_TYPES else _asdict_inner(v, dict_factory) Instances of subclasses of these types are not guaranteed to have Performance testsTest file: https://gist.github.com/DavidCEllis/a2c2ceeeeda2d1ac509fb8877e5fb60d Results on my development machine (not a perfectly stable test machine, but these differences are large enough). MainCurrent Main python branch: ModifiedLinked PRsReacted by Carl Meyer and Alex Waygood Activity
MetadataMetadataAssigneesLabels3.12only security fixesonly security fixesperformancePerformance or resource usagePerformance or resource usagestdlibStandard Library Python modules in the Lib/ directoryStandard Library Python modules in the Lib/ directorytype-featureA feature request or enhancementA feature request or enhancement ProjectsMilestoneRelationshipsDevelopmentIssue actions |
| You are visitor 01402503 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |