Report vfs memory usage in status command

This commit is contained in:
Lukas Wirth 2023-04-22 09:57:48 +02:00
parent f00dcf9a69
commit f2295cda42
3 changed files with 14 additions and 2 deletions

View file

@ -440,6 +440,10 @@ impl GlobalStateSnapshot {
ProjectWorkspace::DetachedFiles { .. } => None, 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 { pub(crate) fn file_id_to_url(vfs: &vfs::Vfs, id: FileId) -> Url {

View file

@ -103,6 +103,7 @@ pub(crate) fn handle_analyzer_status(
.collect::<Vec<&AbsPath>>() .collect::<Vec<&AbsPath>>()
); );
} }
format_to!(buf, "\nVfs memory usage: {}\n", snap.vfs_memory_usage());
buf.push_str("\nAnalysis:\n"); buf.push_str("\nAnalysis:\n");
buf.push_str( buf.push_str(
&snap &snap

View file

@ -139,6 +139,11 @@ impl Vfs {
self.get(file_id).as_deref().unwrap() 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. /// Returns an iterator over the stored ids and their corresponding paths.
/// ///
/// This will skip deleted files. /// This will skip deleted files.
@ -158,7 +163,7 @@ impl Vfs {
/// ///
/// If the path does not currently exists in the `Vfs`, allocates a new /// If the path does not currently exists in the `Vfs`, allocates a new
/// [`FileId`] for it. /// [`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 file_id = self.alloc_file_id(path);
let change_kind = match (self.get(file_id), &contents) { let change_kind = match (self.get(file_id), &contents) {
(None, None) => return false, (None, None) => return false,
@ -167,7 +172,9 @@ impl Vfs {
(Some(_), None) => ChangeKind::Delete, (Some(_), None) => ChangeKind::Delete,
(Some(_), Some(_)) => ChangeKind::Modify, (Some(_), Some(_)) => ChangeKind::Modify,
}; };
if let Some(contents) = &mut contents {
contents.shrink_to_fit();
}
*self.get_mut(file_id) = contents; *self.get_mut(file_id) = contents;
self.changes.push(ChangedFile { file_id, change_kind }); self.changes.push(ChangedFile { file_id, change_kind });
true true