shows that importing Web3.py takes almost ~400 ms; if we don't import
Web3.py unless a command that requires it runs, the execution time of
of any aut --help command is greatly reduced.
Note that Click recommends 2 methods for lazy loading external
libraries: one is moving imports into command functions 2 as currently
used; the other one is creating a "Lazy Group" subclass of click.Group3 that loads commands on demand. The latter results in a neater
codebase, however dynamic imports with imporlib seem to add ~150 ms to
the execution time of any command, therefore it is not a good option.
Commit https://github.com/autonity/aut/commit/cc5e605e65c8c1a657075b913fa0b42d0b6f383a moved all imports of Autonity.py and Web3.py from top-level into functions in order to speed up loading time of the
aut
command for printing help messages.Profiling result with pyinstrument: 1
shows that importing Web3.py takes almost ~400 ms; if we don't import Web3.py unless a command that requires it runs, the execution time of of any
aut --help
command is greatly reduced.However later commits https://github.com/autonity/aut/commit/517efd086e21850e93abfdd30f6975038ddcb35c and https://github.com/autonity/aut/commit/b7cf048e03a684d7129396bd649898cba6b77151 re-added Autonity.py and Web3.py imports to the top-level for typing purposes. As a result Web3.py is again imported for any
aut
command, therefore there is no reduction in loading times.This commit removes these top-level imports.
Note that Click recommends 2 methods for lazy loading external libraries: one is moving imports into command functions 2 as currently used; the other one is creating a "Lazy Group" subclass of
click.Group
3 that loads commands on demand. The latter results in a neater codebase, however dynamic imports withimporlib
seem to add ~150 ms to the execution time of any command, therefore it is not a good option.