[issue36551] Optimize list comprehensions with preallocate size and protect against overflow

anthony shaw report at bugs.python.org
Mon Apr 8 07:35:08 EDT 2019


anthony shaw <anthony.p.shaw at gmail.com> added the comment:

> How about converting `[x for x in it]` to `[*it]` in AST?

I should have been more explicit, this patch improves the performance of all list comprehensions that don’t have an if clause.

Not just
[x for x in y]

but:

d = {} # some sort of dictionary
[f”{k} — {v}” for k, v in d.items()]

a = iterable
[val**2 for val in a]

Would all use BUILD_LIST_PREALLOC and use a LengthHint.

I can do another speed test for those other scenarios. 

Most of the stdlib packages have these sorts of list comps, including those in the default site.py.

----------
nosy:  -pablogsal

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue36551>
_______________________________________


More information about the Python-bugs-list mailing list