Skip to content

Commit bdd9b11

Browse files
committed
implement durability
1 parent d35251c commit bdd9b11

File tree

10 files changed

+117
-49
lines changed

10 files changed

+117
-49
lines changed

Cargo.lock

Lines changed: 34 additions & 9 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,4 @@ incremental = true
66
debug = 1 # only line info
77

88
[patch.'crates-io']
9+
salsa = { git = "https://github.com/nikomatsakis/salsa", branch = "durability" }

crates/ra_cli/src/analysis_bench.rs

Lines changed: 31 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
11
use std::{
22
path::{Path, PathBuf},
3+
sync::Arc,
34
time::Instant,
45
};
56

6-
use ra_db::{salsa::Database, SourceDatabase};
7-
use ra_ide_api::{Analysis, AnalysisHost, FilePosition, LineCol};
7+
use ra_db::{
8+
salsa::{Database, Durability},
9+
FileId, SourceDatabase,
10+
};
11+
use ra_ide_api::{Analysis, AnalysisChange, AnalysisHost, FilePosition, LineCol};
812

913
use crate::Result;
1014

@@ -16,7 +20,7 @@ pub(crate) enum Op {
1620
pub(crate) fn run(verbose: bool, path: &Path, op: Op) -> Result<()> {
1721
let start = Instant::now();
1822
eprint!("loading: ");
19-
let (host, roots) = ra_batch::load_cargo(path)?;
23+
let (mut host, roots) = ra_batch::load_cargo(path)?;
2024
let db = host.raw_database();
2125
eprintln!("{:?}\n", start.elapsed());
2226

@@ -44,7 +48,7 @@ pub(crate) fn run(verbose: bool, path: &Path, op: Op) -> Result<()> {
4448

4549
match op {
4650
Op::Highlight { .. } => {
47-
let res = do_work(&host, |analysis| {
51+
let res = do_work(&mut host, file_id, |analysis| {
4852
analysis.diagnostics(file_id).unwrap();
4953
analysis.highlight_as_html(file_id, false).unwrap()
5054
});
@@ -59,7 +63,7 @@ pub(crate) fn run(verbose: bool, path: &Path, op: Op) -> Result<()> {
5963
.offset(LineCol { line, col_utf16: column });
6064
let file_postion = FilePosition { file_id, offset };
6165

62-
let res = do_work(&host, |analysis| analysis.completions(file_postion));
66+
let res = do_work(&mut host, file_id, |analysis| analysis.completions(file_postion));
6367
if verbose {
6468
println!("\n{:#?}", res);
6569
}
@@ -68,7 +72,7 @@ pub(crate) fn run(verbose: bool, path: &Path, op: Op) -> Result<()> {
6872
Ok(())
6973
}
7074

71-
fn do_work<F: Fn(&Analysis) -> T, T>(host: &AnalysisHost, work: F) -> T {
75+
fn do_work<F: Fn(&Analysis) -> T, T>(host: &mut AnalysisHost, file_id: FileId, work: F) -> T {
7276
{
7377
let start = Instant::now();
7478
eprint!("from scratch: ");
@@ -84,7 +88,27 @@ fn do_work<F: Fn(&Analysis) -> T, T>(host: &AnalysisHost, work: F) -> T {
8488
{
8589
let start = Instant::now();
8690
eprint!("trivial change: ");
87-
host.raw_database().salsa_runtime().next_revision();
91+
host.raw_database().salsa_runtime().synthetic_write(Durability::LOW);
92+
work(&host.analysis());
93+
eprintln!("{:?}", start.elapsed());
94+
}
95+
{
96+
let start = Instant::now();
97+
eprint!("comment change: ");
98+
{
99+
let mut text = host.analysis().file_text(file_id).to_string();
100+
text.push_str("\n/* Hello world */\n");
101+
let mut change = AnalysisChange::new();
102+
change.change_file(file_id, Arc::new(text));
103+
host.apply_change(change);
104+
}
105+
work(&host.analysis());
106+
eprintln!("{:?}", start.elapsed());
107+
}
108+
{
109+
let start = Instant::now();
110+
eprint!("const change: ");
111+
host.raw_database().salsa_runtime().synthetic_write(Durability::HIGH);
88112
let res = work(&host.analysis());
89113
eprintln!("{:?}", start.elapsed());
90114
res

crates/ra_db/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ version = "0.1.0"
55
authors = ["rust-analyzer developers"]
66

77
[dependencies]
8-
salsa = "0.12.3"
8+
salsa = "0.13.0"
99
relative-path = "0.4.0"
1010
rustc-hash = "1.0"
1111

crates/ra_hir/src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
#![recursion_limit = "512"]
2+
13
//! HIR (previously known as descriptors) provides a high-level object oriented
24
//! access to Rust code.
35
//!

crates/ra_hir/src/ty/traits.rs

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ impl ChalkSolver {
3737
) -> Option<chalk_solve::Solution> {
3838
let context = ChalkContext { db, krate: self.krate };
3939
debug!("solve goal: {:?}", goal);
40-
let solution = self.inner.lock().solve_with_fuel(&context, goal, Some(1000));
40+
let solution = self.inner.lock().solve(&context, goal);
4141
debug!("solve({:?}) => {:?}", goal, solution);
4242
solution
4343
}
@@ -85,19 +85,6 @@ pub(crate) fn impls_for_trait_query(
8585
impls.into_iter().collect::<Vec<_>>().into()
8686
}
8787

88-
fn solve(
89-
db: &impl HirDatabase,
90-
krate: Crate,
91-
goal: &chalk_ir::UCanonical<chalk_ir::InEnvironment<chalk_ir::Goal>>,
92-
) -> Option<chalk_solve::Solution> {
93-
let context = ChalkContext { db, krate };
94-
let solver = db.trait_solver(krate);
95-
debug!("solve goal: {:?}", goal);
96-
let solution = solver.lock().solve(&context, goal);
97-
debug!("solve({:?}) => {:?}", goal, solution);
98-
solution
99-
}
100-
10188
/// A set of clauses that we assume to be true. E.g. if we are inside this function:
10289
/// ```rust
10390
/// fn foo<T: Default>(t: T) {}

crates/ra_ide_api/src/change.rs

Lines changed: 39 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use std::{fmt, sync::Arc, time};
22

33
use ra_db::{
4-
salsa::{Database, SweepStrategy},
4+
salsa::{Database, Durability, SweepStrategy},
55
CrateGraph, FileId, SourceDatabase, SourceRoot, SourceRootId,
66
};
77
use ra_prof::{memory_usage, profile, Bytes};
@@ -155,54 +155,71 @@ impl RootDatabase {
155155
log::info!("apply_change {:?}", change);
156156
{
157157
let _p = profile("RootDatabase::apply_change/cancellation");
158-
self.salsa_runtime().next_revision();
158+
self.salsa_runtime().synthetic_write(Durability::LOW);
159159
}
160160
if !change.new_roots.is_empty() {
161161
let mut local_roots = Vec::clone(&self.local_roots());
162162
for (root_id, is_local) in change.new_roots {
163163
let root = if is_local { SourceRoot::new() } else { SourceRoot::new_library() };
164-
self.set_source_root(root_id, Arc::new(root));
164+
let durability = durability(&root);
165+
self.set_source_root_with_durability(root_id, Arc::new(root), durability);
165166
if is_local {
166167
local_roots.push(root_id);
167168
}
168169
}
169-
self.set_local_roots(Arc::new(local_roots));
170+
self.set_local_roots_with_durability(Arc::new(local_roots), Durability::HIGH);
170171
}
171172

172173
for (root_id, root_change) in change.roots_changed {
173174
self.apply_root_change(root_id, root_change);
174175
}
175176
for (file_id, text) in change.files_changed {
176-
self.set_file_text(file_id, text)
177+
let source_root_id = self.file_source_root(file_id);
178+
let source_root = self.source_root(source_root_id);
179+
let durability = durability(&source_root);
180+
self.set_file_text_with_durability(file_id, text, durability)
177181
}
178182
if !change.libraries_added.is_empty() {
179183
let mut libraries = Vec::clone(&self.library_roots());
180184
for library in change.libraries_added {
181185
libraries.push(library.root_id);
182-
self.set_source_root(library.root_id, Default::default());
183-
self.set_constant_library_symbols(library.root_id, Arc::new(library.symbol_index));
186+
self.set_source_root_with_durability(
187+
library.root_id,
188+
Default::default(),
189+
Durability::HIGH,
190+
);
191+
self.set_library_symbols_with_durability(
192+
library.root_id,
193+
Arc::new(library.symbol_index),
194+
Durability::HIGH,
195+
);
184196
self.apply_root_change(library.root_id, library.root_change);
185197
}
186-
self.set_library_roots(Arc::new(libraries));
198+
self.set_library_roots_with_durability(Arc::new(libraries), Durability::HIGH);
187199
}
188200
if let Some(crate_graph) = change.crate_graph {
189-
self.set_crate_graph(Arc::new(crate_graph))
201+
self.set_crate_graph_with_durability(Arc::new(crate_graph), Durability::HIGH)
190202
}
191203
}
192204

193205
fn apply_root_change(&mut self, root_id: SourceRootId, root_change: RootChange) {
194206
let mut source_root = SourceRoot::clone(&self.source_root(root_id));
207+
let durability = durability(&source_root);
195208
for add_file in root_change.added {
196-
self.set_file_text(add_file.file_id, add_file.text);
197-
self.set_file_relative_path(add_file.file_id, add_file.path.clone());
198-
self.set_file_source_root(add_file.file_id, root_id);
209+
self.set_file_text_with_durability(add_file.file_id, add_file.text, durability);
210+
self.set_file_relative_path_with_durability(
211+
add_file.file_id,
212+
add_file.path.clone(),
213+
durability,
214+
);
215+
self.set_file_source_root_with_durability(add_file.file_id, root_id, durability);
199216
source_root.files.insert(add_file.path, add_file.file_id);
200217
}
201218
for remove_file in root_change.removed {
202-
self.set_file_text(remove_file.file_id, Default::default());
219+
self.set_file_text_with_durability(remove_file.file_id, Default::default(), durability);
203220
source_root.files.remove(&remove_file.path);
204221
}
205-
self.set_source_root(root_id, Arc::new(source_root));
222+
self.set_source_root_with_durability(root_id, Arc::new(source_root), durability);
206223
}
207224

208225
pub(crate) fn maybe_collect_garbage(&mut self) {
@@ -308,3 +325,11 @@ impl RootDatabase {
308325
acc
309326
}
310327
}
328+
329+
fn durability(source_root: &SourceRoot) -> Durability {
330+
if source_root.is_library {
331+
Durability::HIGH
332+
} else {
333+
Durability::LOW
334+
}
335+
}

crates/ra_ide_api/src/db.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use std::{sync::Arc, time};
22

33
use ra_db::{
4-
salsa::{self, Database},
4+
salsa::{self, Database, Durability},
55
Canceled, CheckCanceled, FileId, SourceDatabase,
66
};
77

@@ -57,9 +57,9 @@ impl RootDatabase {
5757
last_gc: time::Instant::now(),
5858
last_gc_check: time::Instant::now(),
5959
};
60-
db.set_crate_graph(Default::default());
61-
db.set_local_roots(Default::default());
62-
db.set_library_roots(Default::default());
60+
db.set_crate_graph_with_durability(Default::default(), Durability::HIGH);
61+
db.set_local_roots_with_durability(Default::default(), Durability::HIGH);
62+
db.set_library_roots_with_durability(Default::default(), Durability::HIGH);
6363
let lru_capacity = lru_capacity.unwrap_or(ra_db::DEFAULT_LRU_CAP);
6464
db.query_mut(ra_db::ParseQuery).set_lru_capacity(lru_capacity);
6565
db.query_mut(hir::db::ParseMacroQuery).set_lru_capacity(lru_capacity);

crates/ra_ide_api/src/lib.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -281,6 +281,9 @@ impl AnalysisHost {
281281
pub fn raw_database(&self) -> &(impl hir::db::HirDatabase + salsa::Database) {
282282
&self.db
283283
}
284+
pub fn raw_database_mut(&mut self) -> &mut (impl hir::db::HirDatabase + salsa::Database) {
285+
&mut self.db
286+
}
284287
}
285288

286289
/// Analysis is a snapshot of a world state at a moment in time. It is the main

crates/ra_lsp_server/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
#![recursion_limit = "512"]
12
mod caps;
23
mod cargo_target_spec;
34
mod conv;

0 commit comments

Comments
 (0)