autonity / aut

A command-line RPC client for Autonity
MIT License
11 stars 10 forks source link

Remove top-level Web3 imports to speed up loading time #147

Closed szemate closed 1 week ago

szemate commented 5 months ago

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

0.411 <module>  aut/__main__.py:1
├─ 0.393 <module>  aut/commands/account.py:1
│  └─ 0.392 <module>  web3/__init__.py:1
│        [99 frames hidden]  web3, ens, pyunormalize, <built-in>, ...
└─ 0.006 <module>  click/__init__.py:1
      [2 frames hidden]  click

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 with imporlib seem to add ~150 ms to the execution time of any command, therefore it is not a good option.