Skip to content

Commit ce7496e

Browse files
bors[bot]matklad
andauthored
Merge #3497
3497: Allow specifying additional info on call to profile r=matklad a=matklad bors r+ 🤖 Co-authored-by: Aleksey Kladov <aleksey.kladov@gmail.com>
2 parents 1901794 + b33b843 commit ce7496e

File tree

3 files changed

+52
-21
lines changed

3 files changed

+52
-21
lines changed

crates/ra_hir_ty/src/db.rs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ use crate::{
1616
Binders, CallableDef, GenericPredicate, InferenceResult, PolyFnSig, Substs, TraitRef, Ty,
1717
TyDefId, TypeCtor, ValueTyDefId,
1818
};
19+
use hir_expand::name::Name;
1920

2021
#[salsa::query_group(HirDatabaseStorage)]
2122
#[salsa::requires(salsa::Database)]
@@ -111,7 +112,15 @@ pub trait HirDatabase: DefDatabase {
111112
}
112113

113114
fn infer(db: &impl HirDatabase, def: DefWithBodyId) -> Arc<InferenceResult> {
114-
let _p = profile("wait_infer");
115+
let _p = profile("wait_infer").detail(|| match def {
116+
DefWithBodyId::FunctionId(it) => db.function_data(it).name.to_string(),
117+
DefWithBodyId::StaticId(it) => {
118+
db.static_data(it).name.clone().unwrap_or_else(Name::missing).to_string()
119+
}
120+
DefWithBodyId::ConstId(it) => {
121+
db.const_data(it).name.clone().unwrap_or_else(Name::missing).to_string()
122+
}
123+
});
115124
db.do_infer(def)
116125
}
117126

