feat(perf): Improve load time of help text #1444
Merged
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.
Issue #, if available:
N/A
Description of changes:
sam --help
is a very slow operation. Testing on my machine took about ~1.25s. Comparing this thesam build --help
, help text was displayed in ~.7s. The main reason for this is due to how python loads modules and click. In order for click to know the help text of each command, click callslist_commands
and thenget_command
for each command in the list. Ourget_command
implementation will then import the command module and return thecli
method. This means asam --help
will end up loading the entiresamcli
module (or very close to it), whilesam build --help
will only load the 'build' related modules.This PR moves, most, of the command module loading from the global package/module scope (file), to specific methods. This reduces both
sam --help
andsam build --help
to .63s and .6s respectfully, a roughly 50% reduction onsam --help
.There could be better ways to lazy load these modules but not currently aware. Since this only moves the imports of some modules to the call of
cli
method, execution of the commands won't be impacted. This is due to the same loading that was being done in the global scope is now just done a function execution instead. This does not change how we load any of the 'library' code within the cli, only the parts of the code base that are the entry point for a given command (ones annotated with parameters/options/etc).Checklist:
make pr
passesBy submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.