[Python-Dev] Startup time (original) (raw)

Jeff Epler jepler@unpythonic.net
Wed, 7 May 2003 13:52:46 -0500


On Wed, May 07, 2003 at 01:30:26PM -0500, Jeff Epler wrote:

.. and the input of sre is back. I guess it's used in both warnings.py and encodings/init.py

In encodings.init.py, the only use of re is for the normalize_encoding function. It could potentially be replaced with only string operations: # translate all offending characters to whitespace _norm_encoding_trans = string.maketrans(...)

def normalize_encoding(encoding):
    encoding = encoding.translate(_norm_encoding_trans)
    # let the str.split machinery take care of splitting
    # only once on repeated whitespace
    return "_".join(encoding.split())

.. or the import of re could be moved inside normalize_encoding.

In warnings.py, re is used in two functions, filterwarnings() and _setoption(). it's probably safe to move 'import re' inside these functions. I'm guessing the 'import lock' warnings.py problem doesn't apply when parsing options or adding new warning filters.

Furthermore, filterwarnings() will have to be changed to not use re.compile() when message is "" (the resulting RE is always successfully matched) since several filterwarnings() calls are already performed by default, but always with message="".

These changes would prevent the import of 're' at startup time, which appears to be the real killer. (see my module import timings in an earlier message)