Title: gh-98169 dataclasses.astuple support DefaultDict by kwsp · Pull Request #98170 · python/cpython · GitHub
Open Graph Title: gh-98169 dataclasses.astuple support DefaultDict by kwsp · Pull Request #98170 · python/cpython
X Title: gh-98169 dataclasses.astuple support DefaultDict by kwsp · Pull Request #98170 · python/cpython
Description: Basically applying the same fix for asdict from #32056 to astuple. These two should've been the same PR, but I didn't know about astuple previously. Also modified the handling of DefaultDict in asdict a little so that we only call isinstance(obj, dict) once. This results in a ~3% speed up on my machine on this example: # benchmark from dataclasses import dataclass, asdict from collections import defaultdict from typing import DefaultDict, Dict, List, NamedTuple import time class NT(NamedTuple): x: int y: float @dataclass class C: dd: DefaultDict[str, List] nt: NT d1: Dict[str, List] d2: Dict[str, int] def get_instance(): instance = C(dd=defaultdict(list), nt=NT(1, 1.), d1={}, d2={}) instance.dd["x"].append(12) instance.dd["x"].append(13) instance.d1["y"] = [1, 2, 3] instance.d2["z"] = 1 return instance instance = get_instance() def timeit(callable, n_runs = 100_000, name=""): t1 = time.perf_counter_ns() for _ in range(n_runs): callable() elapsed_ms = round(1e-6 * (time.perf_counter_ns() - t1), 2) print(f"{name}\t{n_runs} runs: {elapsed_ms} ms") timeit(lambda: asdict(instance), name="asdict") timeit(lambda: astuple(instance), name="astuple") Issue: gh-98169
Open Graph Description: Basically applying the same fix for asdict from #32056 to astuple. These two should've been the same PR, but I didn't know about astuple previously. Also modified the handling of DefaultDic...
X Description: Basically applying the same fix for asdict from #32056 to astuple. These two should've been the same PR, but I didn't know about astuple previously. Also modified the handling of De...
Opengraph URL: https://github.com/python/cpython/pull/98170
X: @github
Domain: github.com
| route-pattern | /:user_id/:repository/pull/:id/files(.:format) |
| route-controller | pull_requests |
| route-action | files |
| fetch-nonce | v2:1f3eec6c-08fd-8d1f-f492-ff16007b85b7 |
| current-catalog-service-hash | ae870bc5e265a340912cde392f23dad3671a0a881730ffdadd82f2f57d81641b |
| request-id | AF1C:281585:7991D2:AA55F4:696A2A39 |
| html-safe-nonce | 1904b431b0e7167274dc96ff2482a336f59efa9b6746810f8a4a213de9a3fe25 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJBRjFDOjI4MTU4NTo3OTkxRDI6QUE1NUY0OjY5NkEyQTM5IiwidmlzaXRvcl9pZCI6Ijg1MTI5NTM3OTQ2ODMzNDk1NjEiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | ca6f7983ea79a229eddef0b3d434234e82fb6ced1f8b6cfe2d107836b4ac006b |
| hovercard-subject-tag | pull_request:1082614604 |
| github-keyboard-shortcuts | repository,pull-request-list,pull-request-conversation,pull-request-files-changed,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/python/cpython/pull/98170/files |
| twitter:image | https://avatars.githubusercontent.com/u/30088608?s=400&v=4 |
| twitter:card | summary_large_image |
| og:image | https://avatars.githubusercontent.com/u/30088608?s=400&v=4 |
| og:image:alt | Basically applying the same fix for asdict from #32056 to astuple. These two should've been the same PR, but I didn't know about astuple previously. Also modified the handling of DefaultDic... |
| og:site_name | GitHub |
| og:type | object |
| hostname | github.com |
| expected-hostname | github.com |
| None | ade130928b3890d20ecf9c887468a17e88d5c9a740ea2a85c2c2162e7c58e623 |
| turbo-cache-control | no-preview |
| diff-view | unified |
| go-import | github.com/python/cpython git https://github.com/python/cpython.git |
| octolytics-dimension-user_id | 1525981 |
| octolytics-dimension-user_login | python |
| octolytics-dimension-repository_id | 81598961 |
| octolytics-dimension-repository_nwo | python/cpython |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 81598961 |
| octolytics-dimension-repository_network_root_nwo | python/cpython |
| turbo-body-classes | logged-out env-production page-responsive full-width |
| disable-turbo | true |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | c00842167ca2e5b13e8171812965d503e925ece6 |
| ui-target | canary-1 |
| theme-color | #1e2327 |
| color-scheme | light dark |
Links:
Viewport: width=device-width