about summary refs log tree commit diff
diff options
context:
space:
mode:
authorLukas Wirth <lukastw97@gmail.com>2023-04-22 09:57:48 +0200
committerLukas Wirth <lukastw97@gmail.com>2023-04-22 09:57:48 +0200
commitf2295cda42f6284d8385451864a97c5cba499fac (patch)
tree9fe73a99a0a6e21d3303f68eb68482fc5002627b
parentf00dcf9a69c269bc871f72f3f30ce02a85e5a4ac (diff)
downloadrust-f2295cda42f6284d8385451864a97c5cba499fac.tar.gz
rust-f2295cda42f6284d8385451864a97c5cba499fac.zip
Report vfs memory usage in status command
-rw-r--r--crates/rust-analyzer/src/global_state.rs4
-rw-r--r--crates/rust-analyzer/src/handlers.rs1
-rw-r--r--crates/vfs/src/lib.rs11
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 ff1cdacb7ff..b4f929c2ad1 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 bcc3eed4ff9..3c39f205e74 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::<Vec<&AbsPath>>()
         );
     }
+    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 8ffa3a808f1..b510b9e3942 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<Vec<u8>>) -> bool {
+    pub fn set_file_contents(&mut self, path: VfsPath, mut contents: Option<Vec<u8>>) -> 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