дзеркало
https://github.com/l0ng-ai/openidea.git
синхронізовано 2026-12-03 16:51:16 +02:00
perf(index): space snapshot refreshes by how long the last one took (#7401)
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
Цей коміт міститься в:
1 джерело
de41651a5e
коміт
2b73ef73f1
1 file changed
+55
-16
@@ -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<Option<Instant>>,
|
||||
/// When the snapshot was last refreshed (see [`SNAPSHOT_EVERY`]).
|
||||
snapshot_saved: Mutex<Option<Instant>>,
|
||||
/// When the snapshot was last refreshed, and how long the next one waits after it
|
||||
/// (see [`snapshot_gap`]).
|
||||
snapshot_saved: Mutex<Option<(Instant, Duration)>>,
|
||||
/// Directories whose watcher events still need reconciliation before a
|
||||
/// refreshed snapshot can be trusted.
|
||||
pending_dirs: Mutex<HashSet<PathBuf>>,
|
||||
@@ -683,18 +698,19 @@ impl Shared {
|
||||
let durable = durable_started.elapsed();
|
||||
let update_elapsed = update_started.elapsed();
|
||||
let snapshot_started = Instant::now();
|
||||
let mut deferred_snapshot = false;
|
||||
if let Some(store) = &self.store {
|
||||
let recent: Vec<String> = self.recent.lock().iter().filter_map(|p| filter.recent_key(p)).collect();
|
||||
// process() may already have applied the change and deleted the snapshot;
|
||||
// a follow-up reconcile scan can have scheduled=0 and still need to write it.
|
||||
// A full scan writes it at once; a subtree scan within the gap after the last
|
||||
// write leaves it to the scheduled refresh.
|
||||
if index_changed || !store.snapshot_exists() {
|
||||
let d = self.data.read();
|
||||
let snapshot_started = Instant::now();
|
||||
if let Err(e) = store.save_snapshot(&d, &filter, guard) {
|
||||
tracing::warn!("index snapshot: {e}");
|
||||
if dirs.is_none() || self.snapshot_may_refresh() {
|
||||
self.write_snapshot(store, &filter, guard);
|
||||
} else {
|
||||
deferred_snapshot = true;
|
||||
}
|
||||
tracing::info!("index snapshot: written in {:?}", snapshot_started.elapsed());
|
||||
*self.snapshot_saved.lock() = Some(Instant::now());
|
||||
}
|
||||
if let Err(e) = store.save_recent_file(&recent) {
|
||||
tracing::warn!("index recent snapshot: {e}");
|
||||
@@ -702,6 +718,9 @@ impl Shared {
|
||||
}
|
||||
let snapshot_elapsed = snapshot_started.elapsed();
|
||||
*self.snapshot_due.lock() = None;
|
||||
if deferred_snapshot {
|
||||
self.schedule_snapshot();
|
||||
}
|
||||
if let Some(task) = task {
|
||||
self.indexing.store(false, Ordering::SeqCst);
|
||||
self.sink.emit(IdeEvent::Task {
|
||||
@@ -856,11 +875,27 @@ impl Shared {
|
||||
}
|
||||
let mut due = self.snapshot_due.lock();
|
||||
if due.is_none() {
|
||||
let soonest = self.snapshot_saved.lock().map(|saved| saved + SNAPSHOT_EVERY);
|
||||
let soonest = self.snapshot_saved.lock().map(|(saved, gap)| saved + gap);
|
||||
*due = Some((Instant::now() + SNAPSHOT_DELAY).max(soonest.unwrap_or(Instant::now())));
|
||||
}
|
||||
}
|
||||
|
||||
/// Whether the gap after the last snapshot write has passed.
|
||||
fn snapshot_may_refresh(&self) -> bool {
|
||||
self.snapshot_saved.lock().is_none_or(|(saved, gap)| saved.elapsed() >= gap)
|
||||
}
|
||||
|
||||
fn write_snapshot(&self, store: &Store, filter: &PathFilter, guard: SystemTime) {
|
||||
let d = self.data.read();
|
||||
let started = Instant::now();
|
||||
if let Err(e) = store.save_snapshot(&d, filter, guard) {
|
||||
tracing::warn!("index snapshot: {e}");
|
||||
}
|
||||
let cost = started.elapsed();
|
||||
tracing::info!("index snapshot: written in {cost:?}");
|
||||
*self.snapshot_saved.lock() = Some((Instant::now(), snapshot_gap(cost)));
|
||||
}
|
||||
|
||||
/// Reconciles the folders the watcher reported since the last snapshot, then refreshes it.
|
||||
/// A folder the index already has files below is listed one level deep: its own files, plus
|
||||
/// any subfolder that appeared (walked whole) or is gone (its files dropped). A folder it
|
||||
@@ -920,15 +955,9 @@ impl Shared {
|
||||
return;
|
||||
}
|
||||
if let Some(store) = &self.store {
|
||||
let filter = self.filter();
|
||||
let guard = SystemTime::now();
|
||||
let d = self.data.read();
|
||||
if let Err(e) = store.save_snapshot(&d, &filter, guard) {
|
||||
tracing::warn!("index snapshot: {e}");
|
||||
}
|
||||
self.write_snapshot(store, &self.filter(), SystemTime::now());
|
||||
}
|
||||
*self.snapshot_due.lock() = None;
|
||||
*self.snapshot_saved.lock() = Some(Instant::now());
|
||||
}
|
||||
|
||||
fn persist(&self, puts: &[(String, FileRecord)], deletes: &[String], durable: bool) {
|
||||
@@ -1704,6 +1733,16 @@ mod metric_tests {
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
#[test]
|
||||
fn a_costly_snapshot_waits_longer_before_the_next() {
|
||||
use super::{SNAPSHOT_EVERY_MAX, gap_after};
|
||||
use std::time::Duration;
|
||||
let every = Duration::from_secs(10);
|
||||
assert_eq!(gap_after(Duration::from_millis(120), every), every);
|
||||
assert_eq!(gap_after(Duration::from_secs(4), every), Duration::from_secs(120));
|
||||
assert_eq!(gap_after(Duration::from_secs(11), every), SNAPSHOT_EVERY_MAX);
|
||||
}
|
||||
|
||||
use crate::data::safe_rel;
|
||||
|
||||
#[test]
|
||||
|
||||
Reference in new issue
Заблокувати користувача