[Needs discussion] Improve startup time through lazy-loading #285
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.
shubis excruciatingly slow for a command line tool. On my machine,shub versiontakes almost two seconds to print the version string.I did some profiling: the time is mostly spent while importing
pipandrequests(and by extension while importingscrapinghub, because it importsrequests). Both of these modules are always imported, because they are top-level imported inshub.utils, and almost all command modules import utility functions. Additionally, they are even imported if the command module does not import utility functions (e.g.shub.version), because all command modules are imported inshub.tool.This PR does two things:
pip,requests, andscrapinghubin the utility functions. This reduces the import time forshub.utilsfrom ~900 ms to ~120 ms.requestsall over the place and it is impractical to lazy-import it in every module, changeshub.toolso that, if a command is given, it imports only that command's module, and not all command modules.This is the time required to run
shub versionon my machine:pipinshub.utils)*virtualenv has
shubandpyopensslinstalled. It's fair to assume that this is true for most real-world environmentsThere may be unforeseen side effects with the lazy imports, and the improvement from lazy-importing
requestsmay be somewhat of a sham because almost all commands need it anyways (with the exception of someshub imagecommands). What do you think @chekunkov @dangra?