From b922270ce7e6a7c0bd521f1eab3ea82b6041537d Mon Sep 17 00:00:00 2001 From: Pierre Sassoulas Date: Sat, 10 Feb 2024 15:51:33 +0100 Subject: [PATCH] [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 --- src/_pytest/python.py | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/src/_pytest/python.py b/src/_pytest/python.py index cf3415d98..91c48540d 100644 --- a/src/_pytest/python.py +++ b/src/_pytest/python.py @@ -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