From f2295cda42f6284d8385451864a97c5cba499fac Mon Sep 17 00:00:00 2001 From: Lukas Wirth Date: Sat, 22 Apr 2023 09:57:48 +0200 Subject: [PATCH] Report vfs memory usage in status command --- crates/rust-analyzer/src/global_state.rs | 4 ++++ crates/rust-analyzer/src/handlers.rs | 1 + crates/vfs/src/lib.rs | 11 +++++++++-- 3 files changed, 14 insertions(+), 2 deletions(-) diff --git a/crates/rust-analyzer/src/global_state.rs b/crates/rust-analyzer/src/global_state.rs index ff1cdacb7f..b4f929c2ad 100644 --- a/crates/rust-analyzer/src/global_state.rs +++ b/crates/rust-analyzer/src/global_state.rs @@ -440,6 +440,10 @@ impl GlobalStateSnapshot { ProjectWorkspace::DetachedFiles { .. } => None, }) } + + pub(crate) fn vfs_memory_usage(&self) -> usize { + self.vfs.read().0.memory_usage() + } } pub(crate) fn file_id_to_url(vfs: &vfs::Vfs, id: FileId) -> Url { diff --git a/crates/rust-analyzer/src/handlers.rs b/crates/rust-analyzer/src/handlers.rs index bcc3eed4ff..3c39f205e7 100644 --- a/crates/rust-analyzer/src/handlers.rs +++ b/crates/rust-analyzer/src/handlers.rs @@ -103,6 +103,7 @@ pub(crate) fn handle_analyzer_status( .collect::>() ); } + format_to!(buf, "\nVfs memory usage: {}\n", snap.vfs_memory_usage()); buf.push_str("\nAnalysis:\n"); buf.push_str( &snap diff --git a/crates/vfs/src/lib.rs b/crates/vfs/src/lib.rs index 8ffa3a808f..b510b9e394 100644 --- a/crates/vfs/src/lib.rs +++ b/crates/vfs/src/lib.rs @@ -139,6 +139,11 @@ impl Vfs { self.get(file_id).as_deref().unwrap() } + /// Returns the overall memory usage for the stored files. + pub fn memory_usage(&self) -> usize { + self.data.iter().flatten().map(|d| d.capacity()).sum() + } + /// Returns an iterator over the stored ids and their corresponding paths. /// /// This will skip deleted files. @@ -158,7 +163,7 @@ impl Vfs { /// /// If the path does not currently exists in the `Vfs`, allocates a new /// [`FileId`] for it. - pub fn set_file_contents(&mut self, path: VfsPath, contents: Option>) -> bool { + pub fn set_file_contents(&mut self, path: VfsPath, mut contents: Option>) -> bool { let file_id = self.alloc_file_id(path); let change_kind = match (self.get(file_id), &contents) { (None, None) => return false, @@ -167,7 +172,9 @@ impl Vfs { (Some(_), None) => ChangeKind::Delete, (Some(_), Some(_)) => ChangeKind::Modify, }; - + if let Some(contents) = &mut contents { + contents.shrink_to_fit(); + } *self.get_mut(file_id) = contents; self.changes.push(ChangedFile { file_id, change_kind }); true