rust-analyzer/crates/ide-db/src/apply_change.rs

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

228 lines
8.2 KiB
Rust
Raw Normal View History

//! Applies changes to the IDE state transactionally.
2020-08-13 14:25:38 +00:00
use base_db::{
salsa::{
debug::{DebugQueryTable, TableEntry},
Database, Durability, Query, QueryTable,
},
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;
2023-05-02 14:12:22 +00:00
use triomphe::Arc;
2019-02-08 08:52:18 +00:00
use crate::{symbol_index::SymbolsDatabase, Change, RootDatabase};
2019-02-08 08:52:18 +00:00
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();
tracing::trace!("apply_change {:?}", change);
if let Some(roots) = &change.source_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
}
// Feature: Memory Usage
//
// Clears rust-analyzer's internal database and prints memory usage statistics.
//
// |===
// | Editor | Action Name
//
2022-08-01 11:47:09 +00:00
// | VS Code | **rust-analyzer: Memory Usage (Clears Database)**
// |===
// image::https://user-images.githubusercontent.com/48062697/113065592-08559f00-91b1-11eb-8c96-64b88068ec02.gif[]
pub fn per_query_memory_usage(&mut self) -> Vec<(String, Bytes, usize)> {
let mut acc: Vec<(String, Bytes, usize)> = vec![];
fn collect_query_count<'q, Q>(table: &QueryTable<'q, Q>) -> usize
where
QueryTable<'q, Q>: DebugQueryTable,
Q: Query,
<Q as Query>::Storage: 'q,
{
struct EntryCounter(usize);
impl<K, V> FromIterator<TableEntry<K, V>> for EntryCounter {
fn from_iter<T>(iter: T) -> EntryCounter
where
T: IntoIterator<Item = TableEntry<K, V>>,
{
EntryCounter(iter.into_iter().count())
}
}
table.entries::<EntryCounter>().0
}
2021-10-06 20:42:54 +00:00
macro_rules! purge_each_query {
2019-06-30 11:40:01 +00:00
($($q:path)*) => {$(
let before = memory_usage().allocated;
let table = $q.in_db(self);
let count = collect_query_count(&table);
table.purge();
let after = memory_usage().allocated;
let q: $q = Default::default();
2021-10-06 20:42:54 +00:00
let name = format!("{:?}", q);
acc.push((name, before - after, count));
2019-06-30 11:40:01 +00:00
)*}
}
2021-10-06 20:42:54 +00:00
purge_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
// ExpandDatabase
2019-06-30 11:40:01 +00:00
hir::db::AstIdMapQuery
2023-11-25 16:10:18 +00:00
hir::db::DeclMacroExpanderQuery
hir::db::ExpandProcMacroQuery
hir::db::InternMacroCallQuery
hir::db::InternSyntaxContextQuery
hir::db::MacroArgQuery
2023-11-25 16:10:18 +00:00
hir::db::ParseMacroExpansionQuery
hir::db::RealSpanMapQuery
hir::db::ProcMacrosQuery
// DefDatabase
hir::db::FileItemTreeQuery
2020-03-06 23:11:52 +00:00
hir::db::CrateDefMapQueryQuery
hir::db::BlockDefMapQuery
2019-06-30 11:40:01 +00:00
hir::db::StructDataQuery
hir::db::StructDataWithDiagnosticsQuery
hir::db::UnionDataQuery
hir::db::UnionDataWithDiagnosticsQuery
2019-06-30 11:40:01 +00:00
hir::db::EnumDataQuery
hir::db::EnumDataWithDiagnosticsQuery
hir::db::ImplDataQuery
hir::db::ImplDataWithDiagnosticsQuery
2019-06-30 11:40:01 +00:00
hir::db::TraitDataQuery
hir::db::TraitDataWithDiagnosticsQuery
hir::db::TraitAliasDataQuery
2019-06-30 11:40:01 +00:00
hir::db::TypeAliasDataQuery
hir::db::FunctionDataQuery
2019-06-30 11:40:01 +00:00
hir::db::ConstDataQuery
hir::db::StaticDataQuery
hir::db::Macro2DataQuery
hir::db::MacroRulesDataQuery
hir::db::ProcMacroDataQuery
hir::db::BodyWithSourceMapQuery
hir::db::BodyQuery
hir::db::ExprScopesQuery
hir::db::GenericParamsQuery
hir::db::VariantsAttrsQuery
hir::db::FieldsAttrsQuery
hir::db::VariantsAttrsSourceMapQuery
hir::db::FieldsAttrsSourceMapQuery
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
hir::db::FieldVisibilitiesQuery
hir::db::FunctionVisibilityQuery
hir::db::ConstVisibilityQuery
hir::db::CrateSupportsNoStdQuery
2023-11-25 16:10:18 +00:00
hir::db::BlockItemTreeQueryQuery
hir::db::ExternCrateDeclDataQuery
hir::db::LangAttrQuery
hir::db::InternAnonymousConstQuery
hir::db::InternExternCrateQuery
hir::db::InternInTypeConstQuery
hir::db::InternUseQuery
// HirDatabase
2020-03-06 23:11:52 +00:00
hir::db::InferQueryQuery
hir::db::MirBodyQuery
hir::db::BorrowckQuery
2019-11-26 18:04:24 +00:00
hir::db::TyQuery
hir::db::ValueTyQuery
hir::db::ImplSelfTyQuery
hir::db::ConstParamTyQuery
hir::db::ConstEvalQuery
hir::db::ConstEvalDiscriminantQuery
hir::db::ImplTraitQuery
hir::db::FieldTypesQuery
hir::db::LayoutOfAdtQuery
hir::db::TargetDataLayoutQuery
2019-06-30 11:40:01 +00:00
hir::db::CallableItemSignatureQuery
hir::db::ReturnTypeImplTraitsQuery
hir::db::GenericPredicatesForParamQuery
2019-06-30 11:40:01 +00:00
hir::db::GenericPredicatesQuery
hir::db::TraitEnvironmentQuery
2019-06-30 11:40:01 +00:00
hir::db::GenericDefaultsQuery
hir::db::InherentImplsInCrateQuery
hir::db::InherentImplsInBlockQuery
hir::db::IncoherentInherentImplCratesQuery
hir::db::TraitImplsInCrateQuery
hir::db::TraitImplsInBlockQuery
hir::db::TraitImplsInDepsQuery
hir::db::InternCallableDefQuery
hir::db::InternLifetimeParamIdQuery
hir::db::InternImplTraitIdQuery
hir::db::InternTypeOrConstParamIdQuery
hir::db::InternClosureQuery
hir::db::InternGeneratorQuery
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::FnDefVarianceQuery
hir::db::AdtVarianceQuery
2020-03-25 17:41:46 +00:00
hir::db::AssociatedTyValueQuery
hir::db::TraitSolveQueryQuery
hir::db::ProgramClausesForChalkEnvQuery
2020-03-25 17:41:46 +00:00
// SymbolsDatabase
2021-11-27 13:00:02 +00:00
crate::symbol_index::ModuleSymbolsQuery
crate::symbol_index::LibrarySymbolsQuery
crate::symbol_index::LocalRootsQuery
crate::symbol_index::LibraryRootsQuery
2020-03-25 17:41:46 +00:00
// LineIndexDatabase
crate::LineIndexQuery
// InternDatabase
hir::db::InternFunctionQuery
hir::db::InternStructQuery
hir::db::InternUnionQuery
hir::db::InternEnumQuery
hir::db::InternConstQuery
hir::db::InternStaticQuery
hir::db::InternTraitQuery
hir::db::InternTraitAliasQuery
hir::db::InternTypeAliasQuery
hir::db::InternImplQuery
hir::db::InternExternBlockQuery
hir::db::InternBlockQuery
hir::db::InternMacro2Query
hir::db::InternProcMacroQuery
hir::db::InternMacroRulesQuery
];
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
}