[Python-Dev] Python startup time

Christian Heimes christian at python.org
Thu Oct 10 12:18:14 CEST 2013


Am 10.10.2013 02:18, schrieb Eric Snow:
> On Wed, Oct 9, 2013 at 8:30 AM, Christian Heimes
> <christian at python.org> wrote:
>> The os module imports MutableMapping from collections.abc. That
>> import adds collections, collections.abc and eight more modules.
>> I'm not sure if we can do anything about it, though.
> 
> Well, that depends on how much we want to eliminate those 10
> imports. :)  Both environ and environb could be turned into lazy
> wrappers around an _Environ-created-when-needed.  If we used a
> custom module type for os [1], then adding descriptors for the two
> attributes is a piece of cake.  As it is, with a little metaclass
> magic (or even with explicit wrapping of the various dunder
> methods), we could drop those 10 imports from startup.

We don't have to use a custom module type to get rid of these imports
(but I like to get my hands a piece of chocolate cake *g*). We can
either implement yet another mutable mapping class for the os module.
That would remove the dependency on collections.abc.

Or we have to juggle the modules a bit so we can get to MutableMapping
without the extra stuff from collections.__init__. The abc and
_weakset modules are already loaded by the io module. Only
collections.__init__ imports _collections, operator, keyword, heapq,
itertools and reprlib.

I implemented both as an experiment. A lean and mean MutableMapping
works but it involves some code duplication. Next I moved
collections.abc to its former place _abcoll and installed a new
collections.abc module as facade.

$ hg mv Lib/collections/abc.py Lib/_abcoll.py
$ echo "from _abcoll import *" > Lib/collections/abc.py
$ echo "from _abcoll import __all__" >> Lib/collections/abc.py
$ sed -i "s/collections\.abc/_abcoll/" Lib/os.py


With three additional patches I'm down 19 modules:

$ ./python -c "import sys; print(len(sys.modules))"
34
$ hg revert --all .
$ ./python -c "import sys; print(len(sys.modules))"
53

Christian


More information about the Python-Dev mailing list