From 31af59152511df6a6d3c4dca62843ff0a4b3decd Mon Sep 17 00:00:00 2001
From: l0ng-ai <24760907+l0ng-ai@users.noreply.github.com>
Date: Wed, 2 Dec 2026 13:41:06 +0800
Subject: [PATCH] perf(index): space snapshot refreshes by how long the last
one took
Any index write drops the startup snapshot, and it was written again at
most every 10 s, also inline after every subtree scan. In a project of
160 000 files a write takes up to 11 s and holds the disk, so a build
writing into the project kept it rewriting for minutes after startup.
The gap after a write is now 30 times what it took, between 10 s and
5 min; a subtree scan within the gap leaves the write to the scheduled
refresh. Full scans still write at once. The snapshot only speeds up the
next startup: one that is missing falls back to a scan.
Refs #7363
---
crates/ide-index/src/worker.rs | 71 ++++++++++++++++++++++++++--------
1 file changed, 55 insertions(+), 16 deletions(-)
diff --git a/crates/ide-index/src/worker.rs b/crates/ide-index/src/worker.rs
index a454c99c1..db50ee589 100644
--- a/crates/ide-index/src/worker.rs
+++ b/crates/ide-index/src/worker.rs
@@ -46,6 +46,20 @@ const SNAPSHOT_DELAY: Duration = Duration::from_millis(50);
const SNAPSHOT_EVERY: Duration = Duration::from_secs(10);
#[cfg(test)]
const SNAPSHOT_EVERY: Duration = Duration::ZERO;
+/// A refresh waits this many times as long as the last one took, up to [`SNAPSHOT_EVERY_MAX`]: in
+/// a project of 160 000 files a write takes seconds and holds the disk, and a build writing into
+/// it would otherwise have it rewritten every few seconds for minutes (#7363).
+const SNAPSHOT_COST_FACTOR: u32 = 30;
+const SNAPSHOT_EVERY_MAX: Duration = Duration::from_secs(300);
+
+/// The shortest gap after a snapshot write that took `cost` (none in tests, as [`SNAPSHOT_EVERY`]).
+fn snapshot_gap(cost: Duration) -> Duration {
+ if cfg!(test) { SNAPSHOT_EVERY } else { gap_after(cost, SNAPSHOT_EVERY) }
+}
+
+fn gap_after(cost: Duration, every: Duration) -> Duration {
+ every.max(cost.saturating_mul(SNAPSHOT_COST_FACTOR).min(SNAPSHOT_EVERY_MAX))
+}
/// A subtree scan with fewer files to parse runs without showing "Indexing" (see `scan`).
const QUIET_SCAN_FILES: usize = 200;
pub(crate) const MAX_RECENT: usize = 50;
@@ -87,8 +101,9 @@ pub(crate) struct Shared {
idle: Condvar,
/// Coalesced post-update snapshot refresh.
snapshot_due: Mutex