summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorjvoisin <julien.voisin@dustri.org>2020-01-02 15:18:26 +0100
committerNed Batchelder <ned@nedbatchelder.com>2020-01-04 16:24:05 -0500
commitaa6d8512e3184c1cc3b5868f8ab30aaa1c292d8c (patch)
tree20f42c332c1f0ea0094c8eb212aae4b1d71e450a
parentaefe08b437d1ce9c4e0bbdd7dba69e7316ede313 (diff)
downloadpython-coveragepy-git-aa6d8512e3184c1cc3b5868f8ab30aaa1c292d8c.tar.gz
Improve the performances of SqliteDb._connect
Since the self.filename attribute doesn't change during the lifetime of a SqliteDb object, we can move its relpath transformation in the init method, instead of doing it every time _connect is called, resulting in a ~30% performances gain.
-rw-r--r--coverage/sqldata.py17
1 files changed, 9 insertions, 8 deletions
diff --git a/coverage/sqldata.py b/coverage/sqldata.py
index 613a280c..2bb3035a 100644
--- a/coverage/sqldata.py
+++ b/coverage/sqldata.py
@@ -971,22 +971,23 @@ class SqliteDb(SimpleReprMixin):
self.filename = filename
self.nest = 0
self.con = None
-
- def _connect(self):
- """Connect to the db and do universal initialization."""
- if self.con is not None:
- return
# SQLite on Windows on py2 won't open a file if the filename argument
# has non-ascii characters in it. Opening a relative file name avoids
# a problem if the current directory has non-ascii.
try:
- filename = os.path.relpath(self.filename)
+ self.connect_filename = os.path.relpath(self.filename)
except ValueError:
# ValueError can be raised under Windows when os.getcwd() returns a
# folder from a different drive than the drive of self.filename in
# which case we keep the original value of self.filename unchanged,
# hoping that we won't face the non-ascii directory problem.
- filename = self.filename
+ self.connect_filename = self.filename
+
+ def _connect(self):
+ """Connect to the db and do universal initialization."""
+ if self.con is not None:
+ return
+
# 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 idempotent
@@ -994,7 +995,7 @@ class SqliteDb(SimpleReprMixin):
# is not a problem.
if self.debug:
self.debug.write("Connecting to {!r}".format(self.filename))
- self.con = sqlite3.connect(filename, check_same_thread=False)
+ self.con = sqlite3.connect(self.connect_filename, check_same_thread=False)
self.con.create_function('REGEXP', 2, _regexp)
# This pragma makes writing faster. It disables rollbacks, but we never need them.