diff options
author | Ned Batchelder <ned@nedbatchelder.com> | 2019-07-07 07:40:24 -0400 |
---|---|---|
committer | Ned Batchelder <ned@nedbatchelder.com> | 2019-07-07 13:39:13 -0400 |
commit | dade9db58673d4f712ba5c50ed66d1c67a7e1d5d (patch) | |
tree | 1b88b3f99bd3348b566d01943439ccb72b4673ca /coverage/sqldata.py | |
parent | 4b43eff1377e818db2c42521d98bf2a0973b1cb6 (diff) | |
download | python-coveragepy-git-dade9db58673d4f712ba5c50ed66d1c67a7e1d5d.tar.gz |
Avoid useless or redundant db operations. Faster.
Moving operations into the "with self._connect" means less opening and
closing of the database. Returning early if there is no data to write
avoids writing empty contexts.
Diffstat (limited to 'coverage/sqldata.py')
-rw-r--r-- | coverage/sqldata.py | 20 |
1 files changed, 17 insertions, 3 deletions
diff --git a/coverage/sqldata.py b/coverage/sqldata.py index b8efd936..9d44db11 100644 --- a/coverage/sqldata.py +++ b/coverage/sqldata.py @@ -255,8 +255,10 @@ class CoverageSqliteData(SimpleReprMixin): )) self._start_using() self._choose_lines_or_arcs(lines=True) - self._set_context_id() + if not line_data: + return with self._connect() as con: + self._set_context_id() for filename, linenos in iitems(line_data): file_id = self._file_id(filename, add=True) data = [(file_id, self._current_context_id, lineno) for lineno in linenos] @@ -279,8 +281,10 @@ class CoverageSqliteData(SimpleReprMixin): )) self._start_using() self._choose_lines_or_arcs(arcs=True) - self._set_context_id() + if not arc_data: + return with self._connect() as con: + self._set_context_id() for filename, arcs in iitems(arc_data): file_id = self._file_id(filename, add=True) data = [(file_id, self._current_context_id, fromno, tono) for fromno, tono in arcs] @@ -307,6 +311,10 @@ class CoverageSqliteData(SimpleReprMixin): `file_tracers` is { filename: plugin_name, ... } """ + if self._debug.should('dataop'): + self._debug.write("Adding file tracers: %d files" % (len(file_tracers),)) + if not file_tracers: + return self._start_using() with self._connect() as con: for filename, plugin_name in iitems(file_tracers): @@ -667,6 +675,12 @@ class CoverageSqliteData(SimpleReprMixin): class SqliteDb(SimpleReprMixin): + """A simple abstraction over a SQLite database. + + Use as a context manager to get an object you can call + execute or executemany on. + + """ def __init__(self, filename, debug): self.debug = debug if debug.should('sql') else None self.filename = filename @@ -679,7 +693,7 @@ class SqliteDb(SimpleReprMixin): filename = os.path.relpath(self.filename) # It can happen that Python switches threads while the tracer writes # data. The second thread will also try to write to the data, - # effectively causing a nested context. However, given the indempotent + # effectively causing a nested context. However, given the idempotent # nature of the tracer operations, sharing a conenction among threads # is not a problem. if self.debug: |