From 2f39124ffb0fc7ad5c4a4b237040a8b6f33f6161 Mon Sep 17 00:00:00 2001 From: Carl Friedrich Bolz-Tereick Date: Tue, 24 May 2022 22:08:04 +0200 Subject: [PATCH] speed up python tracer with frame.f_trace_lines = False use the python >= 3.7 feature of being able to disable line tracing by setting the frame attribute f_trace_lines to False. This can be used for the frames of functions that we aren't collecting coverage information for (eg those functions in the stdlib). This speeds up the pure python tracer in CPython by ~9x and in PyPy by 80% on a coverage run of one realistic project that I tried. --- coverage/pytracer.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/coverage/pytracer.py b/coverage/pytracer.py index f0319491d..1f0f79249 100644 --- a/coverage/pytracer.py +++ b/coverage/pytracer.py @@ -159,6 +159,8 @@ def _trace(self, frame, event, arg_unused): if tracename not in self.data: self.data[tracename] = set() self.cur_file_data = self.data[tracename] + else: + frame.f_trace_lines = False # The call event is really a "start frame" event, and happens for # function calls and re-entering generators. The f_lasti field is # -1 for calls, and a real offset for generators. Use <0 as the