diff --git a/mypy/main.py b/mypy/main.py index 14148720269a..dd782b91efbc 100644 --- a/mypy/main.py +++ b/mypy/main.py @@ -189,6 +189,12 @@ def main( list([res]) # noqa: C410 +class BuildResultThunk: + # We pass this around so that we avoid freeing memory, which is slow + def __init__(self, build_result: build.BuildResult | None) -> None: + self._result = build_result + + def run_build( sources: list[BuildSource], options: Options, @@ -196,7 +202,7 @@ def run_build( t0: float, stdout: TextIO, stderr: TextIO, -) -> tuple[build.BuildResult | None, list[str], bool]: +) -> tuple[BuildResultThunk | None, list[str], bool]: formatter = util.FancyFormatter( stdout, stderr, options.hide_error_codes, hide_success=bool(options.output) ) @@ -227,8 +233,12 @@ def flush_errors(filename: str | None, new_messages: list[str], serious: bool) - blockers = True if not e.use_stdout: serious = True + + if res: + res.manager.metastore.close() + maybe_write_junit_xml(time.time() - t0, serious, messages, messages_by_file, options) - return res, messages, blockers + return BuildResultThunk(res), messages, blockers def show_messages( diff --git a/mypy/metastore.py b/mypy/metastore.py index 64839bf8a79c..757825027895 100644 --- a/mypy/metastore.py +++ b/mypy/metastore.py @@ -163,6 +163,14 @@ def connect_db(db_file: str, sync_off: bool = False) -> sqlite3.Connection: # but without this flag, commits are *very* slow, especially when using HDDs, # see https://www.sqlite.org/faq.html#q19 for details. db.execute("PRAGMA synchronous=OFF") + + mode = db.execute("PRAGMA journal_mode").fetchone()[0] + if mode.lower() != "wal": + try: + db.execute("PRAGMA journal_mode=WAL") + except sqlite3.OperationalError: + pass + db.executescript(SCHEMA) return db @@ -172,8 +180,8 @@ def __init__(self, cache_dir_prefix: str, sync_off: bool = False) -> None: # We check startswith instead of equality because the version # will have already been appended by the time the cache dir is # passed here. + self.db = None if cache_dir_prefix.startswith(os.devnull): - self.db = None return os.makedirs(cache_dir_prefix, exist_ok=True)