2020-02-06 08:08:31 -06:00
|
|
|
//! Defines a unit of change that can applied to a state of IDE to get the next
|
|
|
|
//! state. Changes are transactional.
|
2019-09-30 03:58:53 -05:00
|
|
|
|
2019-07-04 15:05:17 -05:00
|
|
|
use std::{fmt, sync::Arc, time};
|
2019-02-08 02:52:18 -06:00
|
|
|
|
2020-08-13 09:25:38 -05:00
|
|
|
use base_db::{
|
2019-06-26 01:12:46 -05:00
|
|
|
salsa::{Database, Durability, SweepStrategy},
|
2020-07-08 12:09:42 -05:00
|
|
|
CrateGraph, FileId, SourceDatabase, SourceDatabaseExt, SourceRoot, SourceRootId,
|
2019-02-08 02:52:18 -06:00
|
|
|
};
|
2020-08-13 09:25:38 -05:00
|
|
|
use profile::{memory_usage, Bytes};
|
2020-06-11 04:04:09 -05:00
|
|
|
use rustc_hash::FxHashSet;
|
2019-02-08 02:52:18 -06:00
|
|
|
|
2020-06-18 01:29:34 -05:00
|
|
|
use crate::{symbol_index::SymbolsDatabase, RootDatabase};
|
2019-02-08 02:52:18 -06:00
|
|
|
|
|
|
|
#[derive(Default)]
|
|
|
|
pub struct AnalysisChange {
|
2020-06-11 04:04:09 -05:00
|
|
|
roots: Option<Vec<SourceRoot>>,
|
|
|
|
files_changed: Vec<(FileId, Option<Arc<String>>)>,
|
2019-02-08 02:52:18 -06:00
|
|
|
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");
|
2020-06-11 04:04:09 -05:00
|
|
|
if let Some(roots) = &self.roots {
|
|
|
|
d.field("roots", roots);
|
2019-02-08 02:52:18 -06:00
|
|
|
}
|
|
|
|
if !self.files_changed.is_empty() {
|
|
|
|
d.field("files_changed", &self.files_changed.len());
|
|
|
|
}
|
2020-02-18 06:53:02 -06:00
|
|
|
if self.crate_graph.is_some() {
|
2019-02-08 02:52:18 -06:00
|
|
|
d.field("crate_graph", &self.crate_graph);
|
|
|
|
}
|
|
|
|
d.finish()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl AnalysisChange {
|
|
|
|
pub fn new() -> AnalysisChange {
|
|
|
|
AnalysisChange::default()
|
|
|
|
}
|
|
|
|
|
2020-06-11 04:04:09 -05:00
|
|
|
pub fn set_roots(&mut self, roots: Vec<SourceRoot>) {
|
|
|
|
self.roots = Some(roots);
|
2019-02-08 02:52:18 -06:00
|
|
|
}
|
|
|
|
|
2020-06-11 04:04:09 -05:00
|
|
|
pub fn change_file(&mut self, file_id: FileId, new_text: Option<Arc<String>>) {
|
2019-02-08 02:52:18 -06:00
|
|
|
self.files_changed.push((file_id, new_text))
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn set_crate_graph(&mut self, graph: CrateGraph) {
|
|
|
|
self.crate_graph = Some(graph);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug)]
|
|
|
|
struct AddFile {
|
|
|
|
file_id: FileId,
|
2020-07-08 12:09:42 -05:00
|
|
|
path: String,
|
2019-02-08 02:52:18 -06:00
|
|
|
text: Arc<String>,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug)]
|
|
|
|
struct RemoveFile {
|
|
|
|
file_id: FileId,
|
2020-07-08 12:09:42 -05:00
|
|
|
path: String,
|
2019-02-08 02:52:18 -06: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("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 05:43:56 -06:00
|
|
|
pub fn request_cancellation(&mut self) {
|
2020-08-12 09:32:36 -05:00
|
|
|
let _p = profile::span("RootDatabase::request_cancellation");
|
2020-01-24 09:35:37 -06:00
|
|
|
self.salsa_runtime_mut().synthetic_write(Durability::LOW);
|
|
|
|
}
|
|
|
|
|
2020-02-06 05:43:56 -06:00
|
|
|
pub fn apply_change(&mut self, change: AnalysisChange) {
|
2020-08-12 09:32:36 -05:00
|
|
|
let _p = profile::span("RootDatabase::apply_change");
|
2020-01-24 09:35:37 -06:00
|
|
|
self.request_cancellation();
|
2019-02-08 02:52:18 -06:00
|
|
|
log::info!("apply_change {:?}", change);
|
2020-06-11 04:04:09 -05:00
|
|
|
if let Some(roots) = change.roots {
|
|
|
|
let mut local_roots = FxHashSet::default();
|
|
|
|
let mut library_roots = FxHashSet::default();
|
|
|
|
for (idx, root) in roots.into_iter().enumerate() {
|
|
|
|
let root_id = SourceRootId(idx as u32);
|
2019-06-26 01:12:46 -05:00
|
|
|
let durability = durability(&root);
|
2020-06-11 04:04:09 -05:00
|
|
|
if root.is_library {
|
|
|
|
library_roots.insert(root_id);
|
2020-06-18 01:29:34 -05:00
|
|
|
} else {
|
2020-06-11 04:04:09 -05:00
|
|
|
local_roots.insert(root_id);
|
|
|
|
}
|
|
|
|
for file_id in root.iter() {
|
|
|
|
self.set_file_source_root_with_durability(file_id, root_id, durability);
|
2019-02-08 02:52:18 -06:00
|
|
|
}
|
2020-06-11 04:04:09 -05:00
|
|
|
self.set_source_root_with_durability(root_id, Arc::new(root), durability);
|
2019-02-08 02:52:18 -06:00
|
|
|
}
|
2019-06-26 01:12:46 -05:00
|
|
|
self.set_local_roots_with_durability(Arc::new(local_roots), Durability::HIGH);
|
2020-06-11 04:04:09 -05:00
|
|
|
self.set_library_roots_with_durability(Arc::new(library_roots), Durability::HIGH);
|
2019-02-08 02:52:18 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
for (file_id, text) in change.files_changed {
|
2019-06-26 01:12:46 -05: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);
|
2020-06-11 04:04:09 -05:00
|
|
|
// XXX: can't actually remove the file, just reset the text
|
|
|
|
let text = text.unwrap_or_default();
|
2019-06-26 01:12:46 -05:00
|
|
|
self.set_file_text_with_durability(file_id, text, durability)
|
2019-02-08 02:52:18 -06:00
|
|
|
}
|
|
|
|
if let Some(crate_graph) = change.crate_graph {
|
2019-06-26 01:12:46 -05:00
|
|
|
self.set_crate_graph_with_durability(Arc::new(crate_graph), Durability::HIGH)
|
2019-02-08 02:52:18 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-06 05:43:56 -06:00
|
|
|
pub fn maybe_collect_garbage(&mut self) {
|
2019-09-20 12:38:16 -05:00
|
|
|
if cfg!(feature = "wasm") {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-02-08 02:52:18 -06:00
|
|
|
if self.last_gc_check.elapsed() > GC_COOLDOWN {
|
2019-09-20 12:38:16 -05:00
|
|
|
self.last_gc_check = crate::wasm_shims::Instant::now();
|
2019-02-08 02:52:18 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-06 05:43:56 -06:00
|
|
|
pub fn collect_garbage(&mut self) {
|
2019-09-20 12:38:16 -05:00
|
|
|
if cfg!(feature = "wasm") {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-08-12 09:32:36 -05:00
|
|
|
let _p = profile::span("RootDatabase::collect_garbage");
|
2019-09-20 12:38:16 -05:00
|
|
|
self.last_gc = crate::wasm_shims::Instant::now();
|
2019-02-08 02:52:18 -06:00
|
|
|
|
2019-02-08 05:49:43 -06:00
|
|
|
let sweep = SweepStrategy::default().discard_values().sweep_all_revisions();
|
2019-02-08 02:52:18 -06:00
|
|
|
|
2020-08-13 09:25:38 -05:00
|
|
|
base_db::ParseQuery.in_db(self).sweep(sweep);
|
2020-07-07 03:14:48 -05:00
|
|
|
hir::db::ParseMacroQuery.in_db(self).sweep(sweep);
|
2019-06-20 08:48:10 -05:00
|
|
|
|
|
|
|
// Macros do take significant space, but less then the syntax trees
|
|
|
|
// self.query(hir::db::MacroDefQuery).sweep(sweep);
|
2020-07-22 13:50:37 -05:00
|
|
|
// self.query(hir::db::MacroArgTextQuery).sweep(sweep);
|
2019-06-20 08:48:10 -05:00
|
|
|
// self.query(hir::db::MacroExpandQuery).sweep(sweep);
|
|
|
|
|
2020-07-07 03:14:48 -05:00
|
|
|
hir::db::AstIdMapQuery.in_db(self).sweep(sweep);
|
2019-02-08 02:52:18 -06:00
|
|
|
|
2020-07-07 03:14:48 -05:00
|
|
|
hir::db::BodyWithSourceMapQuery.in_db(self).sweep(sweep);
|
2019-06-01 14:47:20 -05:00
|
|
|
|
2020-07-07 03:14:48 -05: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 02:52:18 -06:00
|
|
|
}
|
2019-06-30 06:40:01 -05:00
|
|
|
|
2020-07-07 05:10:14 -05: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 05:43:56 -06:00
|
|
|
pub fn per_query_memory_usage(&mut self) -> Vec<(String, Bytes)> {
|
2019-06-30 06:40:01 -05: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 03:14:48 -05:00
|
|
|
$q.in_db(self).sweep(sweep);
|
2019-06-30 06:40:01 -05:00
|
|
|
let after = memory_usage().allocated;
|
|
|
|
let q: $q = Default::default();
|
|
|
|
let name = format!("{:?}", q);
|
|
|
|
acc.push((name, before - after));
|
2019-07-12 10:04:48 -05:00
|
|
|
|
|
|
|
let before = memory_usage().allocated;
|
2020-07-07 03:14:48 -05:00
|
|
|
$q.in_db(self).sweep(sweep.discard_everything());
|
2019-07-12 10:04:48 -05:00
|
|
|
let after = memory_usage().allocated;
|
|
|
|
let q: $q = Default::default();
|
|
|
|
let name = format!("{:?} (deps)", q);
|
|
|
|
acc.push((name, before - after));
|
2020-07-22 11:44:40 -05: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 06:40:01 -05:00
|
|
|
)*}
|
|
|
|
}
|
|
|
|
sweep_each_query![
|
2020-01-29 09:10:46 -06:00
|
|
|
// SourceDatabase
|
2020-08-13 09:25:38 -05:00
|
|
|
base_db::ParseQuery
|
|
|
|
base_db::CrateGraphQuery
|
2020-07-22 11:44:40 -05:00
|
|
|
|
|
|
|
// SourceDatabaseExt
|
2020-08-13 09:25:38 -05:00
|
|
|
base_db::FileTextQuery
|
|
|
|
base_db::FileSourceRootQuery
|
|
|
|
base_db::SourceRootQuery
|
|
|
|
base_db::SourceRootCratesQuery
|
2020-01-29 09:10:46 -06:00
|
|
|
|
|
|
|
// AstDatabase
|
2019-06-30 06:40:01 -05:00
|
|
|
hir::db::AstIdMapQuery
|
2020-07-22 13:50:37 -05:00
|
|
|
hir::db::MacroArgTextQuery
|
2020-01-29 09:10:46 -06:00
|
|
|
hir::db::MacroDefQuery
|
|
|
|
hir::db::ParseMacroQuery
|
2019-06-30 06:40:01 -05:00
|
|
|
hir::db::MacroExpandQuery
|
2020-01-29 09:10:46 -06:00
|
|
|
|
|
|
|
// DefDatabase
|
2020-06-23 04:57:23 -05:00
|
|
|
hir::db::ItemTreeQuery
|
2020-03-06 17:11:52 -06:00
|
|
|
hir::db::CrateDefMapQueryQuery
|
2019-06-30 06:40:01 -05:00
|
|
|
hir::db::StructDataQuery
|
2020-01-29 09:10:46 -06:00
|
|
|
hir::db::UnionDataQuery
|
2019-06-30 06:40:01 -05:00
|
|
|
hir::db::EnumDataQuery
|
2020-01-29 09:10:46 -06:00
|
|
|
hir::db::ImplDataQuery
|
2019-06-30 06:40:01 -05:00
|
|
|
hir::db::TraitDataQuery
|
|
|
|
hir::db::TypeAliasDataQuery
|
2020-01-29 09:10:46 -06:00
|
|
|
hir::db::FunctionDataQuery
|
2019-06-30 06:40:01 -05:00
|
|
|
hir::db::ConstDataQuery
|
|
|
|
hir::db::StaticDataQuery
|
2020-01-29 09:10:46 -06:00
|
|
|
hir::db::BodyWithSourceMapQuery
|
|
|
|
hir::db::BodyQuery
|
|
|
|
hir::db::ExprScopesQuery
|
|
|
|
hir::db::GenericParamsQuery
|
|
|
|
hir::db::AttrsQuery
|
2019-06-30 06:40:01 -05:00
|
|
|
hir::db::ModuleLangItemsQuery
|
2019-09-25 13:01:02 -05:00
|
|
|
hir::db::CrateLangItemsQuery
|
2019-06-30 06:40:01 -05:00
|
|
|
hir::db::LangItemQuery
|
|
|
|
hir::db::DocumentationQuery
|
2020-06-05 06:10:43 -05:00
|
|
|
hir::db::ImportMapQuery
|
2020-01-29 09:10:46 -06:00
|
|
|
|
|
|
|
// HirDatabase
|
2020-03-06 17:11:52 -06:00
|
|
|
hir::db::InferQueryQuery
|
2019-11-26 12:04:24 -06:00
|
|
|
hir::db::TyQuery
|
|
|
|
hir::db::ValueTyQuery
|
2020-01-29 09:10:46 -06:00
|
|
|
hir::db::ImplSelfTyQuery
|
|
|
|
hir::db::ImplTraitQuery
|
2019-11-24 14:48:39 -06:00
|
|
|
hir::db::FieldTypesQuery
|
2019-06-30 06:40:01 -05:00
|
|
|
hir::db::CallableItemSignatureQuery
|
2020-01-29 09:10:46 -06:00
|
|
|
hir::db::GenericPredicatesForParamQuery
|
2019-06-30 06:40:01 -05:00
|
|
|
hir::db::GenericPredicatesQuery
|
|
|
|
hir::db::GenericDefaultsQuery
|
2020-07-01 10:15:20 -05:00
|
|
|
hir::db::InherentImplsInCrateQuery
|
|
|
|
hir::db::TraitImplsInCrateQuery
|
|
|
|
hir::db::TraitImplsInDepsQuery
|
2019-06-30 06:40:01 -05:00
|
|
|
hir::db::AssociatedTyDataQuery
|
2020-07-22 11:44:40 -05:00
|
|
|
hir::db::AssociatedTyDataQuery
|
2019-06-30 06:40:01 -05:00
|
|
|
hir::db::TraitDatumQuery
|
|
|
|
hir::db::StructDatumQuery
|
|
|
|
hir::db::ImplDatumQuery
|
2020-07-22 11:44:40 -05:00
|
|
|
hir::db::FnDefDatumQuery
|
|
|
|
hir::db::ReturnTypeImplTraitsQuery
|
|
|
|
hir::db::InternCallableDefQuery
|
|
|
|
hir::db::InternTypeParamIdQuery
|
|
|
|
hir::db::InternImplTraitIdQuery
|
|
|
|
hir::db::InternClosureQuery
|
2020-03-25 12:41:46 -05:00
|
|
|
hir::db::AssociatedTyValueQuery
|
|
|
|
hir::db::TraitSolveQuery
|
|
|
|
|
|
|
|
// SymbolsDatabase
|
|
|
|
crate::symbol_index::FileSymbolsQuery
|
2020-07-22 11:44:40 -05:00
|
|
|
crate::symbol_index::LibrarySymbolsQuery
|
|
|
|
crate::symbol_index::LocalRootsQuery
|
|
|
|
crate::symbol_index::LibraryRootsQuery
|
2020-03-25 12:41:46 -05:00
|
|
|
|
|
|
|
// LineIndexDatabase
|
|
|
|
crate::LineIndexQuery
|
2019-06-30 06:40:01 -05:00
|
|
|
];
|
2020-07-03 10:12:29 -05: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 03:14:48 -05:00
|
|
|
self.salsa_runtime_mut().synthetic_write(Durability::HIGH);
|
2020-07-03 10:12:29 -05: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 06:40:01 -05:00
|
|
|
acc.sort_by_key(|it| std::cmp::Reverse(it.1));
|
|
|
|
acc
|
|
|
|
}
|
2019-02-08 02:52:18 -06:00
|
|
|
}
|
2019-06-26 01:12:46 -05:00
|
|
|
|
|
|
|
fn durability(source_root: &SourceRoot) -> Durability {
|
|
|
|
if source_root.is_library {
|
|
|
|
Durability::HIGH
|
|
|
|
} else {
|
|
|
|
Durability::LOW
|
|
|
|
}
|
|
|
|
}
|