tracer: use sys._getframe directly.

This speeds up elaboration by ~30-40%.
This commit is contained in:
whitequark 2019-08-08 10:23:35 +00:00
parent e6b1e3de1a
commit 4ee82c9584

View file

@ -1,5 +1,4 @@
import traceback import sys
import inspect
from opcode import opname from opcode import opname
@ -14,10 +13,7 @@ _raise_exception = object()
def get_var_name(depth=2, default=_raise_exception): def get_var_name(depth=2, default=_raise_exception):
frame = inspect.currentframe() frame = sys._getframe(depth)
for _ in range(depth):
frame = frame.f_back
code = frame.f_code code = frame.f_code
call_index = frame.f_lasti call_index = frame.f_lasti
while True: while True:
@ -55,7 +51,5 @@ def get_src_loc(src_loc_at=0):
# n-th frame: get_src_loc() # n-th frame: get_src_loc()
# n-1th frame: caller of get_src_loc() (usually constructor) # n-1th frame: caller of get_src_loc() (usually constructor)
# n-2th frame: caller of caller (usually user code) # n-2th frame: caller of caller (usually user code)
# Python returns the stack frames reversed, so it is enough to set limit and grab the very frame = sys._getframe(2 + src_loc_at)
# first one in the array. return (frame.f_code.co_filename, frame.f_lineno)
tb = traceback.extract_stack(limit=3 + src_loc_at)
return (tb[0].filename, tb[0].lineno)