Commit b69f2e5
committed
gh-144764: Made dataclasses construct automatic docstrings lazily
Move the default docstring construction to occur on-access through a descriptor.
Verified with [tprof](https://github.com/adamchainz/tprof) and this script that generates 10k dataclasses:
```py
from dataclasses import dataclass
for i in range(10_000):
@DataClass
class Example:
field1: int
field2: str
field3: float
```
**Before:**
```
$ tprof -t dataclasses._process_class example.py
🎯 tprof results:
function calls total mean ± σ min … max
dataclasses._process_class() 10000 5s 485μs ± 120μs 458μs … 6ms
```
After:
```
$ PYTHONPATH=Lib/ uvx tprof -t dataclasses._process_class example.py
🎯 tprof results:
function calls total mean ± σ min … max
dataclasses._process_class() 10000 3s 275μs ± 131μs 245μs … 6ms
```
The mean time spent in `_process_class()` has dropped from 485μs to 275μs, a ~42% time saving (admittedly skewed due to the small size of the dataclass).1 parent 66da7bf commit b69f2e5
1 file changed
+18
-15
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1209 | 1209 | | |
1210 | 1210 | | |
1211 | 1211 | | |
1212 | | - | |
1213 | | - | |
1214 | | - | |
| 1212 | + | |
1215 | 1213 | | |
1216 | 1214 | | |
1217 | | - | |
1218 | | - | |
1219 | | - | |
1220 | | - | |
1221 | | - | |
1222 | | - | |
1223 | | - | |
1224 | | - | |
1225 | | - | |
1226 | | - | |
1227 | | - | |
1228 | | - | |
| 1215 | + | |
| 1216 | + | |
1229 | 1217 | | |
1230 | 1218 | | |
1231 | 1219 | | |
| |||
1243 | 1231 | | |
1244 | 1232 | | |
1245 | 1233 | | |
| 1234 | + | |
| 1235 | + | |
| 1236 | + | |
| 1237 | + | |
| 1238 | + | |
| 1239 | + | |
| 1240 | + | |
| 1241 | + | |
| 1242 | + | |
| 1243 | + | |
| 1244 | + | |
| 1245 | + | |
| 1246 | + | |
| 1247 | + | |
| 1248 | + | |
1246 | 1249 | | |
1247 | 1250 | | |
1248 | 1251 | | |
| |||
0 commit comments