-
Notifications
You must be signed in to change notification settings - Fork 281
lazily imports more modules for performance #91
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
Conversation
@@ -1,6 +1,28 @@ | |||
import os | |||
import sys | |||
|
|||
|
|||
class CustomImporter(object): |
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.
Unless I'm missing something, all this does is alias cs50.sql
to cs50.SQL
. Flask indirectly import cs50.sql
anyway, so does this not help in lazy loading.
# Wrap SQLAlchemy | ||
from .sql import SQL | ||
# Lazily load CS50.SQL | ||
sys.meta_path.append(CustomImporter()) |
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.
At this point cs50.flask
has already imported cs50.sql
, so there's not much to gain
There is indeed a significant overhead. From testing it's It is |
Also as an aside, I think both |
Improves performance of using just
get_*
by lazily loading more ofSQL
andFlask
. E.g., on a recent MBP, reduced performance offrom ~500ms to ~300ms.
Cf. https://wiki.python.org/moin/PythonSpeed/PerformanceTips#Import_Statement_Overhead
We could alternatively break a deliberate abstraction barrier and hardcode awareness of / support for Flask inside of
SQL
, which might further delay expensive imports.