Skip to content

Commit aa6d851

Browse files
jvoisinnedbat
authored andcommitted
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.
1 parent aefe08b commit aa6d851

File tree

1 file changed

+9
-8
lines changed

1 file changed

+9
-8
lines changed

coverage/sqldata.py

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -971,30 +971,31 @@ def __init__(self, filename, debug):
971971
self.filename = filename
972972
self.nest = 0
973973
self.con = None
974-
975-
def _connect(self):
976-
"""Connect to the db and do universal initialization."""
977-
if self.con is not None:
978-
return
979974
# SQLite on Windows on py2 won't open a file if the filename argument
980975
# has non-ascii characters in it. Opening a relative file name avoids
981976
# a problem if the current directory has non-ascii.
982977
try:
983-
filename = os.path.relpath(self.filename)
978+
self.connect_filename = os.path.relpath(self.filename)
984979
except ValueError:
985980
# ValueError can be raised under Windows when os.getcwd() returns a
986981
# folder from a different drive than the drive of self.filename in
987982
# which case we keep the original value of self.filename unchanged,
988983
# hoping that we won't face the non-ascii directory problem.
989-
filename = self.filename
984+
self.connect_filename = self.filename
985+
986+
def _connect(self):
987+
"""Connect to the db and do universal initialization."""
988+
if self.con is not None:
989+
return
990+
990991
# It can happen that Python switches threads while the tracer writes
991992
# data. The second thread will also try to write to the data,
992993
# effectively causing a nested context. However, given the idempotent
993994
# nature of the tracer operations, sharing a connection among threads
994995
# is not a problem.
995996
if self.debug:
996997
self.debug.write("Connecting to {!r}".format(self.filename))
997-
self.con = sqlite3.connect(filename, check_same_thread=False)
998+
self.con = sqlite3.connect(self.connect_filename, check_same_thread=False)
998999
self.con.create_function('REGEXP', 2, _regexp)
9991000

10001001
# This pragma makes writing faster. It disables rollbacks, but we never need them.

0 commit comments

Comments
 (0)