Skip to content

Commit

Permalink
11/n cache include lookups in salsa
Browse files Browse the repository at this point in the history
Summary:
Route the `IncludeCtx::resolve_local` and `IncludeCtx::resolve_remote` calls through salsa so the results are cached by `SourceRootId` and path.

This speeds up parse-all from 41s to 30s.

Reviewed By: michalmuskala

Differential Revision: D60509887

fbshipit-source-id: be5f834c6353fc7685a63af686c163f9af7a8878
  • Loading branch information
alanz authored and facebook-github-bot committed Aug 1, 2024
1 parent 85b1b4d commit 79fa273
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 11 deletions.
34 changes: 23 additions & 11 deletions crates/base_db/src/include.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

use std::sync::Arc;

use elp_syntax::SmolStr;
use vfs::FileId;

use crate::SourceDatabase;
Expand Down Expand Up @@ -38,12 +39,12 @@ impl<'a> IncludeCtx<'a> {

pub fn resolve_include(&self, path: &str) -> Option<FileId> {
self.resolve_relative(path)
.or_else(|| self.resolve_local(path))
.or_else(|| self.db.resolve_local(self.source_root_id, path.into()))
}

pub fn resolve_include_lib(&self, path: &str) -> Option<FileId> {
self.resolve_include(path)
.or_else(|| self.resolve_remote(path))
.or_else(|| self.db.resolve_remote(self.source_root_id, path.into()))
}

pub fn resolve_include_doc(&self, path: &str) -> Option<FileId> {
Expand All @@ -54,22 +55,33 @@ impl<'a> IncludeCtx<'a> {
self.source_root.relative_path(self.file_id, path)
}

fn resolve_local(&self, path: &str) -> Option<FileId> {
let app_data = self.db.app_data(self.source_root_id)?;
/// Called via salsa for inserting in the graph
pub(crate) fn resolve_local_query(
db: &dyn SourceDatabase,
source_root_id: SourceRootId,
path: SmolStr,
) -> Option<FileId> {
let path: &str = &path;
let app_data = db.app_data(source_root_id)?;
let source_root = db.source_root(source_root_id);
app_data.include_path.iter().find_map(|include| {
let name = include.join(path);
self.source_root.file_for_path(&name.into())
source_root.file_for_path(&name.into())
})
}

fn resolve_remote(&self, path: &str) -> Option<FileId> {
let app_data = self.db.app_data(self.source_root_id)?;
let project_data = self.db.project_data(app_data.project_id);

/// Called via salsa for inserting in the graph
pub(crate) fn resolve_remote_query(
db: &dyn SourceDatabase,
source_root_id: SourceRootId,
path: SmolStr,
) -> Option<FileId> {
let app_data = db.app_data(source_root_id)?;
let project_data = db.project_data(app_data.project_id);
let (app_name, path) = path.split_once('/')?;
let source_root_id = project_data.app_roots.get(app_name)?;
let source_root = self.db.source_root(source_root_id);
let target_app_data = self.db.app_data(source_root_id)?;
let source_root = db.source_root(source_root_id);
let target_app_data = db.app_data(source_root_id)?;
let path = target_app_data.dir.join(path);
source_root.file_for_path(&path.into())
}
Expand Down
7 changes: 7 additions & 0 deletions crates/base_db/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ use elp_project_model::AppName;
use elp_syntax::ast::SourceFile;
use elp_syntax::AstNode;
use elp_syntax::Parse;
use elp_syntax::SmolStr;
use elp_syntax::TextRange;
use elp_syntax::TextSize;
use lazy_static::lazy_static;
Expand Down Expand Up @@ -186,6 +187,12 @@ pub trait SourceDatabase: FileLoader + salsa::Database {
fn clamp_range(&self, file_id: FileId, range: TextRange) -> TextRange;

fn clamp_offset(&self, file_id: FileId, offset: TextSize) -> TextSize;

#[salsa::invoke(IncludeCtx::resolve_local_query)]
fn resolve_local(&self, source_root: SourceRootId, path: SmolStr) -> Option<FileId>;

#[salsa::invoke(IncludeCtx::resolve_remote_query)]
fn resolve_remote(&self, source_root: SourceRootId, path: SmolStr) -> Option<FileId>;
}

fn module_index(db: &dyn SourceDatabase, project_id: ProjectId) -> Arc<ModuleIndex> {
Expand Down

0 comments on commit 79fa273

Please sign in to comment.