Python: CG trace: More caching

Improves runtime of tracing youtube-dl from 296.19 seconds to 224.50 seconds.

Better, but still not that amazing :|
This commit is contained in:
Rasmus Wriedt Larsen
2020-07-23 18:07:55 +02:00
parent ce42221cf7
commit fbd939133e

View File

@@ -36,6 +36,9 @@ def canonic_filename(filename):
return canonic
_call_cache = dict()
@dataclasses.dataclass(frozen=True, eq=True, order=True)
class Call:
"""A call
@@ -55,17 +58,26 @@ class Call:
@classmethod
def from_frame(cls, frame: FrameType):
global _call_cache
key = cls.hash_key(frame)
if key in _call_cache:
return _call_cache[key]
code = frame.f_code
bytecode_expr = expr_from_frame(frame)
return cls(
call = cls(
filename=canonic_filename(code.co_filename),
linenum=frame.f_lineno,
inst_index=frame.f_lasti,
bytecode_expr=bytecode_expr,
)
_call_cache[key] = call
return call
@staticmethod
def hash_key(frame: FrameType) -> Tuple[str, int, int]:
code = frame.f_code