8000 Using cached internal import for `BaseModel` by sydney-runkle · Pull Request #10013 · pydantic/pydantic · GitHub
[go: up one dir, main page]

Skip to content

Conversation

@sydney-runkle
Copy link
Contributor
@sydney-runkle sydney-runkle commented Jul 31, 2024

Similar to how we use a custom __getattr__ in __init__.py to lazily import attributes, and cache the results, I've added logic to manually cache the BaseModel import in our internal operations.

@github-actions github-actions bot added the relnotes-fix Used for bugfixes. label Jul 31, 2024
@cloudflare-workers-and-pages
Copy link
cloudflare-workers-and-pages bot commented Jul 31, 2024

Deploying pydantic-docs with  Cloudflare Pages  Cloudflare Pages

Latest commit: 0597cba
Status: ✅  Deploy successful!
Preview URL: https://84754b94.pydantic-docs.pages.dev
Branch Preview URL: https://import-caching.pydantic-docs.pages.dev

View logs

@codspeed-hq
Copy link
codspeed-hq bot commented Jul 31, 2024

CodSpeed Performance Report

Merging #10013 will not alter performance

Comparing import-caching (0597cba) with main (861f550)

Summary

✅ 17 untouched benchmarks

@sydney-runkle sydney-runkle added relnotes-performance Used for performance improvements. and removed relnotes-fix Used for bugfixes. labels Jul 31, 2024
@sydney-runkle
Copy link
Contributor Author
sydney-runkle commented Aug 1, 2024

Follow up thoughts:

  • Should we support relative imports?
  • How much should we do this for external packages (typing, re, etc)
  • What are consequences here for type checkers?

@github-actions
Copy link
Contributor
github-actions bot commented Aug 1, 2024

Coverage report

Click to see where and how coverage changed

FileStatementsMissingCoverageCoverage
(new stmts)
Lines missing
  pydantic
  main.py
  pydantic/_internal
  _fields.py
  _generate_schema.py
  _import_utils.py
  _known_annotated_metadata.py
  _model_construction.py
  _std_types_schema.py
  _utils.py
Project Total  

This report was generated by python-coverage-comment-action

@sydney-runkle sydney-runkle changed the title WIP: dynamic internal imports Using dynamic internal import for BaseModel Aug 1, 2024
@sydney-runkle sydney-runkle marked this pull request as ready for review August 1, 2024 15:21
@sydney-runkle sydney-runkle changed the title Using dynamic internal import for BaseModel Using cached internal import for BaseModel Aug 1, 2024
@sydney-runkle sydney-runkle merged commit 6d41460 into main Aug 1, 2024
@sydney-runkle sydney-runkle deleted the import-caching branch August 1, 2024 18:44
@sydney-runkle
Copy link
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!

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

Labels

relnotes-performance Used for performance improvements.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants

0