Skip to content

Commit

Permalink
perf: set frame->f_trace_lines=0 when not tracing. ~3% faster
Browse files Browse the repository at this point in the history
  • Loading branch information
nedbat committed May 28, 2022
1 parent 6503308 commit 4b592fe
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 30 deletions.
1 change: 1 addition & 0 deletions coverage/ctracer/tracer.c
Original file line number Diff line number Diff line change
Expand Up @@ -514,6 +514,7 @@ CTracer_handle_call(CTracer *self, PyFrameObject *frame)
Py_XDECREF(self->pcur_entry->file_data);
self->pcur_entry->file_data = NULL;
self->pcur_entry->file_tracer = Py_None;
frame->f_trace_lines = 0;
SHOWLOG(PyFrame_GetLineNumber(frame), filename, "skipped");
}

Expand Down
4 changes: 4 additions & 0 deletions coverage/pytracer.py
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,7 @@ def _trace(self, frame, event, arg_unused):
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
Expand All @@ -176,6 +177,7 @@ def _trace(self, frame, event, arg_unused):
self.last_line = -frame.f_code.co_firstlineno
else:
self.last_line = frame.f_lineno

elif event == 'line':
# Record an executed line.
if self.cur_file_data is not None:
Expand All @@ -186,6 +188,7 @@ def _trace(self, frame, event, arg_unused):
else:
self.cur_file_data.add(lineno)
self.last_line = lineno

elif event == 'return':
if self.trace_arcs and self.cur_file_data:
# Record an arc leaving the function, but beware that a
Expand Down Expand Up @@ -213,6 +216,7 @@ def _trace(self, frame, event, arg_unused):
if real_return:
first = frame.f_code.co_firstlineno
self.cur_file_data.add((self.last_line, -first))

# Leaving this function, pop the filename stack.
self.cur_file_data, self.cur_file_name, self.last_line, self.started_context = (
self.data_stack.pop()
Expand Down
51 changes: 21 additions & 30 deletions lab/benchmark.py
Original file line number Diff line number Diff line change
Expand Up @@ -257,44 +257,35 @@ def run_experiments(

run_experiments(
py_versions=[
Python(3, 7),
Python(3, 10),
],
cov_versions=[
("none", None, None),
("6.4", "coverage==6.4", ""),
("6.4 timid", "coverage==6.4", "timid=True"),
(
"PR 1381",
"git+https://github.com/cfbolz/coveragepy.git@f_trace_lines",
"",
),
(
"PR 1381 timid",
"git+https://github.com/cfbolz/coveragepy.git@f_trace_lines",
"timid=True",
),
("tip", "-e ~/coverage/trunk", ""),
],
projects=[
PytestHtml(),
],
num_runs=3,
num_runs=5,
)

run_experiments(
py_versions=[
PyPy(3, 9),
],
cov_versions=[
("none", None, None),
("6.4", "coverage==6.4", ""),
(
"PR 1381",
"git+https://github.com/cfbolz/coveragepy.git@f_trace_lines",
"",
),
],
projects=[
PytestHtml(),
],
num_runs=3,
)
# run_experiments(
# py_versions=[
# PyPy(3, 9),
# ],
# cov_versions=[
# ("none", None, None),
# ("6.4", "coverage==6.4", ""),
# (
# "PR 1381",
# "git+https://github.com/cfbolz/coveragepy.git@f_trace_lines",
# "",
# ),
# ],
# projects=[
# PytestHtml(),
# ],
# num_runs=3,
# )

0 comments on commit 4b592fe

Please sign in to comment.