You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
inspect.getframeinfo and traceback build every preceding co_positions() tuple #161799
co_positions() yields one tuple per instruction. islice has to build and discard a tuple for every preceding instruction before it can return the requested one. The returned position is correct. The cost grows with the size of the code object.
inspect.stack(0) does this for every frame. context=0 only skips the source-line listing. It still computes the PEP 657 position. A caller that uses inspect.stack(0) on a hot path therefore pays for a full walk of each frame's line table.
On 3.11 and on current main, a short function and a function with about 2500 statements show the difference:
The byte offset passed in is f_lasti or tb_lasti, the same value traceback.c already passes to PyCode_Addr2Location. One call of that function fills the same (start_line, end_line, start_column, end_column) tuple, with None where co_positions() yields None.
A direct lookup for real code objects removes the per-instruction tuple. Objects that only implement co_positions(), such as the stand-ins in test_traceback, can keep the iterator path.
On 3.11 and on current main, a short function and a function with about 2500 statements show the difference:
Please post the actual numbers. Determine whether they were done on a PGO+LTO/release build. Debug builds are not relevant when it comes down to performance evaluation.
Activity
picnixz commentedon Nov 11, 2026
I'm not convinced by this change. How "slow" is it? those are inspection tools and thus I don't think we should add private C methods just for that.
picnixz commentedon Nov 11, 2026
Please post the actual numbers. Determine whether they were done on a PGO+LTO/release build. Debug builds are not relevant when it comes down to performance evaluation.
xionghul commentedon Nov 12, 2026
small 0.00013875
large 0.00074843
3.11.6 (main, Oct 3 2023, 01:26:22) [Clang 17.0.1] with PGO+LTO/release build.