import pyperf
setup = """
from dataclasses import dataclass, asdict, astuple
from pickle import dumps
@dataclass
class Simple:
i : int
s : str
l : list
s = Simple(10, 'hi', [3, 1, 4, 1])
@dataclass(frozen=True, slots=True)
class Frozen:
i : int
s : str
l : list
f = Frozen(10, 'hi', [3, 1, 4, 1])
f.__getstate__()
"""
runner = pyperf.Runner()
runner.timeit(name="instance creation", stmt="Simple(10, 'hi', [3, 1, 4, 1])", setup=setup)
runner.timeit(name="asdict", stmt="asdict(s)", setup=setup)
runner.timeit(name="astuple", stmt="astuple(s)", setup=setup)
runner.timeit(name="f.__getstate__()", stmt="f.__getstate__()", setup=setup)
Feature or enhancement
Proposal:
About 8 years ago @ericvsmith asked:
cpython/Lib/dataclasses.py
Line 1381 in c779f23
By caching the field names of a dataclass on the class the performance improves:
Test script
(executed on non-pgo build)
The main downside of caching the field names is that (per dataclass, not per instance) we have an additional private field on the class with a list of strings.
Has this already been discussed elsewhere?
No response given
Links to previous discussion of this feature:
No response
Linked PRs