From 8cf092d5de113fc218b84421a2db4449a370ccb6 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Fri, 25 Jan 2019 15:16:50 +0300 Subject: :arrow_up salsa --- crates/ra_db/Cargo.toml | 2 +- crates/ra_db/src/input.rs | 42 ------------------------- crates/ra_db/src/lib.rs | 68 ++++++++++++++++++++++++++++++++--------- crates/ra_hir/Cargo.toml | 2 ++ crates/ra_hir/src/mock.rs | 50 +++--------------------------- crates/ra_ide_api/Cargo.toml | 1 + crates/ra_ide_api/src/db.rs | 56 +++++---------------------------- crates/ra_ide_api/src/status.rs | 2 +- 8 files changed, 71 insertions(+), 152 deletions(-) (limited to 'crates') diff --git a/crates/ra_db/Cargo.toml b/crates/ra_db/Cargo.toml index bb1b5eae7..3568da905 100644 --- a/crates/ra_db/Cargo.toml +++ b/crates/ra_db/Cargo.toml @@ -6,7 +6,7 @@ authors = ["Aleksey Kladov "] [dependencies] relative-path = "0.4.0" -salsa = "0.10.0-alpha1" +salsa = "0.10.0-alpha3" rustc-hash = "1.0" parking_lot = "0.7.0" ra_arena = { path = "../ra_arena" } diff --git a/crates/ra_db/src/input.rs b/crates/ra_db/src/input.rs index 9825d52cf..275894252 100644 --- a/crates/ra_db/src/input.rs +++ b/crates/ra_db/src/input.rs @@ -5,11 +5,8 @@ /// Note that neither this module, nor any other part of the analyzer's core do /// actual IO. See `vfs` and `project_model` in the `ra_lsp_server` crate for how /// actual IO is done and lowered to input. -use std::sync::Arc; - use relative_path::RelativePathBuf; use rustc_hash::FxHashMap; -use salsa; use ra_syntax::SmolStr; use rustc_hash::FxHashSet; @@ -146,45 +143,6 @@ impl CrateGraph { } } -#[salsa::query_group] -pub trait FilesDatabase: salsa::Database { - /// Text of the file. - #[salsa::input] - fn file_text(&self, file_id: FileId) -> Arc; - /// Path to a file, relative to the root of its source root. - #[salsa::input] - fn file_relative_path(&self, file_id: FileId) -> RelativePathBuf; - /// Source root of the file. - #[salsa::input] - fn file_source_root(&self, file_id: FileId) -> SourceRootId; - /// Contents of the source root. - #[salsa::input] - fn source_root(&self, id: SourceRootId) -> Arc; - fn source_root_crates(&self, id: SourceRootId) -> Arc>; - /// The set of "local" (that is, from the current workspace) roots. - /// Files in local roots are assumed to change frequently. - #[salsa::input] - fn local_roots(&self) -> Arc>; - /// The set of roots for crates.io libraries. - /// Files in libraries are assumed to never change. - #[salsa::input] - fn library_roots(&self) -> Arc>; - /// The crate graph. - #[salsa::input] - fn crate_graph(&self) -> Arc; -} - -fn source_root_crates(db: &impl FilesDatabase, id: SourceRootId) -> Arc> { - let root = db.source_root(id); - let graph = db.crate_graph(); - let res = root - .files - .values() - .filter_map(|&it| graph.crate_id_for_crate_root(it)) - .collect::>(); - Arc::new(res) -} - #[cfg(test)] mod tests { use super::{CrateGraph, FileId, SmolStr}; diff --git a/crates/ra_db/src/lib.rs b/crates/ra_db/src/lib.rs index 84759c75a..7e13f70bc 100644 --- a/crates/ra_db/src/lib.rs +++ b/crates/ra_db/src/lib.rs @@ -4,17 +4,18 @@ mod input; mod loc2id; pub mod mock; -use std::panic; +use std::{ + panic, sync::Arc, +}; use ra_syntax::{TextUnit, TextRange, SourceFile, TreeArc}; +use relative_path::RelativePathBuf; pub use ::salsa as salsa; pub use crate::{ cancellation::Canceled, input::{ - FilesDatabase, FileId, CrateId, SourceRoot, SourceRootId, CrateGraph, Dependency, - FileTextQuery, FileSourceRootQuery, SourceRootQuery, SourceRootCratesQuery, LocalRootsQuery, LibraryRootsQuery, CrateGraphQuery, - FileRelativePathQuery + FileId, CrateId, SourceRoot, SourceRootId, CrateGraph, Dependency, }, loc2id::LocationIntener, }; @@ -50,16 +51,6 @@ pub trait BaseDatabase: salsa::Database + panic::RefUnwindSafe { } } -#[salsa::query_group] -pub trait SyntaxDatabase: crate::input::FilesDatabase + BaseDatabase { - fn source_file(&self, file_id: FileId) -> TreeArc; -} - -fn source_file(db: &impl SyntaxDatabase, file_id: FileId) -> TreeArc { - let text = db.file_text(file_id); - SourceFile::parse(&*text) -} - #[derive(Clone, Copy, Debug)] pub struct FilePosition { pub file_id: FileId, @@ -71,3 +62,52 @@ pub struct FileRange { pub file_id: FileId, pub range: TextRange, } + +#[salsa::query_group] +pub trait FilesDatabase: salsa::Database { + /// Text of the file. + #[salsa::input] + fn file_text(&self, file_id: FileId) -> Arc; + /// Path to a file, relative to the root of its source root. + #[salsa::input] + fn file_relative_path(&self, file_id: FileId) -> RelativePathBuf; + /// Source root of the file. + #[salsa::input] + fn file_source_root(&self, file_id: FileId) -> SourceRootId; + /// Contents of the source root. + #[salsa::input] + fn source_root(&self, id: SourceRootId) -> Arc; + fn source_root_crates(&self, id: SourceRootId) -> Arc>; + /// The set of "local" (that is, from the current workspace) roots. + /// Files in local roots are assumed to change frequently. + #[salsa::input] + fn local_roots(&self) -> Arc>; + /// The set of roots for crates.io libraries. + /// Files in libraries are assumed to never change. + #[salsa::input] + fn library_roots(&self) -> Arc>; + /// The crate graph. + #[salsa::input] + fn crate_graph(&self) -> Arc; +} + +fn source_root_crates(db: &impl FilesDatabase, id: SourceRootId) -> Arc> { + let root = db.source_root(id); + let graph = db.crate_graph(); + let res = root + .files + .values() + .filter_map(|&it| graph.crate_id_for_crate_root(it)) + .collect::>(); + Arc::new(res) +} + +#[salsa::query_group] +pub trait SyntaxDatabase: FilesDatabase + BaseDatabase { + fn source_file(&self, file_id: FileId) -> TreeArc; +} + +fn source_file(db: &impl SyntaxDatabase, file_id: FileId) -> TreeArc { + let text = db.file_text(file_id); + SourceFile::parse(&*text) +} diff --git a/crates/ra_hir/Cargo.toml b/crates/ra_hir/Cargo.toml index 86937ccd8..3d802ade4 100644 --- a/crates/ra_hir/Cargo.toml +++ b/crates/ra_hir/Cargo.toml @@ -12,6 +12,8 @@ rustc-hash = "1.0" parking_lot = "0.7.0" ena = "0.11" join_to_string = "0.1.3" +salsa = "0.10.0-alpha3" + ra_syntax = { path = "../ra_syntax" } ra_arena = { path = "../ra_arena" } ra_db = { path = "../ra_db" } diff --git a/crates/ra_hir/src/mock.rs b/crates/ra_hir/src/mock.rs index 1a83a5c87..aa54336b8 100644 --- a/crates/ra_hir/src/mock.rs +++ b/crates/ra_hir/src/mock.rs @@ -12,6 +12,7 @@ use crate::{db, HirInterner}; pub const WORKSPACE: SourceRootId = SourceRootId(0); +#[salsa::database(ra_db::FilesDatabase, ra_db::SyntaxDatabase, db::HirDatabase)] #[derive(Debug)] pub(crate) struct MockDatabase { events: Mutex>>>, @@ -181,8 +182,7 @@ impl MockDatabase { pub(crate) fn log(&self, f: impl FnOnce()) -> Vec> { *self.events.lock() = Some(Vec::new()); f(); - let events = self.events.lock().take().unwrap(); - events + self.events.lock().take().unwrap() } pub(crate) fn log_executed(&self, f: impl FnOnce()) -> Vec { @@ -192,51 +192,11 @@ impl MockDatabase { .filter_map(|e| match e.kind { // This pretty horrible, but `Debug` is the only way to inspect // QueryDescriptor at the moment. - salsa::EventKind::WillExecute { descriptor } => Some(format!("{:?}", descriptor)), + salsa::EventKind::WillExecute { database_key } => { + Some(format!("{:?}", database_key)) + } _ => None, }) .collect() } } - -salsa::database_storage! { - pub(crate) struct MockDatabaseStorage for MockDatabase { - impl ra_db::FilesDatabase { - fn file_text() for ra_db::FileTextQuery; - fn file_relative_path() for ra_db::FileRelativePathQuery; - fn file_source_root() for ra_db::FileSourceRootQuery; - fn source_root() for ra_db::SourceRootQuery; - fn source_root_crates() for ra_db::SourceRootCratesQuery; - fn local_roots() for ra_db::LocalRootsQuery; - fn library_roots() for ra_db::LibraryRootsQuery; - fn crate_graph() for ra_db::CrateGraphQuery; - } - impl ra_db::SyntaxDatabase { - fn source_file() for ra_db::SourceFileQuery; - } - impl db::HirDatabase { - fn hir_source_file() for db::HirSourceFileQuery; - fn expand_macro_invocation() for db::ExpandMacroInvocationQuery; - fn module_tree() for db::ModuleTreeQuery; - fn fn_scopes() for db::FnScopesQuery; - fn file_items() for db::FileItemsQuery; - fn file_item() for db::FileItemQuery; - fn lower_module() for db::LowerModuleQuery; - fn lower_module_module() for db::LowerModuleModuleQuery; - fn lower_module_source_map() for db::LowerModuleSourceMapQuery; - fn item_map() for db::ItemMapQuery; - fn submodules() for db::SubmodulesQuery; - fn infer() for db::InferQuery; - fn type_for_def() for db::TypeForDefQuery; - fn type_for_field() for db::TypeForFieldQuery; - fn struct_data() for db::StructDataQuery; - fn enum_data() for db::EnumDataQuery; - fn impls_in_module() for db::ImplsInModuleQuery; - fn impls_in_crate() for db::ImplsInCrateQuery; - fn body_hir() for db::BodyHirQuery; - fn body_syntax_mapping() for db::BodySyntaxMappingQuery; - fn fn_signature() for db::FnSignatureQuery; - fn generic_params() for db::GenericParamsQuery; - } - } -} diff --git a/crates/ra_ide_api/Cargo.toml b/crates/ra_ide_api/Cargo.toml index 20c911f8f..61942bbbb 100644 --- a/crates/ra_ide_api/Cargo.toml +++ b/crates/ra_ide_api/Cargo.toml @@ -13,6 +13,7 @@ fst = "0.3.1" rustc-hash = "1.0" parking_lot = "0.7.0" unicase = "2.2.0" +salsa = "0.10.0-alpha3" ra_syntax = { path = "../ra_syntax" } ra_ide_api_light = { path = "../ra_ide_api_light" } diff --git a/crates/ra_ide_api/src/db.rs b/crates/ra_ide_api/src/db.rs index 11305613c..f0190ae51 100644 --- a/crates/ra_ide_api/src/db.rs +++ b/crates/ra_ide_api/src/db.rs @@ -7,6 +7,13 @@ use ra_db::{ use crate::{symbol_index, LineIndex}; +#[salsa::database( + ra_db::FilesDatabase, + ra_db::SyntaxDatabase, + LineIndexDatabase, + symbol_index::SymbolsDatabase, + hir::db::HirDatabase +)] #[derive(Debug)] pub(crate) struct RootDatabase { runtime: salsa::Runtime, @@ -64,52 +71,3 @@ fn line_index(db: &impl ra_db::FilesDatabase, file_id: FileId) -> Arc let text = db.file_text(file_id); Arc::new(LineIndex::new(&*text)) } - -salsa::database_storage! { - pub(crate) struct RootDatabaseStorage for RootDatabase { - impl ra_db::FilesDatabase { - fn file_text() for ra_db::FileTextQuery; - fn file_relative_path() for ra_db::FileRelativePathQuery; - fn file_source_root() for ra_db::FileSourceRootQuery; - fn source_root() for ra_db::SourceRootQuery; - fn source_root_crates() for ra_db::SourceRootCratesQuery; - fn local_roots() for ra_db::LocalRootsQuery; - fn library_roots() for ra_db::LibraryRootsQuery; - fn crate_graph() for ra_db::CrateGraphQuery; - } - impl ra_db::SyntaxDatabase { - fn source_file() for ra_db::SourceFileQuery; - } - impl LineIndexDatabase { - fn line_index() for LineIndexQuery; - } - impl symbol_index::SymbolsDatabase { - fn file_symbols() for symbol_index::FileSymbolsQuery; - fn library_symbols() for symbol_index::LibrarySymbolsQuery; - } - impl hir::db::HirDatabase { - fn hir_source_file() for hir::db::HirSourceFileQuery; - fn expand_macro_invocation() for hir::db::ExpandMacroInvocationQuery; - fn module_tree() for hir::db::ModuleTreeQuery; - fn fn_scopes() for hir::db::FnScopesQuery; - fn file_items() for hir::db::FileItemsQuery; - fn file_item() for hir::db::FileItemQuery; - fn lower_module() for hir::db::LowerModuleQuery; - fn lower_module_module() for hir::db::LowerModuleModuleQuery; - fn lower_module_source_map() for hir::db::LowerModuleSourceMapQuery; - fn item_map() for hir::db::ItemMapQuery; - fn submodules() for hir::db::SubmodulesQuery; - fn infer() for hir::db::InferQuery; - fn type_for_def() for hir::db::TypeForDefQuery; - fn type_for_field() for hir::db::TypeForFieldQuery; - fn struct_data() for hir::db::StructDataQuery; - fn enum_data() for hir::db::EnumDataQuery; - fn impls_in_module() for hir::db::ImplsInModuleQuery; - fn impls_in_crate() for hir::db::ImplsInCrateQuery; - fn body_hir() for hir::db::BodyHirQuery; - fn body_syntax_mapping() for hir::db::BodySyntaxMappingQuery; - fn fn_signature() for hir::db::FnSignatureQuery; - fn generic_params() for hir::db::GenericParamsQuery; - } - } -} diff --git a/crates/ra_ide_api/src/status.rs b/crates/ra_ide_api/src/status.rs index 5c14cbdeb..d499dd878 100644 --- a/crates/ra_ide_api/src/status.rs +++ b/crates/ra_ide_api/src/status.rs @@ -6,7 +6,7 @@ use ra_db::{ use crate::db::RootDatabase; pub(crate) fn status(db: &RootDatabase) -> String { - let n_parsed_files = db.query(SourceFileQuery).keys::>().len(); + let n_parsed_files = db.query(SourceFileQuery).entries::>().len(); let n_defs = { let interner: &hir::HirInterner = db.as_ref(); interner.len() -- cgit v1.2.3