From 4cc32922685c6971275f522304b3754ad1a233c1 Mon Sep 17 00:00:00 2001 From: Ned Batchelder Date: Mon, 23 Jan 2023 19:44:41 -0500 Subject: perf: avoid needless sql operations. #1538 If the set of arcs is empty, skip the SQL operations. We also need to allow setting a file tracer for an unmeasured file, to avoid the Cython problem whose fix caused the performance issue in the first place. TBH, I don't know why we had to prevent file tracers on unmeasured files. Perhaps pytest-cov has changed to avoid the behavior that caused problems. --- coverage/sqldata.py | 16 +++++++--------- 1 file changed, 7 insertions(+), 9 deletions(-) (limited to 'coverage/sqldata.py') diff --git a/coverage/sqldata.py b/coverage/sqldata.py index da66ad09..1cb8abe4 100644 --- a/coverage/sqldata.py +++ b/coverage/sqldata.py @@ -528,6 +528,8 @@ class CoverageData(AutoReprMixin): with self._connect() as con: self._set_context_id() for filename, arcs in arc_data.items(): + if not arcs: + continue file_id = self._file_id(filename, add=True) data = [(file_id, self._current_context_id, fromno, tono) for fromno, tono in arcs] con.executemany_void( @@ -571,12 +573,7 @@ class CoverageData(AutoReprMixin): self._start_using() with self._connect() as con: for filename, plugin_name in file_tracers.items(): - file_id = self._file_id(filename) - if file_id is None: - raise DataError( - f"Can't add file tracer data for unmeasured file '{filename}'" - ) - + file_id = self._file_id(filename, add=True) existing_plugin = self.file_tracer(filename) if existing_plugin: if existing_plugin != plugin_name: @@ -1213,10 +1210,9 @@ class SqliteDb(AutoReprMixin): else: raise AssertionError(f"SQL {sql!r} shouldn't return {len(rows)} rows") - def _executemany(self, sql: str, data: Iterable[Any]) -> sqlite3.Cursor: + def _executemany(self, sql: str, data: List[Any]) -> sqlite3.Cursor: """Same as :meth:`python:sqlite3.Connection.executemany`.""" if self.debug.should("sql"): - data = list(data) final = ":" if self.debug.should("sqldata") else "" self.debug.write(f"Executing many {sql!r} with {len(data)} rows{final}") if self.debug.should("sqldata"): @@ -1233,7 +1229,9 @@ class SqliteDb(AutoReprMixin): def executemany_void(self, sql: str, data: Iterable[Any]) -> None: """Same as :meth:`python:sqlite3.Connection.executemany` when you don't need the cursor.""" - self._executemany(sql, data).close() + data = list(data) + if data: + self._executemany(sql, data).close() def executescript(self, script: str) -> None: """Same as :meth:`python:sqlite3.Connection.executescript`.""" -- cgit v1.2.1