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%.
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 theens.utils.normalize_name
function, the execution time ofimport 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:ens/spec/normalization_spec.json
that is 3 Mbytes in size takes 7% of the total web3 import time_extract_valid_codepoints()
function that performs transformations on the loaded data takes 16% of the total web3 import timeThese 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
andpyinstrument
on an AMD Ryzen 7 machine with M.2 SSD using Python 3.10.12:and