8000 Pytracer improvements by cfbolz · Pull Request #1388 · nedbat/coveragepy · GitHub
[go: up one dir, main page]

Skip to content

Pytracer improvements #1388

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
May 30, 2022
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
cache the bound method of _trace on self
this speeds up pure python tracing because we don't have to re-create a
bound method object all the time
  • Loading branch information
cfbolz committed May 30, 2022
commit e4bd4dd2a8e404846299664ee7fd3a2d4fe2a32f
19 changes: 12 additions & 7 deletions coverage/pytracer.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,11 @@ def __init__(self):
# On exit, self.in_atexit = True
atexit.register(setattr, self, 'in_atexit', True)

# cache a bound method on the instance, so that we don't have to
# re-create a bound method object all the time
self._cached_bound_method_trace = self._trace


def __repr__(self):
return "<PyTracer at 0x{:x}: {} lines in {} files>".format(
id(self),
Expand Down Expand Up @@ -105,7 +110,7 @@ def _trace(self, frame, event, arg_unused):

#self.log(":", frame.f_code.co_filename, frame.f_lineno, frame.f_code.co_name + "()", event)

if (self.stopped and sys.gettrace() == self._trace): # pylint: disable=comparison-with-callable
if (self.stopped and sys.gettrace() == self._cached_bound_method_trace): # pylint: disable=comparison-with-callable
# The PyTrace.stop() method has been called, possibly by another
# thread, let's deactivate ourselves now.
if 0:
Expand Down Expand Up @@ -225,7 +230,7 @@ def _trace(self, frame, event, arg_unused):
if self.started_context:
self.context = None
self.switch_context(None)
return self._trace
return self._cached_bound_method_trace

def start(self):
"""Start this Tracer.
Expand All @@ -243,10 +248,10 @@ def start(self):
# function, but we are marked as running again, so maybe it
# will be ok?
#self.log("~", "starting on different threads")
return self._trace
return self._cached_bound_method_trace

sys.settrace(self._trace)
return self._trace
sys.settrace(self._cached_bound_method_trace)
return self._cached_bound_method_trace

def stop(self):
"""Stop this Tracer."""
Expand All @@ -271,9 +276,9 @@ def stop(self):
# so don't warn if we are in atexit on PyPy and the trace function
# has changed to None.
dont_warn = (env.PYPY and env.PYPYVERSION >= (5, 4) and self.in_atexit and tf is None)
if (not dont_warn) and tf != self._trace: # pylint: disable=comparison-with-callable
if (not dont_warn) and tf != self._cached_bound_method_trace: # pylint: disable=comparison-with-callable
self.warn(
f"Trace function changed, data is likely wrong: {tf!r} != {self._trace!r}",
f"Trace function changed, data is likely wrong: {tf!r} != {self._cached_bound_method_trace!r}",
slug="trace-changed",
)

Expand Down
0