From 4d0c80496bde8e32d39933d22e8afb7d585467c3 Mon Sep 17 00:00:00 2001 From: AndreyPolovodov Date: Sat, 28 Mar 2026 16:18:55 +0300 Subject: [PATCH] Merge pull request #1237 from AndreyPolovodov/fix/time-based-cache-save-during-indexing Problem Saving cache every 10 files causes extremely slow indexing on large projects because save_all_caches() is an expensive operation. On a large project it gets triggered thousands of times during indexing, making the whole process take hours instead of minutes. Fix Switch from a file-count-based trigger (every 10 files) to a time-based trigger (every 30 seconds). This keeps cache saves infrequent enough to not hurt performance, while still preserving progress regularly in case indexing is interrupted. --- src/serena/cli.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/serena/cli.py b/src/serena/cli.py index 6ac341ce..7ee83a1f 100644 --- a/src/serena/cli.py +++ b/src/serena/cli.py @@ -5,6 +5,7 @@ import os import shutil import subprocess import sys +import time from collections.abc import Iterator, Sequence from logging import Logger from pathlib import Path @@ -705,6 +706,7 @@ class ProjectCommands(AutoRegisteringGroup): collected_exceptions: list[Exception] = [] files_failed = [] language_file_counts: dict[Language, int] = collections.defaultdict(lambda: 0) + last_save_time = time.monotonic() for i, f in enumerate(tqdm(files, desc="Indexing")): try: ls = ls_mgr.get_language_server(f) @@ -714,8 +716,10 @@ class ProjectCommands(AutoRegisteringGroup): log.error(f"Failed to index {f}, continuing.") collected_exceptions.append(e) files_failed.append(f) - if (i + 1) % 10 == 0: + now = time.monotonic() + if now - last_save_time >= 30: ls_mgr.save_all_caches() + last_save_time = now reported_language_file_counts = {k.value: v for k, v in language_file_counts.items()} click.echo(f"Indexed files per language: {dict_string(reported_language_file_counts, brackets=None)}") ls_mgr.save_all_caches()