Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optimize manifest directory lookup with efficient caching #6444

Merged
merged 4 commits into from
Aug 22, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 25 additions & 14 deletions sway-lsp/src/server_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ pub struct ServerState {
pub(crate) cb_rx: Arc<Receiver<TaskMessage>>,
pub(crate) finished_compilation: Arc<Notify>,
pub(crate) pid_locked_files: PidLockedFiles,
manifest_cache: DashMap<Url, Arc<PathBuf>>,
last_compilation_state: Arc<RwLock<LastCompilationState>>,
}

Expand All @@ -68,6 +69,7 @@ impl Default for ServerState {
cb_rx: Arc::new(cb_rx),
finished_compilation: Arc::new(Notify::new()),
pid_locked_files: PidLockedFiles::new(),
manifest_cache: DashMap::new(),
last_compilation_state: Arc::new(RwLock::new(LastCompilationState::Uninitialized)),
};
// Spawn a new thread dedicated to handling compilation tasks
Expand Down Expand Up @@ -351,19 +353,27 @@ impl ServerState {
}

async fn url_to_session(&self, uri: &Url) -> Result<Arc<Session>, LanguageServerError> {
let path = PathBuf::from(uri.path());
let manifest = PackageManifestFile::from_dir(&path).map_err(|_| {
DocumentError::ManifestFileNotFound {
dir: path.to_string_lossy().to_string(),
}
})?;

// strip Forc.toml from the path to get the manifest directory
let manifest_dir = manifest
.path()
.parent()
.ok_or(DirectoryError::ManifestDirNotFound)?
.to_path_buf();
// Try to get the manifest directory from the cache
let manifest_dir = if let Some(cached_dir) = self.manifest_cache.get(uri) {
cached_dir.clone()
} else {
// Otherwise, find the manifest directory from the uri and cache it
let path = PathBuf::from(uri.path());
let manifest = PackageManifestFile::from_dir(&path).map_err(|_| {
DocumentError::ManifestFileNotFound {
dir: path.to_string_lossy().to_string(),
}
})?;
let dir = Arc::new(
manifest
.path()
.parent()
.ok_or(DirectoryError::ManifestDirNotFound)?
.to_path_buf(),
);
self.manifest_cache.insert(uri.clone(), dir.clone());
dir
};

// If the session is already in the cache, return it
if let Some(session) = self.sessions.get(&manifest_dir) {
Expand All @@ -373,7 +383,8 @@ impl ServerState {
// If no session can be found, then we need to call init and insert a new session into the map
let session = Arc::new(Session::new());
session.init(uri, &self.documents).await?;
self.sessions.insert(manifest_dir.clone(), session.clone());
self.sessions
.insert((*manifest_dir).clone(), session.clone());

Ok(session)
}
Expand Down
Loading