Use lazy imports on Python 3.15 to improve startup speed - #645
Conversation
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## master #645 +/- ##
==========================================
+ Coverage 93.16% 93.19% +0.03%
==========================================
Files 29 29
Lines 3231 3247 +16
==========================================
+ Hits 3010 3026 +16
Misses 221 221 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
|
Interesting, really! I wonder however what's the cost of "hard" importing the Python's own modules, like io, decimal, operator. Aren't essentially external modules that are costly to import? |
|
The light blue boxes in the top chart are stdlib modules, and we can see many of them are contributing real cost to the import tree. As linked in #608, we've been deferring imports inside the stdlib of other stdlib modules to help reduce this cost, and in 3.15+ using the The stdlib has some benefit: they're usually byte-compiled to pyc at install time, but that's a cheap one-time cost for external modules, and doesn't affect costly execution time. Some modules are always imported as part of Looking the top chart again, most of the lower edge underneath tablib is light blue, so most of the import time comes from importing stdlib. |
claudep
left a comment
There was a problem hiding this comment.
Thanks for the detailed explanations 😍
Replaces and closes #608.
The upcoming Python 3.15 (now in beta) introduces a new
lazykeyword for imports:With this, the import doesn't actually happen until the first use:
So there's no need for the deferred method used in #608.
This is new syntax in 3.15, if you use it on an older version you get a syntax error. But there's a transitional mechanism: if you declare the imports in
__lazy_modules__, they'll be treated as if they're lazy in 3.15+, and ignored for older versions.Running with Python 3.15:
Before: 8 ms
After: ~0 ms
Even better than #608!