Skip to content
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

Performance tips docs page #7323

Closed
Tracked by #7324 ...
samuelcolvin opened this issue Sep 4, 2023 · 2 comments · Fixed by #7567
Closed
Tracked by #7324 ...

Performance tips docs page #7323

samuelcolvin opened this issue Sep 4, 2023 · 2 comments · Fixed by #7567
Assignees
Milestone

Comments

@samuelcolvin
Copy link
Member

samuelcolvin commented Sep 4, 2023

Page in the docs with tips on using pydantic in the most performant way.

Should start with a big notice saying something like "In most cases pydantic won't be your bottle neck, only follow this if you're sure it's necessary blah blah blah".

  1. Use model_validate_json() not model_validate(json.loads(...)) (👀 FastAPI)
  2. TypeAdapter instantiated once - generally avoid constructing validators and serializers more than necessary
  3. Sequence vs list/tuple - Mapping vs dict
  4. Don't do validation when you don't have to - use Any to keep the value unchanged
  5. generally avoid recording extra information via subclasses of primatives
  6. Use tagged union, not union
  7. Use Literal not Enum (not sure how big this effect is)
  8. use TypedDict over nested models
  9. Avoid wrap validators if you really care about performance

Anything else?

Selected Assignee: @Kludex

@pydantic-hooky pydantic-hooky bot added the unconfirmed Bug not yet confirmed as valid/applicable label Sep 4, 2023
@samuelcolvin samuelcolvin removed the unconfirmed Bug not yet confirmed as valid/applicable label Sep 4, 2023
@Burnsedia
Copy link

Another thing you can do I do the simple calculations first, only to the complicated once when you have to. another is object pooling arrays and time slicing.

@sydney-runkle sydney-runkle added this to the v2.5.0 milestone Nov 10, 2023
@sydney-runkle
Copy link
Member

Looks like this should have been closed by @Kludex's #7567 PR. Closing now, as the minimum reqs for this page have been met. I think we should open a new issue with new requests for this page if/when they're desired.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants