VFS no longer stores all source files in memory

This commit is contained in:
Lukas Wirth 2024-01-07 20:31:56 +01:00
parent af40101841
commit 1c40ac79c8
9 changed files with 130 additions and 143 deletions

View file

@ -1,8 +1,8 @@
//! # Virtual File System
//!
//! VFS stores all files read by rust-analyzer. Reading file contents from VFS
//! always returns the same contents, unless VFS was explicitly modified with
//! [`set_file_contents`]. All changes to VFS are logged, and can be retrieved via
//! VFS records all file changes pushed to it via [`set_file_contents`].
//! As such it only ever stores changes, not the actual content of a file at any given moment.
//! All file changes are logged, and can be retrieved via
//! [`take_changes`] method. The pack of changes is then pushed to `salsa` and
//! triggers incremental recomputation.
//!
@ -84,40 +84,65 @@ impl FileId {
/// safe because `FileId` is a newtype of `u32`
impl nohash_hasher::IsEnabled for FileId {}
/// Storage for all files read by rust-analyzer.
/// Storage for all file changes and the file id to path mapping.
///
/// For more information see the [crate-level](crate) documentation.
#[derive(Default)]
pub struct Vfs {
interner: PathInterner,
data: Vec<Option<Vec<u8>>>,
data: Vec<FileState>,
changes: Vec<ChangedFile>,
}
#[derive(Copy, PartialEq, PartialOrd, Clone)]
pub enum FileState {
Exists,
Deleted,
}
/// Changed file in the [`Vfs`].
#[derive(Debug)]
pub struct ChangedFile {
/// Id of the changed file
pub file_id: FileId,
/// Kind of change
pub change_kind: ChangeKind,
pub change: Change,
}
impl ChangedFile {
/// Returns `true` if the change is not [`Delete`](ChangeKind::Delete).
pub fn exists(&self) -> bool {
self.change_kind != ChangeKind::Delete
!matches!(self.change, Change::Delete)
}
/// Returns `true` if the change is [`Create`](ChangeKind::Create) or
/// [`Delete`](ChangeKind::Delete).
/// [`Delete`](Change::Delete).
pub fn is_created_or_deleted(&self) -> bool {
matches!(self.change_kind, ChangeKind::Create | ChangeKind::Delete)
matches!(self.change, Change::Create(_) | Change::Delete)
}
pub fn kind(&self) -> ChangeKind {
match self.change {
Change::Create(_) => ChangeKind::Create,
Change::Modify(_) => ChangeKind::Modify,
Change::Delete => ChangeKind::Delete,
}
}
}
/// Kind of [file change](ChangedFile).
#[derive(Eq, PartialEq, Copy, Clone, Debug)]
#[derive(Eq, PartialEq, Debug)]
pub enum Change {
/// The file was (re-)created
Create(Vec<u8>),
/// The file was modified
Modify(Vec<u8>),
/// The file was deleted
Delete,
}
/// Kind of [file change](ChangedFile).
#[derive(Eq, PartialEq, Debug)]
pub enum ChangeKind {
/// The file was (re-)created
Create,
@ -130,7 +155,7 @@ pub enum ChangeKind {
impl Vfs {
/// Id of the given path if it exists in the `Vfs` and is not deleted.
pub fn file_id(&self, path: &VfsPath) -> Option<FileId> {
self.interner.get(path).filter(|&it| self.get(it).is_some())
self.interner.get(path).filter(|&it| matches!(self.get(it), FileState::Exists))
}
/// File path corresponding to the given `file_id`.
@ -142,28 +167,13 @@ impl Vfs {
self.interner.lookup(file_id).clone()
}
/// File content corresponding to the given `file_id`.
///
/// # Panics
///
/// Panics if the id is not present in the `Vfs`, or if the corresponding file is
/// deleted.
pub fn file_contents(&self, file_id: FileId) -> &[u8] {
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.
pub fn iter(&self) -> impl Iterator<Item = (FileId, &VfsPath)> + '_ {
(0..self.data.len())
.map(|it| FileId(it as u32))
.filter(move |&file_id| self.get(file_id).is_some())
.filter(move |&file_id| matches!(self.get(file_id), FileState::Exists))
.map(move |file_id| {
let path = self.interner.lookup(file_id);
(file_id, path)
@ -176,28 +186,21 @@ 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, mut contents: Option<Vec<u8>>) -> bool {
pub fn set_file_contents(&mut self, path: VfsPath, 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,
(Some(old), Some(new)) if old == new => return false,
(None, Some(_)) => ChangeKind::Create,
(Some(_), None) => ChangeKind::Delete,
(Some(_), Some(_)) => ChangeKind::Modify,
let change_kind = match (self.get(file_id), contents) {
(FileState::Deleted, None) => return false,
(FileState::Deleted, Some(v)) => Change::Create(v),
(FileState::Exists, None) => Change::Delete,
(FileState::Exists, Some(v)) => Change::Modify(v),
};
if let Some(contents) = &mut contents {
contents.shrink_to_fit();
}
*self.get_mut(file_id) = contents;
self.changes.push(ChangedFile { file_id, change_kind });
let changed_file = ChangedFile { file_id, change: change_kind };
self.data[file_id.0 as usize] =
if changed_file.exists() { FileState::Exists } else { FileState::Deleted };
self.changes.push(changed_file);
true
}
/// Returns `true` if the `Vfs` contains [changes](ChangedFile).
pub fn has_changes(&self) -> bool {
!self.changes.is_empty()
}
/// Drain and returns all the changes in the `Vfs`.
pub fn take_changes(&mut self) -> Vec<ChangedFile> {
mem::take(&mut self.changes)
@ -205,7 +208,7 @@ impl Vfs {
/// Provides a panic-less way to verify file_id validity.
pub fn exists(&self, file_id: FileId) -> bool {
self.get(file_id).is_some()
matches!(self.get(file_id), FileState::Exists)
}
/// Returns the id associated with `path`
@ -219,26 +222,17 @@ impl Vfs {
let file_id = self.interner.intern(path);
let idx = file_id.0 as usize;
let len = self.data.len().max(idx + 1);
self.data.resize_with(len, || None);
self.data.resize(len, FileState::Deleted);
file_id
}
/// Returns the content associated with the given `file_id`.
/// Returns the status of the file associated with the given `file_id`.
///
/// # Panics
///
/// Panics if no file is associated to that id.
fn get(&self, file_id: FileId) -> &Option<Vec<u8>> {
&self.data[file_id.0 as usize]
}
/// Mutably returns the content associated with the given `file_id`.
///
/// # Panics
///
/// Panics if no file is associated to that id.
fn get_mut(&mut self, file_id: FileId) -> &mut Option<Vec<u8>> {
&mut self.data[file_id.0 as usize]
fn get(&self, file_id: FileId) -> FileState {
self.data[file_id.0 as usize]
}
}