rust-analyzer/crates/ra_ide_db/src/change.rs

317 lines
11 KiB
Rust
Raw Normal View History

2020-02-06 14:08:31 +00:00
//! Defines a unit of change that can applied to a state of IDE to get the next
//! state. Changes are transactional.
use std::{fmt, sync::Arc, time};
2019-02-08 08:52:18 +00:00
use ra_db::{
2019-06-26 06:12:46 +00:00
salsa::{Database, Durability, SweepStrategy},
2020-03-08 13:26:57 +00:00
CrateGraph, FileId, RelativePathBuf, SourceDatabase, SourceDatabaseExt, SourceRoot,
2019-11-03 22:14:17 +00:00
SourceRootId,
2019-02-08 08:52:18 +00:00
};
use ra_prof::{memory_usage, profile, Bytes};
use rustc_hash::FxHashMap;
2019-02-08 08:52:18 +00:00
use crate::{symbol_index::SymbolsDatabase, RootDatabase};
2019-02-08 08:52:18 +00:00
#[derive(Default)]
pub struct AnalysisChange {
new_roots: Vec<(SourceRootId, bool)>,
roots_changed: FxHashMap<SourceRootId, RootChange>,
files_changed: Vec<(FileId, Arc<String>)>,
crate_graph: Option<CrateGraph>,
}
impl fmt::Debug for AnalysisChange {
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
let mut d = fmt.debug_struct("AnalysisChange");
if !self.new_roots.is_empty() {
d.field("new_roots", &self.new_roots);
}
if !self.roots_changed.is_empty() {
d.field("roots_changed", &self.roots_changed);
}
if !self.files_changed.is_empty() {
d.field("files_changed", &self.files_changed.len());
}
if self.crate_graph.is_some() {
2019-02-08 08:52:18 +00:00
d.field("crate_graph", &self.crate_graph);
}
d.finish()
}
}
impl AnalysisChange {
pub fn new() -> AnalysisChange {
AnalysisChange::default()
}
pub fn add_root(&mut self, root_id: SourceRootId, is_local: bool) {
self.new_roots.push((root_id, is_local));
}
pub fn add_file(
&mut self,
root_id: SourceRootId,
file_id: FileId,
path: RelativePathBuf,
text: Arc<String>,
) {
2019-02-08 11:49:43 +00:00
let file = AddFile { file_id, path, text };
self.roots_changed.entry(root_id).or_default().added.push(file);
2019-02-08 08:52:18 +00:00
}
pub fn change_file(&mut self, file_id: FileId, new_text: Arc<String>) {
self.files_changed.push((file_id, new_text))
}
pub fn remove_file(&mut self, root_id: SourceRootId, file_id: FileId, path: RelativePathBuf) {
let file = RemoveFile { file_id, path };
2019-02-08 11:49:43 +00:00
self.roots_changed.entry(root_id).or_default().removed.push(file);
2019-02-08 08:52:18 +00:00
}
pub fn set_crate_graph(&mut self, graph: CrateGraph) {
self.crate_graph = Some(graph);
}
}
#[derive(Debug)]
struct AddFile {
file_id: FileId,
path: RelativePathBuf,
text: Arc<String>,
}
#[derive(Debug)]
struct RemoveFile {
file_id: FileId,
path: RelativePathBuf,
}
#[derive(Default)]
struct RootChange {
added: Vec<AddFile>,
removed: Vec<RemoveFile>,
}
impl fmt::Debug for RootChange {
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
fmt.debug_struct("AnalysisChange")
.field("added", &self.added.len())
.field("removed", &self.removed.len())
.finish()
}
}
const GC_COOLDOWN: time::Duration = time::Duration::from_millis(100);
impl RootDatabase {
2020-02-06 11:43:56 +00:00
pub fn request_cancellation(&mut self) {
2020-01-24 15:35:37 +00:00
let _p = profile("RootDatabase::request_cancellation");
self.salsa_runtime_mut().synthetic_write(Durability::LOW);
}
2020-02-06 11:43:56 +00:00
pub fn apply_change(&mut self, change: AnalysisChange) {
2019-04-14 20:28:10 +00:00
let _p = profile("RootDatabase::apply_change");
2020-01-24 15:35:37 +00:00
self.request_cancellation();
2019-02-08 08:52:18 +00:00
log::info!("apply_change {:?}", change);
if !change.new_roots.is_empty() {
let mut local_roots = Vec::clone(&self.local_roots());
let mut libraries = Vec::clone(&self.library_roots());
2019-02-08 08:52:18 +00:00
for (root_id, is_local) in change.new_roots {
let root =
if is_local { SourceRoot::new_local() } else { SourceRoot::new_library() };
2019-06-26 06:12:46 +00:00
let durability = durability(&root);
self.set_source_root_with_durability(root_id, Arc::new(root), durability);
2019-02-08 08:52:18 +00:00
if is_local {
local_roots.push(root_id);
} else {
libraries.push(root_id)
2019-02-08 08:52:18 +00:00
}
}
2019-06-26 06:12:46 +00:00
self.set_local_roots_with_durability(Arc::new(local_roots), Durability::HIGH);
self.set_library_roots_with_durability(Arc::new(libraries), Durability::HIGH);
2019-02-08 08:52:18 +00:00
}
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 {
2019-06-26 06:12:46 +00:00
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)
2019-02-08 08:52:18 +00:00
}
if let Some(crate_graph) = change.crate_graph {
2019-06-26 06:12:46 +00:00
self.set_crate_graph_with_durability(Arc::new(crate_graph), Durability::HIGH)
2019-02-08 08:52:18 +00:00
}
}
fn apply_root_change(&mut self, root_id: SourceRootId, root_change: RootChange) {
let mut source_root = SourceRoot::clone(&self.source_root(root_id));
2019-06-26 06:12:46 +00:00
let durability = durability(&source_root);
2019-02-08 08:52:18 +00:00
for add_file in root_change.added {
2019-06-26 06:12:46 +00:00
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);
2019-09-05 19:36:04 +00:00
source_root.insert_file(add_file.path, add_file.file_id);
2019-02-08 08:52:18 +00:00
}
for remove_file in root_change.removed {
2019-06-26 06:12:46 +00:00
self.set_file_text_with_durability(remove_file.file_id, Default::default(), durability);
2019-09-05 19:36:04 +00:00
source_root.remove_file(&remove_file.path);
2019-02-08 08:52:18 +00:00
}
2019-06-26 06:12:46 +00:00
self.set_source_root_with_durability(root_id, Arc::new(source_root), durability);
2019-02-08 08:52:18 +00:00
}
2020-02-06 11:43:56 +00:00
pub fn maybe_collect_garbage(&mut self) {
2019-09-20 17:38:16 +00:00
if cfg!(feature = "wasm") {
return;
}
2019-02-08 08:52:18 +00:00
if self.last_gc_check.elapsed() > GC_COOLDOWN {
2019-09-20 17:38:16 +00:00
self.last_gc_check = crate::wasm_shims::Instant::now();
2019-02-08 08:52:18 +00:00
}
}
2020-02-06 11:43:56 +00:00
pub fn collect_garbage(&mut self) {
2019-09-20 17:38:16 +00:00
if cfg!(feature = "wasm") {
return;
}
let _p = profile("RootDatabase::collect_garbage");
2019-09-20 17:38:16 +00:00
self.last_gc = crate::wasm_shims::Instant::now();
2019-02-08 08:52:18 +00:00
2019-02-08 11:49:43 +00:00
let sweep = SweepStrategy::default().discard_values().sweep_all_revisions();
2019-02-08 08:52:18 +00:00
self.query(ra_db::ParseQuery).sweep(sweep);
self.query(hir::db::ParseMacroQuery).sweep(sweep);
2019-06-20 13:48:10 +00:00
// Macros do take significant space, but less then the syntax trees
// self.query(hir::db::MacroDefQuery).sweep(sweep);
// self.query(hir::db::MacroArgQuery).sweep(sweep);
// self.query(hir::db::MacroExpandQuery).sweep(sweep);
2019-03-26 16:00:11 +00:00
self.query(hir::db::AstIdMapQuery).sweep(sweep);
2019-02-08 08:52:18 +00:00
2019-03-02 13:18:40 +00:00
self.query(hir::db::BodyWithSourceMapQuery).sweep(sweep);
2019-06-01 19:47:20 +00:00
self.query(hir::db::ExprScopesQuery).sweep(sweep);
2020-03-06 23:11:52 +00:00
self.query(hir::db::InferQueryQuery).sweep(sweep);
2019-11-12 08:48:34 +00:00
self.query(hir::db::BodyQuery).sweep(sweep);
2019-02-08 08:52:18 +00:00
}
2019-06-30 11:40:01 +00:00
2020-02-06 11:43:56 +00:00
pub fn per_query_memory_usage(&mut self) -> Vec<(String, Bytes)> {
2019-06-30 11:40:01 +00:00
let mut acc: Vec<(String, Bytes)> = vec![];
let sweep = SweepStrategy::default().discard_values().sweep_all_revisions();
macro_rules! sweep_each_query {
($($q:path)*) => {$(
let before = memory_usage().allocated;
self.query($q).sweep(sweep);
let after = memory_usage().allocated;
let q: $q = Default::default();
let name = format!("{:?}", q);
acc.push((name, before - after));
let before = memory_usage().allocated;
self.query($q).sweep(sweep.discard_everything());
let after = memory_usage().allocated;
let q: $q = Default::default();
let name = format!("{:?} (deps)", q);
acc.push((name, before - after));
2019-06-30 11:40:01 +00:00
)*}
}
sweep_each_query![
// SourceDatabase
2019-06-30 11:40:01 +00:00
ra_db::ParseQuery
ra_db::SourceRootCratesQuery
// AstDatabase
2019-06-30 11:40:01 +00:00
hir::db::AstIdMapQuery
hir::db::InternMacroQuery
2019-06-30 11:40:01 +00:00
hir::db::MacroArgQuery
hir::db::MacroDefQuery
hir::db::ParseMacroQuery
2019-06-30 11:40:01 +00:00
hir::db::MacroExpandQuery
2020-03-25 17:41:46 +00:00
hir::db::InternEagerExpansionQuery
// DefDatabase
hir::db::RawItemsQuery
2020-03-06 23:11:52 +00:00
hir::db::CrateDefMapQueryQuery
2019-06-30 11:40:01 +00:00
hir::db::StructDataQuery
hir::db::UnionDataQuery
2019-06-30 11:40:01 +00:00
hir::db::EnumDataQuery
hir::db::ImplDataQuery
2019-06-30 11:40:01 +00:00
hir::db::TraitDataQuery
hir::db::TypeAliasDataQuery
hir::db::FunctionDataQuery
2019-06-30 11:40:01 +00:00
hir::db::ConstDataQuery
hir::db::StaticDataQuery
hir::db::BodyWithSourceMapQuery
hir::db::BodyQuery
hir::db::ExprScopesQuery
hir::db::GenericParamsQuery
hir::db::AttrsQuery
2019-06-30 11:40:01 +00:00
hir::db::ModuleLangItemsQuery
hir::db::CrateLangItemsQuery
2019-06-30 11:40:01 +00:00
hir::db::LangItemQuery
hir::db::DocumentationQuery
2020-06-05 11:10:43 +00:00
hir::db::ImportMapQuery
// InternDatabase
hir::db::InternFunctionQuery
hir::db::InternStructQuery
hir::db::InternUnionQuery
hir::db::InternEnumQuery
hir::db::InternConstQuery
hir::db::InternStaticQuery
hir::db::InternTraitQuery
hir::db::InternTypeAliasQuery
hir::db::InternImplQuery
// HirDatabase
2020-03-06 23:11:52 +00:00
hir::db::InferQueryQuery
2019-11-26 18:04:24 +00:00
hir::db::TyQuery
hir::db::ValueTyQuery
hir::db::ImplSelfTyQuery
hir::db::ImplTraitQuery
hir::db::FieldTypesQuery
2019-06-30 11:40:01 +00:00
hir::db::CallableItemSignatureQuery
hir::db::GenericPredicatesForParamQuery
2019-06-30 11:40:01 +00:00
hir::db::GenericPredicatesQuery
hir::db::GenericDefaultsQuery
hir::db::ImplsInCrateQuery
2020-06-19 21:17:53 +00:00
hir::db::ImplsFromDepsQuery
hir::db::InternTypeCtorQuery
2020-03-25 17:41:46 +00:00
hir::db::InternTypeParamIdQuery
hir::db::InternChalkImplQuery
hir::db::InternAssocTyValueQuery
2019-06-30 11:40:01 +00:00
hir::db::AssociatedTyDataQuery
hir::db::TraitDatumQuery
hir::db::StructDatumQuery
hir::db::ImplDatumQuery
2020-03-25 17:41:46 +00:00
hir::db::AssociatedTyValueQuery
hir::db::TraitSolveQuery
2020-06-05 15:41:58 +00:00
hir::db::ReturnTypeImplTraitsQuery
2020-03-25 17:41:46 +00:00
// SymbolsDatabase
crate::symbol_index::FileSymbolsQuery
// LineIndexDatabase
crate::LineIndexQuery
2019-06-30 11:40:01 +00:00
];
acc.sort_by_key(|it| std::cmp::Reverse(it.1));
acc
}
2019-02-08 08:52:18 +00:00
}
2019-06-26 06:12:46 +00:00
fn durability(source_root: &SourceRoot) -> Durability {
if source_root.is_library {
Durability::HIGH
} else {
Durability::LOW
}
}