[performance] Micro-optimization in '_traceback_filter'

Should be around 40% faster according to this simplified small benchmark:

python -m timeit "a=[0, 1, 2, 3, 4];b=list((e if i in {0, len(a) -1} else str(e)) for i, e in enumerate(a))"
200000 loops, best of 5: 1.12 usec per loop
python -m timeit "a=[0, 1, 2, 3, 4];b=list((a[0], *(str(e) for e in a[1:-1]), a[-1]))"
500000 loops, best of 5: 651 nsec per loop

python -m timeit "a=[0, 1, 2, 3, 4,5,6,7,8,9,10,11,12,13,14,15,16];b=list((e if i in {0, len(a) -1} else str(e)) for i, e in enumerate(a))"
100000 loops, best of 5: 3.31 usec per loop
python -m timeit "a=[0, 1, 2, 3, 4,5,6,7,8,9,10,11,12,13,14,15,16];b=list((a[0], *(str(e) for e in a[1:-1]), a[-1]))"
200000 loops, best of 5: 1.72 usec per loop
This commit is contained in:
Pierre Sassoulas 2024-02-10 15:51:33 +01:00
parent 1180348303
commit b922270ce7
1 changed files with 3 additions and 4 deletions

View File

@ -1786,11 +1786,10 @@ class Function(PyobjMixin, nodes.Item):
if len(ntraceback) > 2:
ntraceback = Traceback(
(
entry
if i in {0, len(ntraceback) - 1}
else entry.with_repr_style("short")
ntraceback[0],
*(t.with_repr_style("short") for t in ntraceback[1:-1]),
ntraceback[-1],
)
for i, entry in enumerate(ntraceback)
)
return ntraceback