-
Notifications
You must be signed in to change notification settings - Fork 243
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Introduce a schema variant to reuse Validators, Serializers and CoreSchema #1414
base: main
Are you sure you want to change the base?
Conversation
@adriangb, would love to get your feedback on this as well! |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Interesting concepts here. I have some reservations about application for things like schemas with forward refs, but this looks like a promising start.
A few questions:
- What type of benchmarking have you been able to do to in order to look at both time / memory comparisons against
main
? - This strikes me as pretty similar to our current pattern used here, at least conceptually. Why should we go about it in this way, instead? Perhaps the memory usage is better because we're not actually rebuilding the schema validator / serializer itself?
- I feel like integration with
pydantic
here would be a bit of a challenge, especially in the namespace realm. Have you thought about how we might handle refs to other models (schemas) in different modules, functions, etc? Definitely ok if not, just wanted to gauge progress here.
I think some helpful next steps would be:
- Get some feedback from @samuelcolvin, @davidhewitt, and @adriangb.
- Post folks familiarizing themselves with said changes, we could set aside some time to chat about this in our next oss sync 🚀.
3c5a8ad
to
0961627
Compare
0961627
to
b65d178
Compare
CodSpeed Performance ReportMerging #1414 will not alter performanceComparing Summary
|
40da0ee
to
a78828e
Compare
8cd91b5
to
8ddba74
Compare
8ddba74
to
f6508d2
Compare
Introduces a
nested
CoreSchema
variant that reuses the specified model's schema/validator/serializer. As an example, the core schema for the following pydantic models:Compare to the schema generated on main
This is similar to the current definitions-ref setup but it has a number of benefits:
CoreSchema
is smaller which means walking it can be significantly faster in cases with lots of nested models when we do not need to recurse into `nested-modeldefinitions
schemas where we walk the built schema and ensure that any nested models' schemas does not have adefinitions
schema. This is what I believe is a significant portion of the performance wins since we now have to do significantly less complex tree traversals over python datastructures in python.more times than once
See pydantic/pydantic#10246 for the implementation of generating this new schema variant and also the benchmak results.
Before Merging
serde_serialize
PyGcTraversible
implementation forResult
ignores the error variant asPyErr
is not traversiblenested
schema variant is defined flexibly enough to be useable for more than just models.There is an issue pydantic/pydantic#10394 tracking future work on the pydantic side to use this schema more