From 95aa7e46fc69b5279a1af74dfa7008396a4455a5 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Wed, 30 Jan 2019 22:18:17 +0300 Subject: move item_map_query --- crates/ra_hir/src/db.rs | 2 +- crates/ra_hir/src/nameres.rs | 29 ++++++++++++++++++++++++++--- crates/ra_hir/src/query_definitions.rs | 33 ++------------------------------- 3 files changed, 29 insertions(+), 35 deletions(-) diff --git a/crates/ra_hir/src/db.rs b/crates/ra_hir/src/db.rs index 70d9de212..e7c5d1436 100644 --- a/crates/ra_hir/src/db.rs +++ b/crates/ra_hir/src/db.rs @@ -67,7 +67,7 @@ pub trait HirDatabase: SourceDatabase + AsRef { #[salsa::invoke(crate::nameres::lower::LoweredModule::lower_module_source_map_query)] fn lower_module_source_map(&self, module: Module) -> Arc; - #[salsa::invoke(query_definitions::item_map)] + #[salsa::invoke(crate::nameres::ItemMap::item_map_query)] fn item_map(&self, crate_id: CrateId) -> Arc; #[salsa::invoke(crate::module_tree::ModuleTree::module_tree_query)] diff --git a/crates/ra_hir/src/nameres.rs b/crates/ra_hir/src/nameres.rs index 97ce6c946..d06cd5c69 100644 --- a/crates/ra_hir/src/nameres.rs +++ b/crates/ra_hir/src/nameres.rs @@ -16,7 +16,7 @@ //! structure itself is modified. pub(crate) mod lower; -use std::sync::Arc; +use std::{time, sync::Arc}; use ra_db::CrateId; use ra_arena::map::ArenaMap; @@ -156,7 +156,7 @@ impl PerNs { } } -pub(crate) struct Resolver<'a, DB> { +struct Resolver<'a, DB> { db: &'a DB, input: &'a FxHashMap>, krate: CrateId, @@ -169,7 +169,7 @@ impl<'a, DB> Resolver<'a, DB> where DB: HirDatabase, { - pub(crate) fn new( + fn new( db: &'a DB, input: &'a FxHashMap>, krate: CrateId, @@ -331,6 +331,29 @@ enum ReachedFixedPoint { } impl ItemMap { + pub(crate) fn item_map_query(db: &impl HirDatabase, crate_id: CrateId) -> Arc { + let start = time::Instant::now(); + let module_tree = db.module_tree(crate_id); + let input = module_tree + .modules() + .map(|module_id| { + ( + module_id, + db.lower_module_module(Module { + krate: crate_id, + module_id, + }), + ) + }) + .collect::>(); + + let resolver = Resolver::new(db, &input, crate_id); + let res = resolver.resolve(); + let elapsed = start.elapsed(); + log::info!("item_map: {:?}", elapsed); + Arc::new(res) + } + pub(crate) fn resolve_path( &self, db: &impl HirDatabase, diff --git a/crates/ra_hir/src/query_definitions.rs b/crates/ra_hir/src/query_definitions.rs index bf9ac0dfb..6724649e1 100644 --- a/crates/ra_hir/src/query_definitions.rs +++ b/crates/ra_hir/src/query_definitions.rs @@ -1,17 +1,11 @@ -use std::{ - sync::Arc, - time::Instant, -}; +use std::sync::Arc; -use rustc_hash::FxHashMap; use ra_syntax::{SyntaxNode, TreeArc}; -use ra_db::{CrateId}; use crate::{ SourceFileItems, SourceItemId, HirFileId, - Function, FnScopes, Module, + Function, FnScopes, db::HirDatabase, - nameres::{ItemMap, Resolver}, }; pub(super) fn fn_scopes(db: &impl HirDatabase, func: Function) -> Arc { @@ -35,26 +29,3 @@ pub(super) fn file_item( .to_node(&source_file) .to_owned() } - -pub(super) fn item_map(db: &impl HirDatabase, crate_id: CrateId) -> Arc { - let start = Instant::now(); - let module_tree = db.module_tree(crate_id); - let input = module_tree - .modules() - .map(|module_id| { - ( - module_id, - db.lower_module_module(Module { - krate: crate_id, - module_id, - }), - ) - }) - .collect::>(); - - let resolver = Resolver::new(db, &input, crate_id); - let res = resolver.resolve(); - let elapsed = start.elapsed(); - log::info!("item_map: {:?}", elapsed); - Arc::new(res) -} -- cgit v1.2.3 From ef7eb8c2576963a9a2df405feff354d36366b202 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Wed, 30 Jan 2019 22:23:14 +0300 Subject: Use Crate instead of CrateId --- crates/ra_hir/src/code_model_api.rs | 6 +++--- crates/ra_hir/src/code_model_impl/krate.rs | 13 +++++-------- crates/ra_hir/src/code_model_impl/module.rs | 6 +----- crates/ra_hir/src/db.rs | 6 +++--- crates/ra_hir/src/module_tree.rs | 11 ++++++----- crates/ra_hir/src/nameres.rs | 18 +++++++----------- crates/ra_hir/src/nameres/tests.rs | 14 +++++++------- crates/ra_hir/src/source_binder.rs | 5 +++-- crates/ra_hir/src/ty/method_resolution.rs | 2 +- 9 files changed, 36 insertions(+), 45 deletions(-) diff --git a/crates/ra_hir/src/code_model_api.rs b/crates/ra_hir/src/code_model_api.rs index 53e84429c..c59b7fbc5 100644 --- a/crates/ra_hir/src/code_model_api.rs +++ b/crates/ra_hir/src/code_model_api.rs @@ -52,7 +52,7 @@ pub enum Def { #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] pub struct Module { - pub(crate) krate: CrateId, + pub(crate) krate: Crate, pub(crate) module_id: ModuleId, } @@ -133,8 +133,8 @@ impl Module { } /// Returns the crate this module is part of. - pub fn krate(&self, db: &impl HirDatabase) -> Option { - self.krate_impl(db) + pub fn krate(&self, _db: &impl HirDatabase) -> Option { + Some(self.krate) } /// Topmost parent of this module. Every module has a `crate_root`, but some diff --git a/crates/ra_hir/src/code_model_impl/krate.rs b/crates/ra_hir/src/code_model_impl/krate.rs index cdd30b402..86f29d959 100644 --- a/crates/ra_hir/src/code_model_impl/krate.rs +++ b/crates/ra_hir/src/code_model_impl/krate.rs @@ -1,31 +1,28 @@ -use ra_db::CrateId; - use crate::{ Crate, CrateDependency, AsName, Module, db::HirDatabase, }; impl Crate { - pub(crate) fn new(crate_id: CrateId) -> Crate { - Crate { crate_id } - } pub(crate) fn dependencies_impl(&self, db: &impl HirDatabase) -> Vec { let crate_graph = db.crate_graph(); crate_graph .dependencies(self.crate_id) .map(|dep| { - let krate = Crate::new(dep.crate_id()); + let krate = Crate { + crate_id: dep.crate_id(), + }; let name = dep.as_name(); CrateDependency { krate, name } }) .collect() } pub(crate) fn root_module_impl(&self, db: &impl HirDatabase) -> Option { - let module_tree = db.module_tree(self.crate_id); + let module_tree = db.module_tree(*self); let module_id = module_tree.modules().next()?; let module = Module { - krate: self.crate_id, + krate: *self, module_id, }; Some(module) diff --git a/crates/ra_hir/src/code_model_impl/module.rs b/crates/ra_hir/src/code_model_impl/module.rs index c6f85ac82..4a3901b8b 100644 --- a/crates/ra_hir/src/code_model_impl/module.rs +++ b/crates/ra_hir/src/code_model_impl/module.rs @@ -3,7 +3,7 @@ use ra_syntax::{ast, SyntaxNode, TreeArc}; use crate::{ Module, ModuleSource, Problem, - Crate, Name, + Name, module_tree::ModuleId, impl_block::ImplId, nameres::{lower::ImportId}, @@ -67,10 +67,6 @@ impl Module { source_map.get(&source, impl_id) } - pub(crate) fn krate_impl(&self, _db: &impl HirDatabase) -> Option { - Some(Crate::new(self.krate)) - } - pub(crate) fn crate_root_impl(&self, db: &impl HirDatabase) -> Module { let module_tree = db.module_tree(self.krate); let module_id = self.module_id.crate_root(&module_tree); diff --git a/crates/ra_hir/src/db.rs b/crates/ra_hir/src/db.rs index e7c5d1436..16d5a7877 100644 --- a/crates/ra_hir/src/db.rs +++ b/crates/ra_hir/src/db.rs @@ -1,7 +1,7 @@ use std::sync::Arc; use ra_syntax::{SyntaxNode, TreeArc, SourceFile}; -use ra_db::{SourceDatabase, CrateId, salsa}; +use ra_db::{SourceDatabase, salsa}; use crate::{ MacroCallId, HirFileId, @@ -68,10 +68,10 @@ pub trait HirDatabase: SourceDatabase + AsRef { fn lower_module_source_map(&self, module: Module) -> Arc; #[salsa::invoke(crate::nameres::ItemMap::item_map_query)] - fn item_map(&self, crate_id: CrateId) -> Arc; + fn item_map(&self, krate: Crate) -> Arc; #[salsa::invoke(crate::module_tree::ModuleTree::module_tree_query)] - fn module_tree(&self, crate_id: CrateId) -> Arc; + fn module_tree(&self, krate: Crate) -> Arc; #[salsa::invoke(crate::impl_block::impls_in_module_with_source_map_query)] fn impls_in_module_with_source_map( diff --git a/crates/ra_hir/src/module_tree.rs b/crates/ra_hir/src/module_tree.rs index 893c375b5..1f327eeb2 100644 --- a/crates/ra_hir/src/module_tree.rs +++ b/crates/ra_hir/src/module_tree.rs @@ -2,7 +2,7 @@ use std::sync::Arc; use arrayvec::ArrayVec; use relative_path::RelativePathBuf; -use ra_db::{FileId, SourceRoot, CrateId}; +use ra_db::{FileId, SourceRoot}; use ra_syntax::{ SyntaxNode, TreeArc, algo::generate, @@ -13,6 +13,7 @@ use test_utils::tested_by; use crate::{ Name, AsName, HirDatabase, SourceItemId, HirFileId, Problem, SourceFileItems, ModuleSource, + Crate, ids::SourceFileItemId, }; @@ -132,10 +133,10 @@ struct LinkData { } impl ModuleTree { - pub(crate) fn module_tree_query(db: &impl HirDatabase, crate_id: CrateId) -> Arc { + pub(crate) fn module_tree_query(db: &impl HirDatabase, krate: Crate) -> Arc { db.check_canceled(); let mut res = ModuleTree::default(); - res.init_crate(db, crate_id); + res.init_crate(db, krate); Arc::new(res) } @@ -155,9 +156,9 @@ impl ModuleTree { Some(res) } - fn init_crate(&mut self, db: &impl HirDatabase, crate_id: CrateId) { + fn init_crate(&mut self, db: &impl HirDatabase, krate: Crate) { let crate_graph = db.crate_graph(); - let file_id = crate_graph.crate_root(crate_id); + let file_id = crate_graph.crate_root(krate.crate_id); let source_root_id = db.file_source_root(file_id); let source_root = db.source_root(source_root_id); diff --git a/crates/ra_hir/src/nameres.rs b/crates/ra_hir/src/nameres.rs index d06cd5c69..4573a72ba 100644 --- a/crates/ra_hir/src/nameres.rs +++ b/crates/ra_hir/src/nameres.rs @@ -18,7 +18,6 @@ pub(crate) mod lower; use std::{time, sync::Arc}; -use ra_db::CrateId; use ra_arena::map::ArenaMap; use test_utils::tested_by; use rustc_hash::{FxHashMap, FxHashSet}; @@ -159,7 +158,7 @@ impl PerNs { struct Resolver<'a, DB> { db: &'a DB, input: &'a FxHashMap>, - krate: CrateId, + krate: Crate, module_tree: Arc, processed_imports: FxHashSet<(ModuleId, ImportId)>, result: ItemMap, @@ -172,7 +171,7 @@ where fn new( db: &'a DB, input: &'a FxHashMap>, - krate: CrateId, + krate: Crate, ) -> Resolver<'a, DB> { let module_tree = db.module_tree(krate); Resolver { @@ -219,7 +218,7 @@ where let crate_graph = self.db.crate_graph(); if let Some(crate_id) = crate_graph.crate_id_for_crate_root(file_id.as_original_file()) { - let krate = Crate::new(crate_id); + let krate = Crate { crate_id }; for dep in krate.dependencies(self.db) { if let Some(module) = dep.krate.root_module(self.db) { let def = module.into(); @@ -331,23 +330,20 @@ enum ReachedFixedPoint { } impl ItemMap { - pub(crate) fn item_map_query(db: &impl HirDatabase, crate_id: CrateId) -> Arc { + pub(crate) fn item_map_query(db: &impl HirDatabase, krate: Crate) -> Arc { let start = time::Instant::now(); - let module_tree = db.module_tree(crate_id); + let module_tree = db.module_tree(krate); let input = module_tree .modules() .map(|module_id| { ( module_id, - db.lower_module_module(Module { - krate: crate_id, - module_id, - }), + db.lower_module_module(Module { krate, module_id }), ) }) .collect::>(); - let resolver = Resolver::new(db, &input, crate_id); + let resolver = Resolver::new(db, &input, krate); let res = resolver.resolve(); let elapsed = start.elapsed(); log::info!("item_map: {:?}", elapsed); diff --git a/crates/ra_hir/src/nameres/tests.rs b/crates/ra_hir/src/nameres/tests.rs index 3d420467c..9c5ca097e 100644 --- a/crates/ra_hir/src/nameres/tests.rs +++ b/crates/ra_hir/src/nameres/tests.rs @@ -16,7 +16,7 @@ fn item_map(fixture: &str) -> (Arc, ModuleId) { let module = crate::source_binder::module_from_position(&db, pos).unwrap(); let krate = module.krate(&db).unwrap(); let module_id = module.module_id; - (db.item_map(krate.crate_id), module_id) + (db.item_map(krate), module_id) } /// Sets the crate root to the file of the cursor marker @@ -30,7 +30,7 @@ fn item_map_custom_crate_root(fixture: &str) -> (Arc, ModuleId) { let module = crate::source_binder::module_from_position(&db, pos).unwrap(); let krate = module.krate(&db).unwrap(); let module_id = module.module_id; - (db.item_map(krate.crate_id), module_id) + (db.item_map(krate), module_id) } fn check_module_item_map(map: &ItemMap, module_id: ModuleId, expected: &str) { @@ -297,7 +297,7 @@ fn item_map_across_crates() { let module = crate::source_binder::module_from_file_id(&db, main_id).unwrap(); let krate = module.krate(&db).unwrap(); - let item_map = db.item_map(krate.crate_id); + let item_map = db.item_map(krate); check_module_item_map( &item_map, @@ -349,7 +349,7 @@ fn import_across_source_roots() { let module = crate::source_binder::module_from_file_id(&db, main_id).unwrap(); let krate = module.krate(&db).unwrap(); - let item_map = db.item_map(krate.crate_id); + let item_map = db.item_map(krate); check_module_item_map( &item_map, @@ -391,7 +391,7 @@ fn reexport_across_crates() { let module = crate::source_binder::module_from_file_id(&db, main_id).unwrap(); let krate = module.krate(&db).unwrap(); - let item_map = db.item_map(krate.crate_id); + let item_map = db.item_map(krate); check_module_item_map( &item_map, @@ -409,7 +409,7 @@ fn check_item_map_is_not_recomputed(initial: &str, file_change: &str) { let krate = module.krate(&db).unwrap(); { let events = db.log_executed(|| { - db.item_map(krate.crate_id); + db.item_map(krate); }); assert!(format!("{:?}", events).contains("item_map")) } @@ -417,7 +417,7 @@ fn check_item_map_is_not_recomputed(initial: &str, file_change: &str) { { let events = db.log_executed(|| { - db.item_map(krate.crate_id); + db.item_map(krate); }); assert!( !format!("{:?}", events).contains("item_map"), diff --git a/crates/ra_hir/src/source_binder.rs b/crates/ra_hir/src/source_binder.rs index 589efd023..d1eaccf23 100644 --- a/crates/ra_hir/src/source_binder.rs +++ b/crates/ra_hir/src/source_binder.rs @@ -14,7 +14,7 @@ use ra_syntax::{ use crate::{ HirDatabase, Function, ModuleDef, Struct, Enum, - AsName, Module, HirFileId, + AsName, Module, HirFileId, Crate, ids::{LocationCtx, SourceFileItemId}, }; @@ -83,7 +83,8 @@ fn module_from_source( let source_root_id = db.file_source_root(file_id.as_original_file()); db.source_root_crates(source_root_id) .iter() - .find_map(|&krate| { + .map(|&crate_id| Crate { crate_id }) + .find_map(|krate| { let module_tree = db.module_tree(krate); let module_id = module_tree.find_module_by_source(file_id, decl_id)?; Some(Module { krate, module_id }) diff --git a/crates/ra_hir/src/ty/method_resolution.rs b/crates/ra_hir/src/ty/method_resolution.rs index d70a24582..37bc3f38c 100644 --- a/crates/ra_hir/src/ty/method_resolution.rs +++ b/crates/ra_hir/src/ty/method_resolution.rs @@ -52,7 +52,7 @@ impl CrateImplBlocks { .flat_map(|i| i.iter()) .map(move |(module_id, impl_id)| { let module = Module { - krate: self.krate.crate_id, + krate: self.krate, module_id: *module_id, }; let module_impl_blocks = db.impls_in_module(module); -- cgit v1.2.3