crates/ra_hir_ty/src/traits.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -221,7 +221,10 @@ pub(crate) fn trait_solve_query(
221221
krate: CrateId,
222222
goal: Canonical<InEnvironment<Obligation>>,
223223
) -> Option<Solution> {
224-
let _p = profile("trait_solve_query");
224+
let _p = profile("trait_solve_query").detail(|| match &goal.value.value {
225+
Obligation::Trait(it) => db.trait_data(it.trait_).name.to_string(),
226+
Obligation::Projection(_) => "projection".to_string(),
227+
});
225228
log::debug!("trait_solve_query({})", goal.value.value.display(db));
226229

227230
if let Obligation::Projection(pred) = &goal.value.value {

crates/ra_prof/src/lib.rs

Lines changed: 38 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ pub type Label = &'static str;
8888
pub fn profile(label: Label) -> Profiler {
8989
assert!(!label.is_empty());
9090
if !PROFILING_ENABLED.load(Ordering::Relaxed) {
91-
return Profiler { label: None };
91+
return Profiler { label: None, detail: None };
9292
}
9393

9494
PROFILE_STACK.with(|stack| {
@@ -101,15 +101,15 @@ pub fn profile(label: Label) -> Profiler {
101101
};
102102
}
103103
if stack.starts.len() > stack.filter_data.depth {
104-
return Profiler { label: None };
104+
return Profiler { label: None, detail: None };
105105
}
106106
let allowed = &stack.filter_data.allowed;
107107
if stack.starts.is_empty() && !allowed.is_empty() && !allowed.contains(label) {
108-
return Profiler { label: None };
108+
return Profiler { label: None, detail: None };
109109
}
110110

111111
stack.starts.push(Instant::now());
112-
Profiler { label: Some(label) }
112+
Profiler { label: Some(label), detail: None }
113113
})
114114
}
115115

@@ -130,6 +130,16 @@ pub fn print_time(label: Label) -> impl Drop {
130130

131131
pub struct Profiler {
132132
label: Option<Label>,
133+
detail: Option<String>,
134+
}
135+
136+
impl Profiler {
137+
pub fn detail(mut self, detail: impl FnOnce() -> String) -> Profiler {
138+
if self.label.is_some() {
139+
self.detail = Some(detail())
140+
}
141+
self
142+
}
133143
}
134144

135145
pub struct Filter {
@@ -183,6 +193,7 @@ struct Message {
183193
level: usize,
184194
duration: Duration,
185195
label: Label,
196+
detail: Option<String>,
186197
}
187198

188199
impl ProfileStack {
@@ -208,13 +219,13 @@ thread_local!(static PROFILE_STACK: RefCell<ProfileStack> = RefCell::new(Profile
208219
impl Drop for Profiler {
209220
fn drop(&mut self) {
210221
match self {
211-
Profiler { label: Some(label) } => {
222+
Profiler { label: Some(label), detail } => {
212223
PROFILE_STACK.with(|stack| {
213224
let mut stack = stack.borrow_mut();
214225
let start = stack.starts.pop().unwrap();
215226
let duration = start.elapsed();
216227
let level = stack.starts.len();
217-
stack.messages.push(Message { level, duration, label });
228+
stack.messages.push(Message { level, duration, label, detail: detail.take() });
218229
if level == 0 {
219230
let stdout = stderr();
220231
let longer_than = stack.filter_data.longer_than;
@@ -228,7 +239,7 @@ impl Drop for Profiler {
228239
}
229240
});
230241
}
231-
Profiler { label: None } => (),
242+
Profiler { label: None, .. } => (),
232243
}
233244
}
234245
}
@@ -251,8 +262,16 @@ fn print_for_idx(
251262
) {
252263
let current = &msgs[current_idx];
253264
let current_indent = " ".repeat(current.level);
254-
writeln!(out, "{}{:5}ms - {}", current_indent, current.duration.as_millis(), current.label)
255-
.expect("printing profiling info");
265+
let detail = current.detail.as_ref().map(|it| format!(" @ {}", it)).unwrap_or_default();
266+
writeln!(
267+
out,
268+
"{}{:5}ms - {}{}",
269+
current_indent,
270+
current.duration.as_millis(),
271+
current.label,
272+
detail,
273+
)
274+
.expect("printing profiling info");
256275

257276
let longer_than_millis = longer_than.as_millis();
258277
let children_indices = &children_map[current_idx];
@@ -417,9 +436,9 @@ mod tests {
417436
fn test_longer_than() {
418437
let mut result = vec![];
419438
let msgs = vec![
420-
Message { level: 1, duration: Duration::from_nanos(3), label: "bar" },
421-
Message { level: 1, duration: Duration::from_nanos(2), label: "bar" },
422-
Message { level: 0, duration: Duration::from_millis(1), label: "foo" },
439+
Message { level: 1, duration: Duration::from_nanos(3), label: "bar", detail: None },
440+
Message { level: 1, duration: Duration::from_nanos(2), label: "bar", detail: None },
441+
Message { level: 0, duration: Duration::from_millis(1), label: "foo", detail: None },
423442
];
424443
print(&msgs, Duration::from_millis(0), &mut result);
425444
// The calls to `bar` are so short that they'll be rounded to 0ms and should get collapsed
@@ -434,8 +453,8 @@ mod tests {
434453
fn test_unaccounted_for_topmost() {
435454
let mut result = vec![];
436455
let msgs = vec![
437-
Message { level: 1, duration: Duration::from_millis(2), label: "bar" },
438-
Message { level: 0, duration: Duration::from_millis(5), label: "foo" },
456+
Message { level: 1, duration: Duration::from_millis(2), label: "bar", detail: None },
457+
Message { level: 0, duration: Duration::from_millis(5), label: "foo", detail: None },
439458
];
440459
print(&msgs, Duration::from_millis(0), &mut result);
441460
assert_eq!(
@@ -453,11 +472,11 @@ mod tests {
453472
fn test_unaccounted_for_multiple_levels() {
454473
let mut result = vec![];
455474
let msgs = vec![
456-
Message { level: 2, duration: Duration::from_millis(3), label: "baz" },
457-
Message { level: 1, duration: Duration::from_millis(5), label: "bar" },
458-
Message { level: 2, duration: Duration::from_millis(2), label: "baz" },
459-
Message { level: 1, duration: Duration::from_millis(4), label: "bar" },
460-
Message { level: 0, duration: Duration::from_millis(9), label: "foo" },
475+
Message { level: 2, duration: Duration::from_millis(3), label: "baz", detail: None },
476+
Message { level: 1, duration: Duration::from_millis(5), label: "bar", detail: None },
477+
Message { level: 2, duration: Duration::from_millis(2), label: "baz", detail: None },
478+
Message { level: 1, duration: Duration::from_millis(4), label: "bar", detail: None },
479+
Message { level: 0, duration: Duration::from_millis(9), label: "foo", detail: None },
461480
];
462481
print(&msgs, Duration::from_millis(0), &mut result);
463482
assert_eq!(

0 commit comments

Comments
 (0)