ethereum / web3.py

A python interface for interacting with the Ethereum blockchain and ecosystem.
http://web3py.readthedocs.io
MIT License
5k stars 1.7k forks source link

Defer importing ens._normalization to speed up web3 import by 27% #3285

Closed szemate closed 7 months ago

szemate commented 7 months ago

What was wrong?

As of version 7.0.0-beta.2 the import web3 command takes 375 ms to run*, which is quite long for a library import operation. This affects the usability of command-line tools built around web3.py, where import times are critical.

How was it fixed?

By deferring the import of ens._normalization until the first call of the ens.utils.normalize_name function, the execution time of import web3 is reduced to 275 ms* i.e. by 27%.

Todo:


Details

Profiling* showed that the ens._normalization module performs two lengthy operations at import time:

These are very costly operations to perform at every single import, with the sole purpose of initializing the ens.utils.normalize_name function.

*: Measurements were taken with timeit and pyinstrument on an AMD Ryzen 7 machine with M.2 SSD using Python 3.10.12:

   >>> timeit.timeit("import web3")

and

   $ pyinstrument --show-regex '.*(eth|web3|ens).*' -c 'import web3'
kclowes commented 7 months ago

Just kidding, looks like I/maintainers can't add to your PR. Once you add a newsfragment I can merge. Thanks!

szemate commented 7 months ago

Newsfragment added, thank you!

kclowes commented 7 months ago

Thank you!