summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAlex Gaynor <alex.gaynor@gmail.com>2014-09-22 22:30:55 +0000
committerAlex Gaynor <alex.gaynor@gmail.com>2014-09-22 22:30:55 +0000
commit866c1262f241641768c21f328048164fe56ca331 (patch)
tree999b502151c4043aed49346e55fa536abd4ba31f
parent093b886e2ed7a424aca018b3261a4e38ea4f4216 (diff)
downloadpython-coveragepy-866c1262f241641768c21f328048164fe56ca331.tar.gz
Improve performance of coverage under PyPy.
An explanation is in order: should_trace_cache is a strictly growing key, which is to say once a key is in it, it never changes. Further, the keys used to access it are generally constant, given sufficient context. That is to say, at any given point _trace() is called, a Sufficient Smart Compiler (tm) is able to know the key. This is because the key is determined by the physical source code line, and that's obviously invariant with the call site. This property of a dict with immutable keys, combined call-site-constant keys is a match for PyPy's module dict, which is optimized for such workloads. This gives a 20% benefit on the workload described at https://bitbucket.org/pypy/pypy/issue/1871/10x-slower-than-cpython-under-coverage I have not benchmarked on a wider array of programs yet.
-rw-r--r--coverage/collector.py10
1 files changed, 9 insertions, 1 deletions
diff --git a/coverage/collector.py b/coverage/collector.py
index 85c8dc9..c571cb0 100644
--- a/coverage/collector.py
+++ b/coverage/collector.py
@@ -23,6 +23,11 @@ except ImportError:
sys.exit(1)
CTracer = None
+try:
+ import __pypy__
+except ImportError:
+ __pypy__ = None
+
class Collector(object):
"""Collects trace data.
@@ -134,7 +139,10 @@ class Collector(object):
# A cache of the results from should_trace, the decision about whether
# to trace execution in a file. A dict of filename to (filename or
# None).
- self.should_trace_cache = {}
+ if __pypy__ is not None:
+ self.should_trace_cache = __pypy__.newdict("module")
+ else:
+ self.should_trace_cache = {}
# Our active Tracers.
self.tracers = []