2020-10-02 13:45:09 +00:00
|
|
|
//! Applies changes to the IDE state transactionally.
|
2019-09-30 08:58:53 +00:00
|
|
|
|
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},
|
2020-10-02 13:45:09 +00:00
|
|
|
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
|
|
|
|
2020-06-18 06:29:34 +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 {
|
2021-02-28 11:57:41 +00:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2020-10-02 13:45:09 +00:00
|
|
|
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);
|
2020-10-02 13:45:09 +00:00
|
|
|
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();
|
2020-10-02 13:45:09 +00:00
|
|
|
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);
|
2020-06-18 06:29:34 +00:00
|
|
|
} 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
|
|
|
}
|
2020-10-02 13:45:09 +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) {
|
2021-02-16 17:50:06 +00:00
|
|
|
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);
|
2020-11-24 20:57:51 +00:00
|
|
|
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);
|
2020-07-22 18:50:37 +00:00
|
|
|
// self.query(hir::db::MacroArgTextQuery).sweep(sweep);
|
2019-06-20 13:48:10 +00:00
|
|
|
// self.query(hir::db::MacroExpandQuery).sweep(sweep);
|
|
|
|
|
2020-07-07 08:14:48 +00:00
|
|
|
hir::db::AstIdMapQuery.in_db(self).sweep(sweep);
|
2019-02-08 08:52:18 +00:00
|
|
|
|
2020-07-07 08:14:48 +00:00
|
|
|
hir::db::BodyWithSourceMapQuery.in_db(self).sweep(sweep);
|
2019-06-01 19:47:20 +00:00
|
|
|
|
2020-07-07 08:14:48 +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
|
|
|
|
2020-07-07 10:10:14 +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;
|
2020-07-07 08:14:48 +00:00
|
|
|
$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));
|
2019-07-12 15:04:48 +00:00
|
|
|
|
|
|
|
let before = memory_usage().allocated;
|
2020-07-07 08:14:48 +00:00
|
|
|
$q.in_db(self).sweep(sweep.discard_everything());
|
2019-07-12 15:04:48 +00:00
|
|
|
let after = memory_usage().allocated;
|
|
|
|
let q: $q = Default::default();
|
|
|
|
let name = format!("{:?} (deps)", q);
|
|
|
|
acc.push((name, before - after));
|
2020-07-22 16:44:40 +00:00
|
|
|
|
|
|
|
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![
|
2020-01-29 15:10:46 +00:00
|
|
|
// SourceDatabase
|
2020-08-13 14:25:38 +00:00
|
|
|
base_db::ParseQuery
|
|
|
|
base_db::CrateGraphQuery
|
2020-07-22 16:44:40 +00:00
|
|
|
|
|
|
|
// SourceDatabaseExt
|
2020-08-13 14:25:38 +00:00
|
|
|
base_db::FileTextQuery
|
|
|
|
base_db::FileSourceRootQuery
|
|
|
|
base_db::SourceRootQuery
|
|
|
|
base_db::SourceRootCratesQuery
|
2020-01-29 15:10:46 +00:00
|
|
|
|
|
|
|
// AstDatabase
|
2019-06-30 11:40:01 +00:00
|
|
|
hir::db::AstIdMapQuery
|
2020-07-22 18:50:37 +00:00
|
|
|
hir::db::MacroArgTextQuery
|
2020-01-29 15:10:46 +00:00
|
|
|
hir::db::MacroDefQuery
|
2020-11-24 20:57:51 +00:00
|
|
|
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
|
2020-01-29 15:10:46 +00:00
|
|
|
|
|
|
|
// DefDatabase
|
2021-03-13 01:24:26 +00:00
|
|
|
hir::db::FileItemTreeQuery
|
2021-01-21 14:22:17 +00:00
|
|
|
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
|
2020-01-29 15:10:46 +00:00
|
|
|
hir::db::UnionDataQuery
|
2019-06-30 11:40:01 +00:00
|
|
|
hir::db::EnumDataQuery
|
2020-01-29 15:10:46 +00:00
|
|
|
hir::db::ImplDataQuery
|
2019-06-30 11:40:01 +00:00
|
|
|
hir::db::TraitDataQuery
|
|
|
|
hir::db::TypeAliasDataQuery
|
2020-01-29 15:10:46 +00:00
|
|
|
hir::db::FunctionDataQuery
|
2019-06-30 11:40:01 +00:00
|
|
|
hir::db::ConstDataQuery
|
|
|
|
hir::db::StaticDataQuery
|
2020-01-29 15:10:46 +00:00
|
|
|
hir::db::BodyWithSourceMapQuery
|
|
|
|
hir::db::BodyQuery
|
|
|
|
hir::db::ExprScopesQuery
|
|
|
|
hir::db::GenericParamsQuery
|
|
|
|
hir::db::AttrsQuery
|
2019-09-25 18:01:02 +00:00
|
|
|
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
|
2020-01-29 15:10:46 +00:00
|
|
|
|
|
|
|
// 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
|
2020-01-29 15:10:46 +00:00
|
|
|
hir::db::ImplSelfTyQuery
|
|
|
|
hir::db::ImplTraitQuery
|
2019-11-24 20:48:39 +00:00
|
|
|
hir::db::FieldTypesQuery
|
2019-06-30 11:40:01 +00:00
|
|
|
hir::db::CallableItemSignatureQuery
|
2020-01-29 15:10:46 +00:00
|
|
|
hir::db::GenericPredicatesForParamQuery
|
2019-06-30 11:40:01 +00:00
|
|
|
hir::db::GenericPredicatesQuery
|
|
|
|
hir::db::GenericDefaultsQuery
|
2020-07-01 15:15:20 +00:00
|
|
|
hir::db::InherentImplsInCrateQuery
|
2021-03-24 17:59:35 +00:00
|
|
|
hir::db::TraitEnvironmentQuery
|
2020-07-01 15:15:20 +00:00
|
|
|
hir::db::TraitImplsInCrateQuery
|
|
|
|
hir::db::TraitImplsInDepsQuery
|
2019-06-30 11:40:01 +00:00
|
|
|
hir::db::AssociatedTyDataQuery
|
2020-07-22 16:44:40 +00:00
|
|
|
hir::db::AssociatedTyDataQuery
|
2019-06-30 11:40:01 +00:00
|
|
|
hir::db::TraitDatumQuery
|
|
|
|
hir::db::StructDatumQuery
|
|
|
|
hir::db::ImplDatumQuery
|
2020-07-22 16:44:40 +00:00
|
|
|
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
|
2020-07-22 16:44:40 +00:00
|
|
|
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
|
|
|
];
|
2020-07-03 15:12:29 +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.
|
2020-07-07 08:14:48 +00:00
|
|
|
self.salsa_runtime_mut().synthetic_write(Durability::HIGH);
|
2020-07-03 15:12:29 +00:00
|
|
|
|
|
|
|
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
|
|
|
}
|