From d76463c8573964ffcdff9f47f98bfe0f8254e62b Mon Sep 17 00:00:00 2001 From: Chayim Refael Friedman Date: Tue, 21 Oct 2025 22:58:31 +0300 Subject: [PATCH] Clear next-solver cache before reporting memory usage in analysis-stats The cache shouldn't be included, as it is mostly temporary (per-revision). --- .../crates/hir-ty/src/next_solver/interner.rs | 9 +++++++++ src/tools/rust-analyzer/crates/hir/src/lib.rs | 1 + .../crates/rust-analyzer/src/cli/analysis_stats.rs | 2 ++ 3 files changed, 12 insertions(+) diff --git a/src/tools/rust-analyzer/crates/hir-ty/src/next_solver/interner.rs b/src/tools/rust-analyzer/crates/hir-ty/src/next_solver/interner.rs index 42f1d926d7db..ce8b76837a3c 100644 --- a/src/tools/rust-analyzer/crates/hir-ty/src/next_solver/interner.rs +++ b/src/tools/rust-analyzer/crates/hir-ty/src/next_solver/interner.rs @@ -2,6 +2,7 @@ use std::{fmt, ops::ControlFlow}; +pub use tls_cache::clear_tls_solver_cache; pub use tls_db::{attach_db, attach_db_allow_change, with_attached_db}; use base_db::Crate; @@ -2239,4 +2240,12 @@ mod tls_cache { }) }) } + + /// Clears the thread-local trait solver cache. + /// + /// Should be called before getting memory usage estimations, as the solver cache + /// is per-revision and usually should be excluded from estimations. + pub fn clear_tls_solver_cache() { + GLOBAL_CACHE.with_borrow_mut(|handle| *handle = None); + } } diff --git a/src/tools/rust-analyzer/crates/hir/src/lib.rs b/src/tools/rust-analyzer/crates/hir/src/lib.rs index 48eafb0bd4c6..941890312317 100644 --- a/src/tools/rust-analyzer/crates/hir/src/lib.rs +++ b/src/tools/rust-analyzer/crates/hir/src/lib.rs @@ -171,6 +171,7 @@ pub use { method_resolution::TyFingerprint, mir::{MirEvalError, MirLowerError}, next_solver::abi::Safety, + next_solver::clear_tls_solver_cache, }, intern::{Symbol, sym}, }; diff --git a/src/tools/rust-analyzer/crates/rust-analyzer/src/cli/analysis_stats.rs b/src/tools/rust-analyzer/crates/rust-analyzer/src/cli/analysis_stats.rs index 717bd230a21e..de24bc09ff0f 100644 --- a/src/tools/rust-analyzer/crates/rust-analyzer/src/cli/analysis_stats.rs +++ b/src/tools/rust-analyzer/crates/rust-analyzer/src/cli/analysis_stats.rs @@ -345,6 +345,8 @@ impl flags::AnalysisStats { self.run_term_search(&workspace, db, &vfs, &file_ids, verbosity); } + hir::clear_tls_solver_cache(); + let db = host.raw_database_mut(); db.trigger_lru_eviction();