[Python-Dev] Impact of Namedtuple on startup time

Isaac Morland isaac.morland at gmail.com
Mon Jul 17 09:26:21 EDT 2017


On 17 July 2017 at 08:43, Antoine Pitrou <solipsis at pitrou.net> wrote:

>
> Hello,
>
> Cost of creating a namedtuple has been identified as a contributor to
> Python startup time.  Not only Python core and the stdlib, but any
> third-party library creating namedtuple classes (there are many of
> them).  An issue was created for this:
> https://bugs.python.org/issue28638
>
> Raymond decided to close the issue because:
>
> 1) the proposed resolution makes the "_source" attribute empty (or, at
> least, something else than it currently is).  Raymond claims the
> "_source" attribute is an essential feature of namedtuples.
>

I think I understand well enough to say something intelligent…

While actual references to _source are likely rare (certainly I’ve never
used it), my understanding is that the way namedtuple works is to construct
_source, and then exec it to create the class. Once that is done, there is
no significant saving to be had by throwing away the constructed _source
value.

When namedtuple was being considered for inclusion, I actually went so far
as to write a proof-of-concept version that worked by creating a class,
creating attributes on it, etc. I don’t remember how far I got but the exec
version is the version included in the stdlib. I come from a non-Pythonic
background so use of exec still feels a bit weird to me but I absolutely
love namedtuple and use it constantly. I don't know whether a polished and
completed version of my idea could be faster than using exec, but I
wouldn't expect a major saving — a whole bunch of code has to run either
way.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-dev/attachments/20170717/ea716618/attachment.html>


More information about the Python-Dev mailing list