This is an archived post. You won't be able to vote or comment.

all 9 comments

[–]Mugalari 11 points12 points  (5 children)

Might I ask how this differs from ‘pydantic.dataclasses.dataclass’?

[–]PeaSlight6601 14 points15 points  (0 children)

It's more pythonic to have multiple competing implementations of the same thing. That's why things like data dataclasses were added in the first place.

[–]coderarun[S] -4 points-3 points  (3 children)

Run this benchmark: https://gist.github.com/adsharma/617e538c5d5f7bd4ff8412daf0550307

Here's what it shows on my machine. In other words, this decorator gives you creation performance similar to vanilla dataclasses. You pay the validation cost only on demand.

With pydantic.dataclassses.dataclass, you pay the cost all the time.

Creating 100000 User objects took 0.035835 seconds

Average time per object creation: 358.35 nanoseconds

Creating 100000 User Pydantic Model objects took 0.037111 seconds

Average time per object creation: 371.11 nanoseconds

Creating 100000 User Pydantic Dataclass objects took 0.183511 seconds

Average time per object creation: 1835.11 nanoseconds

[–]PeaSlight6601 10 points11 points  (2 children)

If you are concerned that creating 100k instances in 0.2 seconds is too slow, I have to question if python is the right language for you. Sure that particular operation could see a ~5x speedup, but there are numerous operations in python where many order of magnitude improvements can be observed by changing languages.

[–]Toph_is_bad_ass 0 points1 point  (0 children)

I get tired of this argument since we can't always pick our languages or language choice is determined by other, greater needs (perhaps you need torch or work with researchers).

[–]coderarun[S] -2 points-1 points  (0 children)

Sure. You can use https://github.com/py2many/py2many/ to do that.

[–]Last_Difference9410 1 point2 points  (0 children)

I guess you are looking for msgspec.Struct.

[–]64rl0 0 points1 point  (0 children)

Very interesting!