Skip to content

Commit 79298b9

Browse files
bors[bot]matklad
andcommitted
Merge #1463
1463: print memory usage for queries r=matklad a=matklad Co-authored-by: Aleksey Kladov <[email protected]>
2 parents 2ad8220 + d70520e commit 79298b9

File tree

7 files changed

+101
-13
lines changed

7 files changed

+101
-13
lines changed

crates/ra_cli/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,4 @@ ra_db = { path = "../ra_db" }
2020
[dependencies.ra_prof]
2121
path = "../ra_prof"
2222
# features = [ "cpuprofiler" ]
23+
# features = [ "jemalloc" ]

crates/ra_cli/src/analysis_stats.rs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@ use ra_syntax::AstNode;
66

77
use crate::Result;
88

9-
pub fn run(verbose: bool, path: &Path, only: Option<&str>) -> Result<()> {
9+
pub fn run(verbose: bool, memory_usage: bool, path: &Path, only: Option<&str>) -> Result<()> {
1010
let db_load_time = Instant::now();
11-
let (host, roots) = ra_batch::load_cargo(path)?;
11+
let (mut host, roots) = ra_batch::load_cargo(path)?;
1212
let db = host.raw_database();
1313
println!("Database loaded, {} roots, {:?}", roots.len(), db_load_time.elapsed());
1414
let analysis_time = Instant::now();
@@ -113,5 +113,12 @@ pub fn run(verbose: bool, path: &Path, only: Option<&str>) -> Result<()> {
113113
(num_exprs_partially_unknown * 100 / num_exprs)
114114
);
115115
println!("Analysis: {:?}, {}", analysis_time.elapsed(), ra_prof::memory_usage());
116+
117+
if memory_usage {
118+
for (name, bytes) in host.per_query_memory_usage() {
119+
println!("{:>8} {}", bytes, name)
120+
}
121+
}
122+
116123
Ok(())
117124
}

crates/ra_cli/src/main.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ fn main() -> Result<()> {
2424
.subcommand(
2525
SubCommand::with_name("analysis-stats")
2626
.arg(Arg::with_name("verbose").short("v").long("verbose"))
27+
.arg(Arg::with_name("memory-usage").long("memory-usage"))
2728
.arg(Arg::with_name("only").short("o").takes_value(true))
2829
.arg(Arg::with_name("path")),
2930
)
@@ -71,9 +72,10 @@ fn main() -> Result<()> {
7172
}
7273
("analysis-stats", Some(matches)) => {
7374
let verbose = matches.is_present("verbose");
75+
let memory_usage = matches.is_present("memory-usage");
7476
let path = matches.value_of("path").unwrap_or("");
7577
let only = matches.value_of("only");
76-
analysis_stats::run(verbose, path.as_ref(), only)?;
78+
analysis_stats::run(verbose, memory_usage, path.as_ref(), only)?;
7779
}
7880
("analysis-bench", Some(matches)) => {
7981
let verbose = matches.is_present("verbose");

crates/ra_hir/src/db.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,17 +57,21 @@ pub trait InternDatabase: SourceDatabase {
5757
pub trait AstDatabase: InternDatabase {
5858
#[salsa::invoke(crate::source_id::AstIdMap::ast_id_map_query)]
5959
fn ast_id_map(&self, file_id: HirFileId) -> Arc<AstIdMap>;
60+
6061
#[salsa::transparent]
6162
#[salsa::invoke(crate::source_id::AstIdMap::file_item_query)]
6263
fn ast_id_to_node(&self, file_id: HirFileId, ast_id: ErasedFileAstId) -> TreeArc<SyntaxNode>;
64+
6365
#[salsa::transparent]
6466
#[salsa::invoke(crate::ids::HirFileId::parse_or_expand_query)]
6567
fn parse_or_expand(&self, file_id: HirFileId) -> Option<TreeArc<SyntaxNode>>;
68+
6669
#[salsa::invoke(crate::ids::HirFileId::parse_macro_query)]
6770
fn parse_macro(&self, macro_file: ids::MacroFile) -> Option<TreeArc<SyntaxNode>>;
6871

6972
#[salsa::invoke(crate::ids::macro_def_query)]
7073
fn macro_def(&self, macro_id: MacroDefId) -> Option<Arc<mbe::MacroRules>>;
74+
7175
#[salsa::invoke(crate::ids::macro_arg_query)]
7276
fn macro_arg(&self, macro_call: ids::MacroCallId) -> Option<Arc<tt::Subtree>>;
7377

crates/ra_ide_api/src/change.rs

Lines changed: 62 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use ra_db::{
99
salsa::{Database, SweepStrategy},
1010
};
1111
use ra_syntax::SourceFile;
12-
use ra_prof::profile;
12+
use ra_prof::{profile, Bytes, memory_usage};
1313
use relative_path::RelativePathBuf;
1414
use rayon::prelude::*;
1515

@@ -243,4 +243,65 @@ impl RootDatabase {
243243
self.query(hir::db::InferQuery).sweep(sweep);
244244
self.query(hir::db::BodyHirQuery).sweep(sweep);
245245
}
246+
247+
pub(crate) fn per_query_memory_usage(&mut self) -> Vec<(String, Bytes)> {
248+
let mut acc: Vec<(String, Bytes)> = vec![];
249+
let sweep = SweepStrategy::default().discard_values().sweep_all_revisions();
250+
macro_rules! sweep_each_query {
251+
($($q:path)*) => {$(
252+
let before = memory_usage().allocated;
253+
self.query($q).sweep(sweep);
254+
let after = memory_usage().allocated;
255+
let q: $q = Default::default();
256+
let name = format!("{:?}", q);
257+
acc.push((name, before - after));
258+
)*}
259+
}
260+
sweep_each_query![
261+
ra_db::ParseQuery
262+
ra_db::SourceRootCratesQuery
263+
hir::db::AstIdMapQuery
264+
hir::db::ParseMacroQuery
265+
hir::db::MacroDefQuery
266+
hir::db::MacroArgQuery
267+
hir::db::MacroExpandQuery
268+
hir::db::StructDataQuery
269+
hir::db::EnumDataQuery
270+
hir::db::TraitDataQuery
271+
hir::db::TraitItemsIndexQuery
272+
hir::db::RawItemsQuery
273+
hir::db::RawItemsWithSourceMapQuery
274+
hir::db::CrateDefMapQuery
275+
hir::db::ImplsInModuleQuery
276+
hir::db::ImplsInModuleWithSourceMapQuery
277+
hir::db::GenericParamsQuery
278+
hir::db::FnDataQuery
279+
hir::db::TypeAliasDataQuery
280+
hir::db::ConstDataQuery
281+
hir::db::StaticDataQuery
282+
hir::db::ModuleLangItemsQuery
283+
hir::db::LangItemsQuery
284+
hir::db::LangItemQuery
285+
hir::db::DocumentationQuery
286+
hir::db::ExprScopesQuery
287+
hir::db::InferQuery
288+
hir::db::TypeForDefQuery
289+
hir::db::TypeForFieldQuery
290+
hir::db::CallableItemSignatureQuery
291+
hir::db::GenericPredicatesQuery
292+
hir::db::GenericDefaultsQuery
293+
hir::db::BodyWithSourceMapQuery
294+
hir::db::BodyHirQuery
295+
hir::db::ImplsInCrateQuery
296+
hir::db::ImplsForTraitQuery
297+
hir::db::AssociatedTyDataQuery
298+
hir::db::TraitDatumQuery
299+
hir::db::StructDatumQuery
300+
hir::db::ImplDatumQuery
301+
hir::db::ImplementsQuery
302+
hir::db::NormalizeQuery
303+
];
304+
acc.sort_by_key(|it| std::cmp::Reverse(it.1));
305+
acc
306+
}
246307
}

crates/ra_ide_api/src/lib.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -270,6 +270,10 @@ impl AnalysisHost {
270270
pub fn collect_garbage(&mut self) {
271271
self.db.collect_garbage();
272272
}
273+
/// NB: this clears the database
274+
pub fn per_query_memory_usage(&mut self) -> Vec<(String, ra_prof::Bytes)> {
275+
self.db.per_query_memory_usage()
276+
}
273277
pub fn raw_database(&self) -> &(impl hir::db::HirDatabase + salsa::Database) {
274278
&self.db
275279
}

crates/ra_prof/src/memory_usage.rs

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -27,21 +27,23 @@ impl fmt::Display for MemoryUsage {
2727
}
2828
}
2929

30-
#[derive(Default)]
30+
#[derive(Default, PartialEq, Eq, PartialOrd, Ord, Hash, Clone, Copy)]
3131
pub struct Bytes(usize);
3232

3333
impl fmt::Display for Bytes {
3434
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
3535
let bytes = self.0;
36-
if bytes < 4096 {
37-
return write!(f, "{} bytes", bytes);
36+
let mut value = bytes;
37+
let mut suffix = "b";
38+
if value > 4096 {
39+
value /= 1024;
40+
suffix = "kb";
41+
if value > 4096 {
42+
value /= 1024;
43+
suffix = "mb";
44+
}
3845
}
39-
let kb = bytes / 1024;
40-
if kb < 4096 {
41-
return write!(f, "{}kb", kb);
42-
}
43-
let mb = kb / 1024;
44-
write!(f, "{}mb", mb)
46+
f.pad(&format!("{}{}", value, suffix))
4547
}
4648
}
4749

@@ -50,3 +52,10 @@ impl std::ops::AddAssign<usize> for Bytes {
5052
self.0 += x;
5153
}
5254
}
55+
56+
impl std::ops::Sub for Bytes {
57+
type Output = Bytes;
58+
fn sub(self, rhs: Bytes) -> Bytes {
59+
Bytes(self.0 - rhs.0)
60+
}
61+
}

0 commit comments

Comments
 (0)