Skip to content

Commit 9230ae5

Browse files
bors[bot]Veetaha
andauthored
Merge #4148
4148: Simplify profiler impl (bubble up Option) r=matklad a=Veetaha Co-authored-by: veetaha <[email protected]>
2 parents fdaa5e7 + 24d18d9 commit 9230ae5

File tree

1 file changed

+21
-24
lines changed

1 file changed

+21
-24
lines changed

crates/ra_prof/src/hprof.rs

Lines changed: 21 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,9 @@ pub fn init_from(spec: &str) {
3030
pub type Label = &'static str;
3131

3232
/// This function starts a profiling scope in the current execution stack with a given description.
33-
/// It returns a Profile structure and measure elapsed time between this method invocation and Profile structure drop.
34-
/// It supports nested profiling scopes in case when this function invoked multiple times at the execution stack. In this case the profiling information will be nested at the output.
33+
/// It returns a `Profile` struct that measures elapsed time between this method invocation and `Profile` struct drop.
34+
/// It supports nested profiling scopes in case when this function is invoked multiple times at the execution stack.
35+
/// In this case the profiling information will be nested at the output.
3536
/// Profiling information is being printed in the stderr.
3637
///
3738
/// # Example
@@ -58,36 +59,35 @@ pub type Label = &'static str;
5859
/// ```
5960
pub fn profile(label: Label) -> Profiler {
6061
assert!(!label.is_empty());
61-
let enabled = PROFILING_ENABLED.load(Ordering::Relaxed)
62-
&& PROFILE_STACK.with(|stack| stack.borrow_mut().push(label));
63-
let label = if enabled { Some(label) } else { None };
64-
Profiler { label, detail: None }
62+
63+
if PROFILING_ENABLED.load(Ordering::Relaxed)
64+
&& PROFILE_STACK.with(|stack| stack.borrow_mut().push(label))
65+
{
66+
Profiler(Some(ProfilerImpl { label, detail: None }))
67+
} else {
68+
Profiler(None)
69+
}
6570
}
6671

67-
pub struct Profiler {
68-
label: Option<Label>,
72+
pub struct Profiler(Option<ProfilerImpl>);
73+
74+
struct ProfilerImpl {
75+
label: Label,
6976
detail: Option<String>,
7077
}
7178

7279
impl Profiler {
7380
pub fn detail(mut self, detail: impl FnOnce() -> String) -> Profiler {
74-
if self.label.is_some() {
75-
self.detail = Some(detail())
81+
if let Some(profiler) = &mut self.0 {
82+
profiler.detail = Some(detail())
7683
}
7784
self
7885
}
7986
}
8087

81-
impl Drop for Profiler {
88+
impl Drop for ProfilerImpl {
8289
fn drop(&mut self) {
83-
match self {
84-
Profiler { label: Some(label), detail } => {
85-
PROFILE_STACK.with(|stack| {
86-
stack.borrow_mut().pop(label, detail.take());
87-
});
88-
}
89-
Profiler { label: None, .. } => (),
90-
}
90+
PROFILE_STACK.with(|it| it.borrow_mut().pop(self.label, self.detail.take()));
9191
}
9292
}
9393

@@ -179,21 +179,18 @@ impl ProfileStack {
179179
pub fn pop(&mut self, label: Label, detail: Option<String>) {
180180
let start = self.starts.pop().unwrap();
181181
let duration = start.elapsed();
182-
let level = self.starts.len();
183182
self.messages.finish(Message { duration, label, detail });
184-
if level == 0 {
183+
if self.starts.is_empty() {
185184
let longer_than = self.filter.longer_than;
186185
// Convert to millis for comparison to avoid problems with rounding
187186
// (otherwise we could print `0ms` despite user's `>0` filter when
188187
// `duration` is just a few nanos).
189188
if duration.as_millis() > longer_than.as_millis() {
190-
let stderr = stderr();
191189
if let Some(root) = self.messages.root() {
192-
print(&self.messages, root, 0, longer_than, &mut stderr.lock());
190+
print(&self.messages, root, 0, longer_than, &mut stderr().lock());
193191
}
194192
}
195193
self.messages.clear();
196-
assert!(self.starts.is_empty())
197194
}
198195
}
199196
}

0 commit comments

Comments
 (0)