Skip instance __dataclass_fields__ lookup for non-dataclass types - #1196
Conversation
|
I'm not sure where we stand on this. While dataclasses do not use IMO if a type is recognised by @sobolevn any opinions on this? |
sobolevn
left a comment
There was a problem hiding this comment.
Pros:
- additional perf
Cons:
- potential breaking change (which has very low probability in the real world)
Can we get the perf numbers please? So, we can fully decide on this.
|
As for the results on my
Every object that reaches The script used below uv run bench_msgspec_1196.py |
|
I'm all for the change, given its impressive perf results :) |
|
12% slower encode of dataclasses seems a bit not so great. If we could mitigate that, I'd be for this. Otherwise
As long as we do have an escape hatch, and document both it and the edge case, that would make it less severe |
Its on a list of dataclass, its 2% on a single dataclass |
|
Revamped it, so now for plain dataclasses it's using the fields dict we already found on the class instead of looking it up again on the instance, it removes the regression and makes ot a bit faster than main; unusual classes (custom The same bench file attached before was used (no modification). @provinzkraut @sobolevn
If this is accepted, I can add documentation about it + changelog |
|
This approach looks promising. Not having the performance hit on dataclasses is nice :) |
sobolevn
left a comment
There was a problem hiding this comment.
Looks like a good optimization idea and implementation! 👍
Entries for msgspec#1194, msgspec#1196, msgspec#1197, msgspec#1199 and msgspec#1209 are missing from the changelog of the 0.22.0 release. Of the changes merged after msgspec#1211 moved the unreleased entries into the 0.22.0 section, only msgspec#1207 added its own entry. This adds the missing entries next to the related entries in that section, and marks msgspec#1196 as a breaking change: objects that expose `__dataclass_fields__` only through instance attribute access are no longer encoded as dataclasses. Co-authored-by: Tseluiko Aleksandr <4410812+Siyet@users.noreply.github.com>
Skip the instance
__dataclass_fields__lookup when the type doesn't define it, so objects with a Python level__getattr__(like pydantic models) reachenc_hookwithout running Python code.