-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
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
Reduce import time #4649
Reduce import time #4649
Conversation
utils.console is responsible for almost half of the astropy import time (it imports IPython and astropy.units). It is used in the logger module to colorize the logging message, so here this import is moved to a local import.
As units import takes some time and is used only for one specific case in `human_file_size`, it can be moved to a local import to avoid importing units when it is not useful.
Yep, I confirm the roughly ~2x improvement in import time. Nice find, @saimn! Two suggestions:
There's a bit of a question what version this goes in. I tend toward 1.2 because it's not specifically a bug/docs fix. But I'm open to other opinions. |
I was going to ask @MSeifert04 to give this a try when the tests pass |
It's almost 3x faster doing |
@eteq - Done, 1.2 seems fine. @MSeifert04 - Yes, it will have only a minor effect for subpackages. For |
Alright, merged, thanks @saimn. @MSeifert04 @saimn - yes, a lot of the long import effects come from the underlying situation that importing everything is always slower than importing some things. I think that there's a balance to be considered here - there's often a clarity/performance tradeoff when it comes to imports: it may be clearer to put them all at the top at the cost of a bit of performance. This particular one is no-brainer because |
@eteq - I agree, this kind of optimization should be reserved for specific cases, e.g. when a module is imported once for an optional feature. It seems to be the case currently for |
Labelled as |
Ref #4598
utils.console
in the logger setup. The import will be done when the first logging message is printed.utils.console
to a local one. With the previous change it is useless, but I think it may still be useful on some cases or for command-line scripts (when Spinner or ProgressBar are used).With these changes, the import time goes down from ~480ms to ~250ms on my laptop. The remaining time is mostly in developer specific stuff (subprocess call to get the git hash for the version string), so the import time will be even better for released package.