Skip to content

Commit e0451ce

Browse files
committed
Use WAL with SQLite cache, fix close
This is the more modern way to manage concurrency with SQLite In our case, it means concurrent mypy runs using the cache will wait for each other, rather than fail SQLite also claims this is faster, but I haven't yet done a good profile (If you are profiling this, note that WAL is a persistent setting, so you will want to delete the cache) Finally, I also explicitly close the connection in main. This is relevant to this change, because it forces checkpointing of the WAL, which reduces disk space and means the cache.db remains a single self-contained file in regular use
1 parent ef7e8a6 commit e0451ce

File tree

2 files changed

+13
-2
lines changed

2 files changed

+13
-2
lines changed

mypy/main.py

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -189,14 +189,20 @@ def main(
189189
list([res]) # noqa: C410
190190

191191

192+
class BuildResultThunk:
193+
# We pass this around so that we avoid freeing memory, which is slow
194+
def __init__(self, build_result: build.BuildResult | None) -> None:
195+
self._result = build_result
196+
197+
192198
def run_build(
193199
sources: list[BuildSource],
194200
options: Options,
195201
fscache: FileSystemCache,
196202
t0: float,
197203
stdout: TextIO,
198204
stderr: TextIO,
199-
) -> tuple[build.BuildResult | None, list[str], bool]:
205+
) -> tuple[BuildResultThunk | None, list[str], bool]:
200206
formatter = util.FancyFormatter(
201207
stdout, stderr, options.hide_error_codes, hide_success=bool(options.output)
202208
)
@@ -227,8 +233,12 @@ def flush_errors(filename: str | None, new_messages: list[str], serious: bool) -
227233
blockers = True
228234
if not e.use_stdout:
229235
serious = True
236+
237+
if res:
238+
res.manager.metastore.close()
239+
230240
maybe_write_junit_xml(time.time() - t0, serious, messages, messages_by_file, options)
231-
return res, messages, blockers
241+
return BuildResultThunk(res), messages, blockers
232242

233243

234244
def show_messages(

mypy/metastore.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,7 @@ def connect_db(db_file: str, sync_off: bool = False) -> sqlite3.Connection:
163163
# but without this flag, commits are *very* slow, especially when using HDDs,
164164
# see https://www.sqlite.org/faq.html#q19 for details.
165165
db.execute("PRAGMA synchronous=OFF")
166+
db.execute("PRAGMA journal_mode=WAL")
166167
db.executescript(SCHEMA)
167168
return db
168169

0 commit comments

Comments
 (0)