aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_ide_api
diff options
context:
space:
mode:
authorbors[bot] <bors[bot]@users.noreply.github.com>2019-01-28 11:39:02 +0000
committerbors[bot] <bors[bot]@users.noreply.github.com>2019-01-28 11:39:02 +0000
commit7abe1f422c1a1230ad5b39474101806c438ef452 (patch)
tree2da2a92660b4410e91cb14c0dc3b9bbacdef08ef /crates/ra_ide_api
parent6e36d3f6537f80071e7ed8ff45ca2271a9ffe7a7 (diff)
parent53e3e82d919e36cc2225d4f7f867a45f0cede53a (diff)
Merge #678
678: WIP: automatically collect garbage r=matklad a=matklad Co-authored-by: Aleksey Kladov <[email protected]>
Diffstat (limited to 'crates/ra_ide_api')
-rw-r--r--crates/ra_ide_api/Cargo.toml2
-rw-r--r--crates/ra_ide_api/src/db.rs11
-rw-r--r--crates/ra_ide_api/src/imp.rs49
-rw-r--r--crates/ra_ide_api/src/lib.rs9
-rw-r--r--crates/ra_ide_api/src/status.rs44
5 files changed, 96 insertions, 19 deletions
diff --git a/crates/ra_ide_api/Cargo.toml b/crates/ra_ide_api/Cargo.toml
index 79e473463..ad9dd2088 100644
--- a/crates/ra_ide_api/Cargo.toml
+++ b/crates/ra_ide_api/Cargo.toml
@@ -14,6 +14,8 @@ fst = "0.3.1"
14rustc-hash = "1.0" 14rustc-hash = "1.0"
15parking_lot = "0.7.0" 15parking_lot = "0.7.0"
16unicase = "2.2.0" 16unicase = "2.2.0"
17jemallocator = "0.1.9"
18jemalloc-ctl = "0.2.0"
17 19
18ra_syntax = { path = "../ra_syntax" } 20ra_syntax = { path = "../ra_syntax" }
19ra_ide_api_light = { path = "../ra_ide_api_light" } 21ra_ide_api_light = { path = "../ra_ide_api_light" }
diff --git a/crates/ra_ide_api/src/db.rs b/crates/ra_ide_api/src/db.rs
index 3da93ec35..6850811d7 100644
--- a/crates/ra_ide_api/src/db.rs
+++ b/crates/ra_ide_api/src/db.rs
@@ -1,4 +1,7 @@
1use std::sync::Arc; 1use std::{
2 sync::Arc,
3 time,
4};
2 5
3use ra_db::{ 6use ra_db::{
4 CheckCanceled, FileId, Canceled, SourceDatabase, 7 CheckCanceled, FileId, Canceled, SourceDatabase,
@@ -17,6 +20,8 @@ use crate::{LineIndex, symbol_index::{self, SymbolsDatabase}};
17pub(crate) struct RootDatabase { 20pub(crate) struct RootDatabase {
18 runtime: salsa::Runtime<RootDatabase>, 21 runtime: salsa::Runtime<RootDatabase>,
19 interner: Arc<hir::HirInterner>, 22 interner: Arc<hir::HirInterner>,
23 pub(crate) last_gc: time::Instant,
24 pub(crate) last_gc_check: time::Instant,
20} 25}
21 26
22impl salsa::Database for RootDatabase { 27impl salsa::Database for RootDatabase {
@@ -33,6 +38,8 @@ impl Default for RootDatabase {
33 let mut db = RootDatabase { 38 let mut db = RootDatabase {
34 runtime: salsa::Runtime::default(), 39 runtime: salsa::Runtime::default(),
35 interner: Default::default(), 40 interner: Default::default(),
41 last_gc: time::Instant::now(),
42 last_gc_check: time::Instant::now(),
36 }; 43 };
37 db.set_crate_graph(Default::default()); 44 db.set_crate_graph(Default::default());
38 db.set_local_roots(Default::default()); 45 db.set_local_roots(Default::default());
@@ -46,6 +53,8 @@ impl salsa::ParallelDatabase for RootDatabase {
46 salsa::Snapshot::new(RootDatabase { 53 salsa::Snapshot::new(RootDatabase {
47 runtime: self.runtime.snapshot(self), 54 runtime: self.runtime.snapshot(self),
48 interner: Arc::clone(&self.interner), 55 interner: Arc::clone(&self.interner),
56 last_gc: self.last_gc.clone(),
57 last_gc_check: self.last_gc_check.clone(),
49 }) 58 })
50 } 59 }
51} 60}
diff --git a/crates/ra_ide_api/src/imp.rs b/crates/ra_ide_api/src/imp.rs
index 399433a01..31e0f5d6d 100644
--- a/crates/ra_ide_api/src/imp.rs
+++ b/crates/ra_ide_api/src/imp.rs
@@ -1,4 +1,7 @@
1use std::sync::Arc; 1use std::{
2 sync::Arc,
3 time,
4};
2 5
3use hir::{ 6use hir::{
4 self, Problem, source_binder 7 self, Problem, source_binder
@@ -19,12 +22,14 @@ use crate::{
19 CrateId, db, Diagnostic, FileId, FilePosition, FileRange, FileSystemEdit, 22 CrateId, db, Diagnostic, FileId, FilePosition, FileRange, FileSystemEdit,
20 Query, RootChange, SourceChange, SourceFileEdit, 23 Query, RootChange, SourceChange, SourceFileEdit,
21 symbol_index::{FileSymbol, SymbolsDatabase}, 24 symbol_index::{FileSymbol, SymbolsDatabase},
25 status::syntax_tree_stats
22}; 26};
23 27
28const GC_COOLDOWN: time::Duration = time::Duration::from_millis(100);
29
24impl db::RootDatabase { 30impl db::RootDatabase {
25 pub(crate) fn apply_change(&mut self, change: AnalysisChange) { 31 pub(crate) fn apply_change(&mut self, change: AnalysisChange) {
26 log::info!("apply_change {:?}", change); 32 log::info!("apply_change {:?}", change);
27 // self.gc_syntax_trees();
28 if !change.new_roots.is_empty() { 33 if !change.new_roots.is_empty() {
29 let mut local_roots = Vec::clone(&self.local_roots()); 34 let mut local_roots = Vec::clone(&self.local_roots());
30 for (root_id, is_local) in change.new_roots { 35 for (root_id, is_local) in change.new_roots {
@@ -72,18 +77,36 @@ impl db::RootDatabase {
72 self.set_source_root(root_id, Arc::new(source_root)); 77 self.set_source_root(root_id, Arc::new(source_root));
73 } 78 }
74 79
75 /// Ideally, we should call this function from time to time to collect heavy 80 pub(crate) fn maybe_collect_garbage(&mut self) {
76 /// syntax trees. However, if we actually do that, everything is recomputed 81 if self.last_gc_check.elapsed() > GC_COOLDOWN {
77 /// for some reason. Needs investigation. 82 self.last_gc_check = time::Instant::now();
83 let retained_trees = syntax_tree_stats(self).retained;
84 if retained_trees > 100 {
85 log::info!(
86 "automatic garbadge collection, {} retained trees",
87 retained_trees
88 );
89 self.collect_garbage();
90 }
91 }
92 }
93
78 pub(crate) fn collect_garbage(&mut self) { 94 pub(crate) fn collect_garbage(&mut self) {
79 self.query(ra_db::ParseQuery) 95 self.last_gc = time::Instant::now();
80 .sweep(SweepStrategy::default().discard_values()); 96
81 self.query(hir::db::HirParseQuery) 97 let sweep = SweepStrategy::default()
82 .sweep(SweepStrategy::default().discard_values()); 98 .discard_values()
83 self.query(hir::db::FileItemsQuery) 99 .sweep_all_revisions();
84 .sweep(SweepStrategy::default().discard_values()); 100
85 self.query(hir::db::FileItemQuery) 101 self.query(ra_db::ParseQuery).sweep(sweep);
86 .sweep(SweepStrategy::default().discard_values()); 102
103 self.query(hir::db::HirParseQuery).sweep(sweep);
104 self.query(hir::db::FileItemsQuery).sweep(sweep);
105 self.query(hir::db::FileItemQuery).sweep(sweep);
106
107 self.query(hir::db::LowerModuleQuery).sweep(sweep);
108 self.query(hir::db::LowerModuleSourceMapQuery).sweep(sweep);
109 self.query(hir::db::BodySyntaxMappingQuery).sweep(sweep);
87 } 110 }
88} 111}
89 112
diff --git a/crates/ra_ide_api/src/lib.rs b/crates/ra_ide_api/src/lib.rs
index 43c8bea71..dc531e068 100644
--- a/crates/ra_ide_api/src/lib.rs
+++ b/crates/ra_ide_api/src/lib.rs
@@ -59,6 +59,11 @@ pub use ra_db::{
59 Canceled, CrateGraph, CrateId, FileId, FilePosition, FileRange, SourceRootId 59 Canceled, CrateGraph, CrateId, FileId, FilePosition, FileRange, SourceRootId
60}; 60};
61 61
62// We use jemalloc mainly to get heap usage statistics, actual performance
63// differnece is not measures.
64#[global_allocator]
65static ALLOC: jemallocator::Jemalloc = jemallocator::Jemalloc;
66
62pub type Cancelable<T> = Result<T, Canceled>; 67pub type Cancelable<T> = Result<T, Canceled>;
63 68
64#[derive(Default)] 69#[derive(Default)]
@@ -286,6 +291,10 @@ impl AnalysisHost {
286 self.db.apply_change(change) 291 self.db.apply_change(change)
287 } 292 }
288 293
294 pub fn maybe_collect_garbage(&mut self) {
295 self.db.maybe_collect_garbage();
296 }
297
289 pub fn collect_garbage(&mut self) { 298 pub fn collect_garbage(&mut self) {
290 self.db.collect_garbage(); 299 self.db.collect_garbage();
291 } 300 }
diff --git a/crates/ra_ide_api/src/status.rs b/crates/ra_ide_api/src/status.rs
index e11eed223..c3e5745d5 100644
--- a/crates/ra_ide_api/src/status.rs
+++ b/crates/ra_ide_api/src/status.rs
@@ -15,9 +15,13 @@ use crate::{
15 symbol_index::{SymbolIndex, LibrarySymbolsQuery}, 15 symbol_index::{SymbolIndex, LibrarySymbolsQuery},
16}; 16};
17 17
18pub(crate) fn syntax_tree_stats(db: &RootDatabase) -> SyntaxTreeStats {
19 db.query(ParseQuery).entries::<SyntaxTreeStats>()
20}
21
18pub(crate) fn status(db: &RootDatabase) -> String { 22pub(crate) fn status(db: &RootDatabase) -> String {
19 let files_stats = db.query(FileTextQuery).entries::<FilesStats>(); 23 let files_stats = db.query(FileTextQuery).entries::<FilesStats>();
20 let syntax_tree_stats = db.query(ParseQuery).entries::<SyntaxTreeStats>(); 24 let syntax_tree_stats = syntax_tree_stats(db);
21 let symbols_stats = db 25 let symbols_stats = db
22 .query(LibrarySymbolsQuery) 26 .query(LibrarySymbolsQuery)
23 .entries::<LibrarySymbolsStats>(); 27 .entries::<LibrarySymbolsStats>();
@@ -26,8 +30,13 @@ pub(crate) fn status(db: &RootDatabase) -> String {
26 interner.len() 30 interner.len()
27 }; 31 };
28 format!( 32 format!(
29 "{}\n{}\n{}\nn_defs {}\n", 33 "{}\n{}\n{}\n{} defs\n\nmemory:\n{}\ngc {:?} seconds ago",
30 files_stats, symbols_stats, syntax_tree_stats, n_defs 34 files_stats,
35 symbols_stats,
36 syntax_tree_stats,
37 n_defs,
38 MemoryStats::current(),
39 db.last_gc.elapsed().as_secs(),
31 ) 40 )
32} 41}
33 42
@@ -58,9 +67,9 @@ impl FromIterator<TableEntry<FileId, Arc<String>>> for FilesStats {
58} 67}
59 68
60#[derive(Default)] 69#[derive(Default)]
61struct SyntaxTreeStats { 70pub(crate) struct SyntaxTreeStats {
62 total: usize, 71 total: usize,
63 retained: usize, 72 pub(crate) retained: usize,
64 retained_size: Bytes, 73 retained_size: Bytes,
65} 74}
66 75
@@ -118,6 +127,31 @@ impl FromIterator<TableEntry<SourceRootId, Arc<SymbolIndex>>> for LibrarySymbols
118 } 127 }
119} 128}
120 129
130struct MemoryStats {
131 allocated: Bytes,
132 resident: Bytes,
133}
134
135impl MemoryStats {
136 fn current() -> MemoryStats {
137 jemalloc_ctl::epoch().unwrap();
138 MemoryStats {
139 allocated: Bytes(jemalloc_ctl::stats::allocated().unwrap()),
140 resident: Bytes(jemalloc_ctl::stats::resident().unwrap()),
141 }
142 }
143}
144
145impl fmt::Display for MemoryStats {
146 fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
147 write!(
148 fmt,
149 "{} allocated {} resident",
150 self.allocated, self.resident,
151 )
152 }
153}
154
121#[derive(Default)] 155#[derive(Default)]
122struct Bytes(usize); 156struct Bytes(usize);
123 157