2019-07-04 15:05:17 -05:00
|
|
|
use std::{fmt, sync::Arc, time};
|
2019-02-08 02:52:18 -06:00
|
|
|
|
|
|
|
use ra_db::{
|
2019-06-26 01:12:46 -05:00
|
|
|
salsa::{Database, Durability, SweepStrategy},
|
2019-07-04 15:05:17 -05:00
|
|
|
CrateGraph, FileId, SourceDatabase, SourceRoot, SourceRootId,
|
2019-02-08 02:52:18 -06:00
|
|
|
};
|
2019-07-04 15:05:17 -05:00
|
|
|
use ra_prof::{memory_usage, profile, Bytes};
|
2019-02-08 02:52:18 -06:00
|
|
|
use ra_syntax::SourceFile;
|
|
|
|
use rayon::prelude::*;
|
2019-07-04 15:05:17 -05:00
|
|
|
use relative_path::RelativePathBuf;
|
|
|
|
use rustc_hash::FxHashMap;
|
2019-02-08 02:52:18 -06:00
|
|
|
|
|
|
|
use crate::{
|
|
|
|
db::RootDatabase,
|
|
|
|
status::syntax_tree_stats,
|
2019-07-04 15:05:17 -05:00
|
|
|
symbol_index::{SymbolIndex, SymbolsDatabase},
|
2019-02-08 02:52:18 -06:00
|
|
|
};
|
|
|
|
|
|
|
|
#[derive(Default)]
|
|
|
|
pub struct AnalysisChange {
|
|
|
|
new_roots: Vec<(SourceRootId, bool)>,
|
|
|
|
roots_changed: FxHashMap<SourceRootId, RootChange>,
|
|
|
|
files_changed: Vec<(FileId, Arc<String>)>,
|
|
|
|
libraries_added: Vec<LibraryData>,
|
|
|
|
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.libraries_added.is_empty() {
|
|
|
|
d.field("libraries_added", &self.libraries_added.len());
|
|
|
|
}
|
|
|
|
if !self.crate_graph.is_some() {
|
|
|
|
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 05:49:43 -06:00
|
|
|
let file = AddFile { file_id, path, text };
|
|
|
|
self.roots_changed.entry(root_id).or_default().added.push(file);
|
2019-02-08 02:52:18 -06: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 05:49:43 -06:00
|
|
|
self.roots_changed.entry(root_id).or_default().removed.push(file);
|
2019-02-08 02:52:18 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn add_library(&mut self, data: LibraryData) {
|
|
|
|
self.libraries_added.push(data)
|
|
|
|
}
|
|
|
|
|
|
|
|
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()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub struct LibraryData {
|
|
|
|
root_id: SourceRootId,
|
|
|
|
root_change: RootChange,
|
|
|
|
symbol_index: SymbolIndex,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl fmt::Debug for LibraryData {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
|
|
|
f.debug_struct("LibraryData")
|
|
|
|
.field("root_id", &self.root_id)
|
|
|
|
.field("root_change", &self.root_change)
|
|
|
|
.field("n_symbols", &self.symbol_index.len())
|
|
|
|
.finish()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl LibraryData {
|
|
|
|
pub fn prepare(
|
|
|
|
root_id: SourceRootId,
|
|
|
|
files: Vec<(FileId, RelativePathBuf, Arc<String>)>,
|
|
|
|
) -> LibraryData {
|
|
|
|
let symbol_index = SymbolIndex::for_files(files.par_iter().map(|(file_id, _, text)| {
|
2019-07-12 11:41:13 -05:00
|
|
|
let parse = SourceFile::parse(text);
|
|
|
|
(*file_id, parse)
|
2019-02-08 02:52:18 -06:00
|
|
|
}));
|
|
|
|
let mut root_change = RootChange::default();
|
|
|
|
root_change.added = files
|
|
|
|
.into_iter()
|
2019-02-08 05:49:43 -06:00
|
|
|
.map(|(file_id, path, text)| AddFile { file_id, path, text })
|
2019-02-08 02:52:18 -06:00
|
|
|
.collect();
|
2019-02-08 05:49:43 -06:00
|
|
|
LibraryData { root_id, root_change, symbol_index }
|
2019-02-08 02:52:18 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const GC_COOLDOWN: time::Duration = time::Duration::from_millis(100);
|
|
|
|
|
|
|
|
impl RootDatabase {
|
|
|
|
pub(crate) fn apply_change(&mut self, change: AnalysisChange) {
|
2019-04-14 15:28:10 -05:00
|
|
|
let _p = profile("RootDatabase::apply_change");
|
2019-02-08 02:52:18 -06:00
|
|
|
log::info!("apply_change {:?}", change);
|
2019-05-27 06:27:05 -05:00
|
|
|
{
|
|
|
|
let _p = profile("RootDatabase::apply_change/cancellation");
|
2019-06-26 01:12:46 -05:00
|
|
|
self.salsa_runtime().synthetic_write(Durability::LOW);
|
2019-05-27 06:27:05 -05:00
|
|
|
}
|
2019-02-08 02:52:18 -06:00
|
|
|
if !change.new_roots.is_empty() {
|
|
|
|
let mut local_roots = Vec::clone(&self.local_roots());
|
|
|
|
for (root_id, is_local) in change.new_roots {
|
2019-06-24 04:35:07 -05:00
|
|
|
let root = if is_local { SourceRoot::new() } else { SourceRoot::new_library() };
|
2019-06-26 01:12:46 -05:00
|
|
|
let durability = durability(&root);
|
|
|
|
self.set_source_root_with_durability(root_id, Arc::new(root), durability);
|
2019-02-08 02:52:18 -06:00
|
|
|
if is_local {
|
|
|
|
local_roots.push(root_id);
|
|
|
|
}
|
|
|
|
}
|
2019-06-26 01:12:46 -05:00
|
|
|
self.set_local_roots_with_durability(Arc::new(local_roots), Durability::HIGH);
|
2019-02-08 02:52:18 -06: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 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);
|
|
|
|
self.set_file_text_with_durability(file_id, text, durability)
|
2019-02-08 02:52:18 -06:00
|
|
|
}
|
|
|
|
if !change.libraries_added.is_empty() {
|
|
|
|
let mut libraries = Vec::clone(&self.library_roots());
|
|
|
|
for library in change.libraries_added {
|
|
|
|
libraries.push(library.root_id);
|
2019-06-26 01:12:46 -05:00
|
|
|
self.set_source_root_with_durability(
|
|
|
|
library.root_id,
|
|
|
|
Default::default(),
|
|
|
|
Durability::HIGH,
|
|
|
|
);
|
|
|
|
self.set_library_symbols_with_durability(
|
|
|
|
library.root_id,
|
|
|
|
Arc::new(library.symbol_index),
|
|
|
|
Durability::HIGH,
|
|
|
|
);
|
2019-02-08 02:52:18 -06:00
|
|
|
self.apply_root_change(library.root_id, library.root_change);
|
|
|
|
}
|
2019-06-26 01:12:46 -05:00
|
|
|
self.set_library_roots_with_durability(Arc::new(libraries), Durability::HIGH);
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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 01:12:46 -05:00
|
|
|
let durability = durability(&source_root);
|
2019-02-08 02:52:18 -06:00
|
|
|
for add_file in root_change.added {
|
2019-06-26 01:12:46 -05: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-02-08 02:52:18 -06:00
|
|
|
source_root.files.insert(add_file.path, add_file.file_id);
|
|
|
|
}
|
|
|
|
for remove_file in root_change.removed {
|
2019-06-26 01:12:46 -05:00
|
|
|
self.set_file_text_with_durability(remove_file.file_id, Default::default(), durability);
|
2019-02-08 02:52:18 -06:00
|
|
|
source_root.files.remove(&remove_file.path);
|
|
|
|
}
|
2019-06-26 01:12:46 -05:00
|
|
|
self.set_source_root_with_durability(root_id, Arc::new(source_root), durability);
|
2019-02-08 02:52:18 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
pub(crate) fn maybe_collect_garbage(&mut self) {
|
|
|
|
if self.last_gc_check.elapsed() > GC_COOLDOWN {
|
|
|
|
self.last_gc_check = time::Instant::now();
|
|
|
|
let retained_trees = syntax_tree_stats(self).retained;
|
|
|
|
if retained_trees > 100 {
|
2019-02-08 05:49:43 -06:00
|
|
|
log::info!("automatic garbadge collection, {} retained trees", retained_trees);
|
2019-02-08 02:52:18 -06:00
|
|
|
self.collect_garbage();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub(crate) fn collect_garbage(&mut self) {
|
2019-05-04 07:29:35 -05:00
|
|
|
let _p = profile("RootDatabase::collect_garbage");
|
2019-02-08 02:52:18 -06:00
|
|
|
self.last_gc = time::Instant::now();
|
|
|
|
|
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
|
|
|
|
|
|
|
self.query(ra_db::ParseQuery).sweep(sweep);
|
2019-06-02 12:15:10 -05:00
|
|
|
self.query(hir::db::ParseMacroQuery).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);
|
|
|
|
// self.query(hir::db::MacroArgQuery).sweep(sweep);
|
|
|
|
// self.query(hir::db::MacroExpandQuery).sweep(sweep);
|
|
|
|
|
2019-03-26 11:00:11 -05:00
|
|
|
self.query(hir::db::AstIdMapQuery).sweep(sweep);
|
2019-02-08 02:52:18 -06:00
|
|
|
|
2019-03-14 05:14:54 -05:00
|
|
|
self.query(hir::db::RawItemsWithSourceMapQuery).sweep(sweep);
|
2019-06-01 14:31:22 -05:00
|
|
|
self.query(hir::db::ImplsInModuleWithSourceMapQuery).sweep(sweep);
|
2019-03-02 07:18:40 -06:00
|
|
|
self.query(hir::db::BodyWithSourceMapQuery).sweep(sweep);
|
2019-06-01 14:47:20 -05:00
|
|
|
|
|
|
|
self.query(hir::db::ExprScopesQuery).sweep(sweep);
|
|
|
|
self.query(hir::db::InferQuery).sweep(sweep);
|
|
|
|
self.query(hir::db::BodyHirQuery).sweep(sweep);
|
2019-02-08 02:52:18 -06:00
|
|
|
}
|
2019-06-30 06:40:01 -05:00
|
|
|
|
|
|
|
pub(crate) fn per_query_memory_usage(&mut self) -> Vec<(String, Bytes)> {
|
|
|
|
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));
|
2019-07-12 10:04:48 -05:00
|
|
|
|
|
|
|
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 06:40:01 -05:00
|
|
|
)*}
|
|
|
|
}
|
|
|
|
sweep_each_query![
|
|
|
|
ra_db::ParseQuery
|
|
|
|
ra_db::SourceRootCratesQuery
|
|
|
|
hir::db::AstIdMapQuery
|
|
|
|
hir::db::ParseMacroQuery
|
|
|
|
hir::db::MacroDefQuery
|
|
|
|
hir::db::MacroArgQuery
|
|
|
|
hir::db::MacroExpandQuery
|
|
|
|
hir::db::StructDataQuery
|
|
|
|
hir::db::EnumDataQuery
|
|
|
|
hir::db::TraitDataQuery
|
|
|
|
hir::db::TraitItemsIndexQuery
|
|
|
|
hir::db::RawItemsWithSourceMapQuery
|
2019-06-30 08:35:40 -05:00
|
|
|
hir::db::RawItemsQuery
|
2019-06-30 06:40:01 -05:00
|
|
|
hir::db::CrateDefMapQuery
|
|
|
|
hir::db::ImplsInModuleWithSourceMapQuery
|
2019-06-30 08:35:40 -05:00
|
|
|
hir::db::ImplsInModuleQuery
|
2019-06-30 06:40:01 -05:00
|
|
|
hir::db::GenericParamsQuery
|
|
|
|
hir::db::FnDataQuery
|
|
|
|
hir::db::TypeAliasDataQuery
|
|
|
|
hir::db::ConstDataQuery
|
|
|
|
hir::db::StaticDataQuery
|
|
|
|
hir::db::ModuleLangItemsQuery
|
|
|
|
hir::db::LangItemsQuery
|
|
|
|
hir::db::LangItemQuery
|
|
|
|
hir::db::DocumentationQuery
|
|
|
|
hir::db::ExprScopesQuery
|
|
|
|
hir::db::InferQuery
|
|
|
|
hir::db::TypeForDefQuery
|
|
|
|
hir::db::TypeForFieldQuery
|
|
|
|
hir::db::CallableItemSignatureQuery
|
|
|
|
hir::db::GenericPredicatesQuery
|
|
|
|
hir::db::GenericDefaultsQuery
|
|
|
|
hir::db::BodyWithSourceMapQuery
|
|
|
|
hir::db::BodyHirQuery
|
|
|
|
hir::db::ImplsInCrateQuery
|
|
|
|
hir::db::ImplsForTraitQuery
|
|
|
|
hir::db::AssociatedTyDataQuery
|
|
|
|
hir::db::TraitDatumQuery
|
|
|
|
hir::db::StructDatumQuery
|
|
|
|
hir::db::ImplDatumQuery
|
2019-07-09 14:34:23 -05:00
|
|
|
hir::db::TraitSolveQuery
|
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
|
|
|
|
}
|
|
|
|
}
|