rust-analyzer/crates/ide_db/src/apply_change.rs

241 lines
8 KiB
Rust
Raw Normal View History

//! Applies changes to the IDE state transactionally.
2020-09-29 19:13:58 +00:00
use std::{fmt, sync::Arc};
2019-02-08 08:52:18 +00:00
2020-08-13 14:25:38 +00:00
use base_db::{
2019-06-26 06:12:46 +00:00
salsa::{Database, Durability, SweepStrategy},
Change, FileId, SourceRootId,
2019-02-08 08:52:18 +00:00
};
2020-08-13 14:25:38 +00:00
use profile::{memory_usage, Bytes};
2020-06-11 09:04:09 +00:00
use rustc_hash::FxHashSet;
2019-02-08 08:52:18 +00:00
use crate::{symbol_index::SymbolsDatabase, RootDatabase};
2019-02-08 08:52:18 +00:00
#[derive(Debug)]
struct AddFile {
file_id: FileId,
2020-07-08 17:09:42 +00:00
path: String,
2019-02-08 08:52:18 +00:00
text: Arc<String>,
}
#[derive(Debug)]
struct RemoveFile {
file_id: FileId,
2020-07-08 17:09:42 +00:00
path: String,
2019-02-08 08:52:18 +00:00
}
#[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("RootChange")
2019-02-08 08:52:18 +00:00
.field("added", &self.added.len())
.field("removed", &self.removed.len())
.finish()
}
}
impl RootDatabase {
2020-02-06 11:43:56 +00:00
pub fn request_cancellation(&mut self) {
2020-08-12 14:32:36 +00:00
let _p = profile::span("RootDatabase::request_cancellation");
2020-01-24 15:35:37 +00:00
self.salsa_runtime_mut().synthetic_write(Durability::LOW);
}
pub fn apply_change(&mut self, change: Change) {
2020-08-12 14:32:36 +00:00
let _p = profile::span("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 let Some(roots) = &change.roots {
2020-06-11 09:04:09 +00:00
let mut local_roots = FxHashSet::default();
let mut library_roots = FxHashSet::default();
for (idx, root) in roots.iter().enumerate() {
2020-06-11 09:04:09 +00:00
let root_id = SourceRootId(idx as u32);
if root.is_library {
library_roots.insert(root_id);
} else {
2020-06-11 09:04:09 +00:00
local_roots.insert(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);
2020-06-11 09:04:09 +00:00
self.set_library_roots_with_durability(Arc::new(library_roots), Durability::HIGH);
2019-02-08 08:52:18 +00:00
}
change.apply(self);
2019-02-08 08:52:18 +00:00
}
2020-02-06 11:43:56 +00:00
pub fn collect_garbage(&mut self) {
if cfg!(target_arch = "wasm32") {
2019-09-20 17:38:16 +00:00
return;
}
2020-08-12 14:32:36 +00:00
let _p = profile::span("RootDatabase::collect_garbage");
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
2020-08-13 14:25:38 +00:00
base_db::ParseQuery.in_db(self).sweep(sweep);
hir::db::ParseMacroExpansionQuery.in_db(self).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::MacroArgTextQuery).sweep(sweep);
2019-06-20 13:48:10 +00:00
// self.query(hir::db::MacroExpandQuery).sweep(sweep);
hir::db::AstIdMapQuery.in_db(self).sweep(sweep);
2019-02-08 08:52:18 +00:00
hir::db::BodyWithSourceMapQuery.in_db(self).sweep(sweep);
2019-06-01 19:47:20 +00:00
hir::db::ExprScopesQuery.in_db(self).sweep(sweep);
hir::db::InferQueryQuery.in_db(self).sweep(sweep);
hir::db::BodyQuery.in_db(self).sweep(sweep);
2019-02-08 08:52:18 +00:00
}
2019-06-30 11:40:01 +00:00
// Feature: Memory Usage
//
// Clears rust-analyzer's internal database and prints memory usage statistics.
//
// |===
// | Editor | Action Name
//
// | VS Code | **Rust Analyzer: Memory Usage (Clears Database)**
// |===
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;
$q.in_db(self).sweep(sweep);
2019-06-30 11:40:01 +00:00
let after = memory_usage().allocated;
let q: $q = Default::default();
let name = format!("{:?}", q);
acc.push((name, before - after));
let before = memory_usage().allocated;
$q.in_db(self).sweep(sweep.discard_everything());
let after = memory_usage().allocated;
let q: $q = Default::default();
let name = format!("{:?} (deps)", q);
acc.push((name, before - after));
let before = memory_usage().allocated;
$q.in_db(self).purge();
let after = memory_usage().allocated;
let q: $q = Default::default();
let name = format!("{:?} (purge)", q);
acc.push((name, before - after));
2019-06-30 11:40:01 +00:00
)*}
}
sweep_each_query![
// SourceDatabase
2020-08-13 14:25:38 +00:00
base_db::ParseQuery
base_db::CrateGraphQuery
// SourceDatabaseExt
2020-08-13 14:25:38 +00:00
base_db::FileTextQuery
base_db::FileSourceRootQuery
base_db::SourceRootQuery
base_db::SourceRootCratesQuery
// AstDatabase
2019-06-30 11:40:01 +00:00
hir::db::AstIdMapQuery
hir::db::MacroArgTextQuery
hir::db::MacroDefQuery
hir::db::ParseMacroExpansionQuery
2019-06-30 11:40:01 +00:00
hir::db::MacroExpandQuery
2021-01-04 02:53:31 +00:00
hir::db::HygieneFrameQuery
// DefDatabase
hir::db::FileItemTreeQuery
hir::db::BlockDefMapQuery
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
hir::db::CrateLangItemsQuery
2019-06-30 11:40:01 +00:00
hir::db::LangItemQuery
2020-06-05 11:10:43 +00:00
hir::db::ImportMapQuery
// 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::InherentImplsInCrateQuery
2021-03-24 17:59:35 +00:00
hir::db::TraitEnvironmentQuery
hir::db::TraitImplsInCrateQuery
hir::db::TraitImplsInDepsQuery
2019-06-30 11:40:01 +00:00
hir::db::AssociatedTyDataQuery
hir::db::AssociatedTyDataQuery
2019-06-30 11:40:01 +00:00
hir::db::TraitDatumQuery
hir::db::StructDatumQuery
hir::db::ImplDatumQuery
hir::db::FnDefDatumQuery
hir::db::ReturnTypeImplTraitsQuery
hir::db::InternCallableDefQuery
hir::db::InternTypeParamIdQuery
hir::db::InternImplTraitIdQuery
hir::db::InternClosureQuery
2020-03-25 17:41:46 +00:00
hir::db::AssociatedTyValueQuery
hir::db::TraitSolveQuery
// SymbolsDatabase
crate::symbol_index::FileSymbolsQuery
crate::symbol_index::LibrarySymbolsQuery
crate::symbol_index::LocalRootsQuery
crate::symbol_index::LibraryRootsQuery
2020-03-25 17:41:46 +00:00
// LineIndexDatabase
crate::LineIndexQuery
2019-06-30 11:40:01 +00:00
];
// To collect interned data, we need to bump the revision counter by performing a synthetic
// write.
// We do this after collecting the non-interned queries to correctly attribute memory used
// by interned data.
self.salsa_runtime_mut().synthetic_write(Durability::HIGH);
sweep_each_query![
// AstDatabase
hir::db::InternMacroQuery
hir::db::InternEagerExpansionQuery
// 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
hir::db::InternTypeParamIdQuery
];
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
}