-
-
Notifications
You must be signed in to change notification settings - Fork 2.4k
Using cached internal import for BaseModel
#10013
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
Merged
Merged
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Deploying pydantic-docs with
|
| Latest commit: |
0597cba
|
| Status: | ✅ Deploy successful! |
| Preview URL: | https://84754b94.pydantic-docs.pages.dev |
| Branch Preview URL: | https://import-caching.pydantic-docs.pages.dev |
CodSpeed Performance ReportMerging #10013 will not alter performanceComparing Summary
|
Contributor
Author
|
Follow up thoughts:
|
Contributor
Coverage reportClick to see where and how coverage changed
This report was generated by python-coverage-comment-action |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
BaseModel
BaseModelBaseModel
sydney-runkle
commented
Aug 1, 2024
dmontagu
approved these changes
Aug 1, 2024
Contributor
Author
|
Note - we were inclined to make this change due to the promise of manually caching imports, as shown by: In [1]: from functools import cache
...:
...: def simple_import():
...: from pydantic import BaseModel
...: return BaseModel
...:
...:
...: @cache
...: def fast_import_dc():
...: from pydantic import BaseModel
...: return BaseModel
...:
...:
...: def use_cached():
...: return fast_import_dc()
...:
In [2]: %timeit simple_import()
4.11 µs ± 2.22 µs per loop (mean ± std. dev. of 7 runs, 1 loop each)
In [3]: %timeit use_cached()
25.1 ns ± 0.445 ns per loop (mean ± std. dev. of 7 runs, 10,000,000 loops each)This resulted in a 5-10% speedup for various schema building cases! |
This was referenced Aug 2, 2024
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Similar to how we use a custom
__getattr__in__init__.pyto lazily import attributes, and cache the results, I've added logic to manually cache theBaseModelimport in our internal operations.