Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit f2295cd

Browse files
committed
Report vfs memory usage in status command
1 parent f00dcf9 commit f2295cd

File tree

3 files changed

+14
-2
lines changed

3 files changed

+14
-2
lines changed

crates/rust-analyzer/src/global_state.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -440,6 +440,10 @@ impl GlobalStateSnapshot {
440440
ProjectWorkspace::DetachedFiles { .. } => None,
441441
})
442442
}
443+
444+
pub(crate) fn vfs_memory_usage(&self) -> usize {
445+
self.vfs.read().0.memory_usage()
446+
}
443447
}
444448

445449
pub(crate) fn file_id_to_url(vfs: &vfs::Vfs, id: FileId) -> Url {

crates/rust-analyzer/src/handlers.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,7 @@ pub(crate) fn handle_analyzer_status(
103103
.collect::<Vec<&AbsPath>>()
104104
);
105105
}
106+
format_to!(buf, "\nVfs memory usage: {}\n", snap.vfs_memory_usage());
106107
buf.push_str("\nAnalysis:\n");
107108
buf.push_str(
108109
&snap

crates/vfs/src/lib.rs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,11 @@ impl Vfs {
139139
self.get(file_id).as_deref().unwrap()
140140
}
141141

142+
/// Returns the overall memory usage for the stored files.
143+
pub fn memory_usage(&self) -> usize {
144+
self.data.iter().flatten().map(|d| d.capacity()).sum()
145+
}
146+
142147
/// Returns an iterator over the stored ids and their corresponding paths.
143148
///
144149
/// This will skip deleted files.
@@ -158,7 +163,7 @@ impl Vfs {
158163
///
159164
/// If the path does not currently exists in the `Vfs`, allocates a new
160165
/// [`FileId`] for it.
161-
pub fn set_file_contents(&mut self, path: VfsPath, contents: Option<Vec<u8>>) -> bool {
166+
pub fn set_file_contents(&mut self, path: VfsPath, mut contents: Option<Vec<u8>>) -> bool {
162167
let file_id = self.alloc_file_id(path);
163168
let change_kind = match (self.get(file_id), &contents) {
164169
(None, None) => return false,
@@ -167,7 +172,9 @@ impl Vfs {
167172
(Some(_), None) => ChangeKind::Delete,
168173
(Some(_), Some(_)) => ChangeKind::Modify,
169174
};
170-
175+
if let Some(contents) = &mut contents {
176+
contents.shrink_to_fit();
177+
}
171178
*self.get_mut(file_id) = contents;
172179
self.changes.push(ChangedFile { file_id, change_kind });
173180
true

0 commit comments

Comments
 (0)