From 9266c18ce61daa53481db67e982acf25fd0452e3 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Fri, 5 Jul 2019 17:45:57 +0300 Subject: switch from volatile to untracked read --- crates/ra_hir/src/db.rs | 9 ++++--- crates/ra_hir/src/ty/traits.rs | 54 +++++++++++++++++++++++++++++------------- 2 files changed, 41 insertions(+), 22 deletions(-) (limited to 'crates') diff --git a/crates/ra_hir/src/db.rs b/crates/ra_hir/src/db.rs index 358365176..7b7974f5b 100644 --- a/crates/ra_hir/src/db.rs +++ b/crates/ra_hir/src/db.rs @@ -1,6 +1,5 @@ use std::sync::Arc; -use parking_lot::Mutex; use ra_db::{salsa, SourceDatabase}; use ra_syntax::{ast, Parse, SmolStr, SyntaxNode}; @@ -147,6 +146,7 @@ pub trait DefDatabase: InternDatabase { } #[salsa::query_group(HirDatabaseStorage)] +#[salsa::requires(salsa::Database)] pub trait HirDatabase: DefDatabase + AstDatabase { #[salsa::invoke(ExprScopes::expr_scopes_query)] fn expr_scopes(&self, def: DefWithBody) -> Arc; @@ -187,11 +187,10 @@ pub trait HirDatabase: DefDatabase + AstDatabase { /// This provides the Chalk trait solver instance. Because Chalk always /// works from a specific crate, this query is keyed on the crate; and /// because Chalk does its own internal caching, the solver is wrapped in a - /// Mutex and the query is marked volatile, to make sure the cached state is - /// thrown away when input facts change. + /// Mutex and the query does an untracked read internally, to make sure the + /// cached state is thrown away when input facts change. #[salsa::invoke(crate::ty::traits::trait_solver_query)] - #[salsa::volatile] - fn trait_solver(&self, krate: Crate) -> Arc>; + fn trait_solver(&self, krate: Crate) -> crate::ty::traits::TraitSolver; #[salsa::invoke(crate::ty::traits::chalk::associated_ty_data_query)] fn associated_ty_data(&self, id: chalk_ir::TypeId) -> Arc; diff --git a/crates/ra_hir/src/ty/traits.rs b/crates/ra_hir/src/ty/traits.rs index fde5d8a47..5406fb4a6 100644 --- a/crates/ra_hir/src/ty/traits.rs +++ b/crates/ra_hir/src/ty/traits.rs @@ -4,6 +4,7 @@ use std::sync::Arc; use chalk_ir::cast::Cast; use log::debug; use parking_lot::Mutex; +use ra_db::salsa; use ra_prof::profile; use rustc_hash::FxHashSet; @@ -14,7 +15,34 @@ use self::chalk::{from_chalk, ToChalk}; pub(crate) mod chalk; -pub(crate) type Solver = chalk_solve::Solver; +#[derive(Debug, Clone)] +pub struct TraitSolver { + krate: Crate, + inner: Arc>, +} + +/// We need eq for salsa +impl PartialEq for TraitSolver { + fn eq(&self, other: &TraitSolver) -> bool { + Arc::ptr_eq(&self.inner, &other.inner) + } +} + +impl Eq for TraitSolver {} + +impl TraitSolver { + fn solve( + &self, + db: &impl HirDatabase, + goal: &chalk_ir::UCanonical>, + ) -> Option { + let context = ChalkContext { db, krate: self.krate }; + debug!("solve goal: {:?}", goal); + let solution = self.inner.lock().solve_with_fuel(&context, goal, Some(1000)); + debug!("solve({:?}) => {:?}", goal, solution); + solution + } +} /// This controls the maximum size of types Chalk considers. If we set this too /// high, we can run into slow edge cases; if we set it too low, Chalk won't @@ -27,11 +55,15 @@ struct ChalkContext<'a, DB> { krate: Crate, } -pub(crate) fn trait_solver_query(_db: &impl HirDatabase, _krate: Crate) -> Arc> { +pub(crate) fn trait_solver_query( + db: &(impl HirDatabase + salsa::Database), + krate: Crate, +) -> TraitSolver { + db.salsa_runtime().report_untracked_read(); // krate parameter is just so we cache a unique solver per crate let solver_choice = chalk_solve::SolverChoice::SLG { max_size: CHALK_SOLVER_MAX_SIZE }; - debug!("Creating new solver for crate {:?}", _krate); - Arc::new(Mutex::new(solver_choice.into_solver())) + debug!("Creating new solver for crate {:?}", krate); + TraitSolver { krate, inner: Arc::new(Mutex::new(solver_choice.into_solver())) } } /// Collects impls for the given trait in the whole dependency tree of `krate`. @@ -54,18 +86,6 @@ pub(crate) fn impls_for_trait_query( impls.into_iter().collect::>().into() } -fn solve( - db: &impl HirDatabase, - krate: Crate, - goal: &chalk_ir::UCanonical>, -) -> Option { - let context = ChalkContext { db, krate }; - let solver = db.trait_solver(krate); - let solution = solver.lock().solve(&context, goal); - debug!("solve({:?}) => {:?}", goal, solution); - solution -} - /// A set of clauses that we assume to be true. E.g. if we are inside this function: /// ```rust /// fn foo(t: T) {} @@ -127,7 +147,7 @@ pub(crate) fn trait_solve_query( // We currently don't deal with universes (I think / hope they're not yet // relevant for our use cases?) let u_canonical = chalk_ir::UCanonical { canonical, universes: 1 }; - let solution = solve(db, krate, &u_canonical); + let solution = db.trait_solver(krate).solve(db, &u_canonical); solution.map(|solution| solution_from_chalk(db, solution)) } -- cgit v1.2.3 From 343463c824f4672f19be08f4786d3eeb2e7dea9f Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Wed, 26 Jun 2019 09:12:46 +0300 Subject: implement durability --- crates/ra_cli/src/analysis_bench.rs | 38 +++++++++++++++++++++----- crates/ra_db/Cargo.toml | 2 +- crates/ra_hir/src/lib.rs | 2 ++ crates/ra_hir/src/ty/traits.rs | 2 +- crates/ra_ide_api/src/change.rs | 53 +++++++++++++++++++++++++++---------- crates/ra_ide_api/src/db.rs | 8 +++--- crates/ra_ide_api/src/lib.rs | 3 +++ crates/ra_lsp_server/src/lib.rs | 1 + 8 files changed, 82 insertions(+), 27 deletions(-) (limited to 'crates') diff --git a/crates/ra_cli/src/analysis_bench.rs b/crates/ra_cli/src/analysis_bench.rs index 5e9d0c16d..9e76bcebf 100644 --- a/crates/ra_cli/src/analysis_bench.rs +++ b/crates/ra_cli/src/analysis_bench.rs @@ -1,10 +1,14 @@ use std::{ path::{Path, PathBuf}, + sync::Arc, time::Instant, }; -use ra_db::{salsa::Database, SourceDatabase}; -use ra_ide_api::{Analysis, AnalysisHost, FilePosition, LineCol}; +use ra_db::{ + salsa::{Database, Durability}, + FileId, SourceDatabase, +}; +use ra_ide_api::{Analysis, AnalysisChange, AnalysisHost, FilePosition, LineCol}; use crate::Result; @@ -16,7 +20,7 @@ pub(crate) enum Op { pub(crate) fn run(verbose: bool, path: &Path, op: Op) -> Result<()> { let start = Instant::now(); eprint!("loading: "); - let (host, roots) = ra_batch::load_cargo(path)?; + let (mut host, roots) = ra_batch::load_cargo(path)?; let db = host.raw_database(); eprintln!("{:?}\n", start.elapsed()); @@ -44,7 +48,7 @@ pub(crate) fn run(verbose: bool, path: &Path, op: Op) -> Result<()> { match op { Op::Highlight { .. } => { - let res = do_work(&host, |analysis| { + let res = do_work(&mut host, file_id, |analysis| { analysis.diagnostics(file_id).unwrap(); analysis.highlight_as_html(file_id, false).unwrap() }); @@ -59,7 +63,7 @@ pub(crate) fn run(verbose: bool, path: &Path, op: Op) -> Result<()> { .offset(LineCol { line, col_utf16: column }); let file_postion = FilePosition { file_id, offset }; - let res = do_work(&host, |analysis| analysis.completions(file_postion)); + let res = do_work(&mut host, file_id, |analysis| analysis.completions(file_postion)); if verbose { println!("\n{:#?}", res); } @@ -68,7 +72,7 @@ pub(crate) fn run(verbose: bool, path: &Path, op: Op) -> Result<()> { Ok(()) } -fn do_work T, T>(host: &AnalysisHost, work: F) -> T { +fn do_work T, T>(host: &mut AnalysisHost, file_id: FileId, work: F) -> T { { let start = Instant::now(); eprint!("from scratch: "); @@ -84,7 +88,27 @@ fn do_work T, T>(host: &AnalysisHost, work: F) -> T { { let start = Instant::now(); eprint!("trivial change: "); - host.raw_database().salsa_runtime().next_revision(); + host.raw_database().salsa_runtime().synthetic_write(Durability::LOW); + work(&host.analysis()); + eprintln!("{:?}", start.elapsed()); + } + { + let start = Instant::now(); + eprint!("comment change: "); + { + let mut text = host.analysis().file_text(file_id).unwrap().to_string(); + text.push_str("\n/* Hello world */\n"); + let mut change = AnalysisChange::new(); + change.change_file(file_id, Arc::new(text)); + host.apply_change(change); + } + work(&host.analysis()); + eprintln!("{:?}", start.elapsed()); + } + { + let start = Instant::now(); + eprint!("const change: "); + host.raw_database().salsa_runtime().synthetic_write(Durability::HIGH); let res = work(&host.analysis()); eprintln!("{:?}", start.elapsed()); res diff --git a/crates/ra_db/Cargo.toml b/crates/ra_db/Cargo.toml index 7a13c247b..2fac07bc5 100644 --- a/crates/ra_db/Cargo.toml +++ b/crates/ra_db/Cargo.toml @@ -5,7 +5,7 @@ version = "0.1.0" authors = ["rust-analyzer developers"] [dependencies] -salsa = "0.12.3" +salsa = "0.13.0" relative-path = "0.4.0" rustc-hash = "1.0" diff --git a/crates/ra_hir/src/lib.rs b/crates/ra_hir/src/lib.rs index 081974e2b..018fcd096 100644 --- a/crates/ra_hir/src/lib.rs +++ b/crates/ra_hir/src/lib.rs @@ -1,3 +1,5 @@ +#![recursion_limit = "512"] + //! HIR (previously known as descriptors) provides a high-level object oriented //! access to Rust code. //! diff --git a/crates/ra_hir/src/ty/traits.rs b/crates/ra_hir/src/ty/traits.rs index 5406fb4a6..b634f0b79 100644 --- a/crates/ra_hir/src/ty/traits.rs +++ b/crates/ra_hir/src/ty/traits.rs @@ -38,7 +38,7 @@ impl TraitSolver { ) -> Option { let context = ChalkContext { db, krate: self.krate }; debug!("solve goal: {:?}", goal); - let solution = self.inner.lock().solve_with_fuel(&context, goal, Some(1000)); + let solution = self.inner.lock().solve(&context, goal); debug!("solve({:?}) => {:?}", goal, solution); solution } diff --git a/crates/ra_ide_api/src/change.rs b/crates/ra_ide_api/src/change.rs index 147d2b21d..0234c572d 100644 --- a/crates/ra_ide_api/src/change.rs +++ b/crates/ra_ide_api/src/change.rs @@ -1,7 +1,7 @@ use std::{fmt, sync::Arc, time}; use ra_db::{ - salsa::{Database, SweepStrategy}, + salsa::{Database, Durability, SweepStrategy}, CrateGraph, FileId, SourceDatabase, SourceRoot, SourceRootId, }; use ra_prof::{memory_usage, profile, Bytes}; @@ -155,54 +155,71 @@ impl RootDatabase { log::info!("apply_change {:?}", change); { let _p = profile("RootDatabase::apply_change/cancellation"); - self.salsa_runtime().next_revision(); + self.salsa_runtime().synthetic_write(Durability::LOW); } if !change.new_roots.is_empty() { let mut local_roots = Vec::clone(&self.local_roots()); for (root_id, is_local) in change.new_roots { let root = if is_local { SourceRoot::new() } else { SourceRoot::new_library() }; - self.set_source_root(root_id, Arc::new(root)); + let durability = durability(&root); + self.set_source_root_with_durability(root_id, Arc::new(root), durability); if is_local { local_roots.push(root_id); } } - self.set_local_roots(Arc::new(local_roots)); + self.set_local_roots_with_durability(Arc::new(local_roots), Durability::HIGH); } for (root_id, root_change) in change.roots_changed { self.apply_root_change(root_id, root_change); } for (file_id, text) in change.files_changed { - self.set_file_text(file_id, text) + let source_root_id = self.file_source_root(file_id); + let source_root = self.source_root(source_root_id); + let durability = durability(&source_root); + self.set_file_text_with_durability(file_id, text, durability) } if !change.libraries_added.is_empty() { let mut libraries = Vec::clone(&self.library_roots()); for library in change.libraries_added { libraries.push(library.root_id); - self.set_source_root(library.root_id, Default::default()); - self.set_constant_library_symbols(library.root_id, Arc::new(library.symbol_index)); + self.set_source_root_with_durability( + library.root_id, + Default::default(), + Durability::HIGH, + ); + self.set_library_symbols_with_durability( + library.root_id, + Arc::new(library.symbol_index), + Durability::HIGH, + ); self.apply_root_change(library.root_id, library.root_change); } - self.set_library_roots(Arc::new(libraries)); + self.set_library_roots_with_durability(Arc::new(libraries), Durability::HIGH); } if let Some(crate_graph) = change.crate_graph { - self.set_crate_graph(Arc::new(crate_graph)) + self.set_crate_graph_with_durability(Arc::new(crate_graph), Durability::HIGH) } } fn apply_root_change(&mut self, root_id: SourceRootId, root_change: RootChange) { let mut source_root = SourceRoot::clone(&self.source_root(root_id)); + let durability = durability(&source_root); for add_file in root_change.added { - self.set_file_text(add_file.file_id, add_file.text); - self.set_file_relative_path(add_file.file_id, add_file.path.clone()); - self.set_file_source_root(add_file.file_id, root_id); + self.set_file_text_with_durability(add_file.file_id, add_file.text, durability); + self.set_file_relative_path_with_durability( + add_file.file_id, + add_file.path.clone(), + durability, + ); + self.set_file_source_root_with_durability(add_file.file_id, root_id, durability); source_root.files.insert(add_file.path, add_file.file_id); } for remove_file in root_change.removed { - self.set_file_text(remove_file.file_id, Default::default()); + self.set_file_text_with_durability(remove_file.file_id, Default::default(), durability); source_root.files.remove(&remove_file.path); } - self.set_source_root(root_id, Arc::new(source_root)); + self.set_source_root_with_durability(root_id, Arc::new(source_root), durability); } pub(crate) fn maybe_collect_garbage(&mut self) { @@ -308,3 +325,11 @@ impl RootDatabase { acc } } + +fn durability(source_root: &SourceRoot) -> Durability { + if source_root.is_library { + Durability::HIGH + } else { + Durability::LOW + } +} diff --git a/crates/ra_ide_api/src/db.rs b/crates/ra_ide_api/src/db.rs index 44216b045..fc8252e4b 100644 --- a/crates/ra_ide_api/src/db.rs +++ b/crates/ra_ide_api/src/db.rs @@ -1,7 +1,7 @@ use std::{sync::Arc, time}; use ra_db::{ - salsa::{self, Database}, + salsa::{self, Database, Durability}, Canceled, CheckCanceled, FileId, SourceDatabase, }; @@ -57,9 +57,9 @@ impl RootDatabase { last_gc: time::Instant::now(), last_gc_check: time::Instant::now(), }; - db.set_crate_graph(Default::default()); - db.set_local_roots(Default::default()); - db.set_library_roots(Default::default()); + db.set_crate_graph_with_durability(Default::default(), Durability::HIGH); + db.set_local_roots_with_durability(Default::default(), Durability::HIGH); + db.set_library_roots_with_durability(Default::default(), Durability::HIGH); let lru_capacity = lru_capacity.unwrap_or(ra_db::DEFAULT_LRU_CAP); db.query_mut(ra_db::ParseQuery).set_lru_capacity(lru_capacity); db.query_mut(hir::db::ParseMacroQuery).set_lru_capacity(lru_capacity); diff --git a/crates/ra_ide_api/src/lib.rs b/crates/ra_ide_api/src/lib.rs index edb646c11..fa4ae4379 100644 --- a/crates/ra_ide_api/src/lib.rs +++ b/crates/ra_ide_api/src/lib.rs @@ -281,6 +281,9 @@ impl AnalysisHost { pub fn raw_database(&self) -> &(impl hir::db::HirDatabase + salsa::Database) { &self.db } + pub fn raw_database_mut(&mut self) -> &mut (impl hir::db::HirDatabase + salsa::Database) { + &mut self.db + } } /// Analysis is a snapshot of a world state at a moment in time. It is the main diff --git a/crates/ra_lsp_server/src/lib.rs b/crates/ra_lsp_server/src/lib.rs index 795f86383..ca388e472 100644 --- a/crates/ra_lsp_server/src/lib.rs +++ b/crates/ra_lsp_server/src/lib.rs @@ -1,3 +1,4 @@ +#![recursion_limit = "512"] mod caps; mod cargo_target_spec; mod conv; -- cgit v1.2.3