Skip to content

Commit ccce893

Browse files
committed
Count query entries in memory usage command
1 parent b6fb35f commit ccce893

File tree

5 files changed

+56
-18
lines changed

5 files changed

+56
-18
lines changed

crates/hir-ty/src/display.rs

+11-1
Original file line numberDiff line numberDiff line change
@@ -553,7 +553,17 @@ fn render_const_scalar(
553553
render_const_scalar(f, bytes, memory_map, t)
554554
}
555555
_ => {
556-
let addr = usize::from_le_bytes(b.try_into().unwrap());
556+
let addr = usize::from_le_bytes(match b.try_into() {
557+
Ok(b) => b,
558+
Err(_) => {
559+
never!(
560+
"tried rendering ty {:?} in const ref with incorrect byte count {}",
561+
t,
562+
b.len()
563+
);
564+
return f.write_str("<layout-error>");
565+
}
566+
});
557567
let Ok(layout) = f.db.layout_of_ty(t.clone(), krate) else {
558568
return f.write_str("<layout-error>");
559569
};

crates/ide-db/src/apply_change.rs

+29-5
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
//! Applies changes to the IDE state transactionally.
22
33
use base_db::{
4-
salsa::{Database, Durability},
4+
salsa::{
5+
debug::{DebugQueryTable, TableEntry},
6+
Database, Durability, Query, QueryTable,
7+
},
58
Change, SourceRootId,
69
};
710
use profile::{memory_usage, Bytes};
@@ -47,16 +50,37 @@ impl RootDatabase {
4750
// | VS Code | **rust-analyzer: Memory Usage (Clears Database)**
4851
// |===
4952
// image::https://user-images.githubusercontent.com/48062697/113065592-08559f00-91b1-11eb-8c96-64b88068ec02.gif[]
50-
pub fn per_query_memory_usage(&mut self) -> Vec<(String, Bytes)> {
51-
let mut acc: Vec<(String, Bytes)> = vec![];
53+
pub fn per_query_memory_usage(&mut self) -> Vec<(String, Bytes, usize)> {
54+
let mut acc: Vec<(String, Bytes, usize)> = vec![];
55+
56+
fn collect_query_count<'q, Q>(table: &QueryTable<'q, Q>) -> usize
57+
where
58+
QueryTable<'q, Q>: DebugQueryTable,
59+
Q: Query,
60+
<Q as Query>::Storage: 'q,
61+
{
62+
struct EntryCounter(usize);
63+
impl<K, V> FromIterator<TableEntry<K, V>> for EntryCounter {
64+
fn from_iter<T>(iter: T) -> EntryCounter
65+
where
66+
T: IntoIterator<Item = TableEntry<K, V>>,
67+
{
68+
EntryCounter(iter.into_iter().count())
69+
}
70+
}
71+
table.entries::<EntryCounter>().0
72+
}
73+
5274
macro_rules! purge_each_query {
5375
($($q:path)*) => {$(
5476
let before = memory_usage().allocated;
55-
$q.in_db(self).purge();
77+
let table = $q.in_db(self);
78+
let count = collect_query_count(&table);
79+
table.purge();
5680
let after = memory_usage().allocated;
5781
let q: $q = Default::default();
5882
let name = format!("{:?}", q);
59-
acc.push((name, before - after));
83+
acc.push((name, before - after, count));
6084
)*}
6185
}
6286
purge_each_query![

crates/ide/src/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,7 @@ impl AnalysisHost {
181181
}
182182

183183
/// NB: this clears the database
184-
pub fn per_query_memory_usage(&mut self) -> Vec<(String, profile::Bytes)> {
184+
pub fn per_query_memory_usage(&mut self) -> Vec<(String, profile::Bytes, usize)> {
185185
self.db.per_query_memory_usage()
186186
}
187187
pub fn request_cancellation(&mut self) {

crates/rust-analyzer/src/cli.rs

+10-7
Original file line numberDiff line numberDiff line change
@@ -50,21 +50,24 @@ fn report_metric(metric: &str, value: u64, unit: &str) {
5050
}
5151

5252
fn print_memory_usage(mut host: AnalysisHost, vfs: Vfs) {
53-
let mut mem = host.per_query_memory_usage();
53+
let mem = host.per_query_memory_usage();
5454

5555
let before = profile::memory_usage();
5656
drop(vfs);
5757
let vfs = before.allocated - profile::memory_usage().allocated;
58-
mem.push(("VFS".into(), vfs));
5958

6059
let before = profile::memory_usage();
6160
drop(host);
62-
mem.push(("Unaccounted".into(), before.allocated - profile::memory_usage().allocated));
61+
let unaccounted = before.allocated - profile::memory_usage().allocated;
62+
let remaining = profile::memory_usage().allocated;
6363

64-
mem.push(("Remaining".into(), profile::memory_usage().allocated));
65-
66-
for (name, bytes) in mem {
64+
for (name, bytes, entries) in mem {
6765
// NOTE: Not a debug print, so avoid going through the `eprintln` defined above.
68-
eprintln!("{bytes:>8} {name}");
66+
eprintln!("{bytes:>8} {entries:>6} {name}");
6967
}
68+
eprintln!("{vfs:>8} VFS");
69+
70+
eprintln!("{unaccounted:>8} Unaccounted");
71+
72+
eprintln!("{remaining:>8} Remaining");
7073
}

crates/rust-analyzer/src/handlers/request.rs

+5-4
Original file line numberDiff line numberDiff line change
@@ -115,13 +115,14 @@ pub(crate) fn handle_analyzer_status(
115115

116116
pub(crate) fn handle_memory_usage(state: &mut GlobalState, _: ()) -> Result<String> {
117117
let _p = profile::span("handle_memory_usage");
118-
let mut mem = state.analysis_host.per_query_memory_usage();
119-
mem.push(("Remaining".into(), profile::memory_usage().allocated));
118+
let mem = state.analysis_host.per_query_memory_usage();
120119

121120
let mut out = String::new();
122-
for (name, bytes) in mem {
123-
format_to!(out, "{:>8} {}\n", bytes, name);
121+
for (name, bytes, entries) in mem {
122+
format_to!(out, "{:>8} {:>6} {}\n", bytes, entries, name);
124123
}
124+
format_to!(out, "{:>8} Remaining\n", profile::memory_usage().allocated);
125+
125126
Ok(out)
126127
}
127128

0 commit comments

Comments
 (0)