diff options
author | bors[bot] <26634292+bors[bot]@users.noreply.github.com> | 2020-04-28 19:44:34 +0100 |
---|---|---|
committer | GitHub <[email protected]> | 2020-04-28 19:44:34 +0100 |
commit | 9230ae5492bbfd73483d7f30918c1f686ebe46e9 (patch) | |
tree | 96d43a52891ef5d71dfbdb2812e25d7a1bf847c4 /crates/ra_prof | |
parent | fdaa5e7ccffa04594c52c73234fed7615f378055 (diff) | |
parent | 24d18d92f69c1ce3d4fc3ff3be67d4f9bf65d857 (diff) |
Merge #4148
4148: Simplify profiler impl (bubble up Option) r=matklad a=Veetaha
Co-authored-by: veetaha <[email protected]>
Diffstat (limited to 'crates/ra_prof')
-rw-r--r-- | crates/ra_prof/src/hprof.rs | 45 |
1 files changed, 21 insertions, 24 deletions
diff --git a/crates/ra_prof/src/hprof.rs b/crates/ra_prof/src/hprof.rs index 2b8a90363..a3f5321fb 100644 --- a/crates/ra_prof/src/hprof.rs +++ b/crates/ra_prof/src/hprof.rs | |||
@@ -30,8 +30,9 @@ pub fn init_from(spec: &str) { | |||
30 | pub type Label = &'static str; | 30 | pub type Label = &'static str; |
31 | 31 | ||
32 | /// This function starts a profiling scope in the current execution stack with a given description. | 32 | /// 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. | 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 invoked multiple times at the execution stack. In this case the profiling information will be nested at the output. | 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. | ||
35 | /// Profiling information is being printed in the stderr. | 36 | /// Profiling information is being printed in the stderr. |
36 | /// | 37 | /// |
37 | /// # Example | 38 | /// # Example |
@@ -58,36 +59,35 @@ pub type Label = &'static str; | |||
58 | /// ``` | 59 | /// ``` |
59 | pub fn profile(label: Label) -> Profiler { | 60 | pub fn profile(label: Label) -> Profiler { |
60 | assert!(!label.is_empty()); | 61 | assert!(!label.is_empty()); |
61 | let enabled = PROFILING_ENABLED.load(Ordering::Relaxed) | 62 | |
62 | && PROFILE_STACK.with(|stack| stack.borrow_mut().push(label)); | 63 | if PROFILING_ENABLED.load(Ordering::Relaxed) |
63 | let label = if enabled { Some(label) } else { None }; | 64 | && PROFILE_STACK.with(|stack| stack.borrow_mut().push(label)) |
64 | Profiler { label, detail: None } | 65 | { |
66 | Profiler(Some(ProfilerImpl { label, detail: None })) | ||
67 | } else { | ||
68 | Profiler(None) | ||
69 | } | ||
65 | } | 70 | } |
66 | 71 | ||
67 | pub struct Profiler { | 72 | pub struct Profiler(Option<ProfilerImpl>); |
68 | label: Option<Label>, | 73 | |
74 | struct ProfilerImpl { | ||
75 | label: Label, | ||
69 | detail: Option<String>, | 76 | detail: Option<String>, |
70 | } | 77 | } |
71 | 78 | ||
72 | impl Profiler { | 79 | impl Profiler { |
73 | pub fn detail(mut self, detail: impl FnOnce() -> String) -> Profiler { | 80 | pub fn detail(mut self, detail: impl FnOnce() -> String) -> Profiler { |
74 | if self.label.is_some() { | 81 | if let Some(profiler) = &mut self.0 { |
75 | self.detail = Some(detail()) | 82 | profiler.detail = Some(detail()) |
76 | } | 83 | } |
77 | self | 84 | self |
78 | } | 85 | } |
79 | } | 86 | } |
80 | 87 | ||
81 | impl Drop for Profiler { | 88 | impl Drop for ProfilerImpl { |
82 | fn drop(&mut self) { | 89 | fn drop(&mut self) { |
83 | match self { | 90 | PROFILE_STACK.with(|it| it.borrow_mut().pop(self.label, self.detail.take())); |
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 | } | ||
91 | } | 91 | } |
92 | } | 92 | } |
93 | 93 | ||
@@ -179,21 +179,18 @@ impl ProfileStack { | |||
179 | pub fn pop(&mut self, label: Label, detail: Option<String>) { | 179 | pub fn pop(&mut self, label: Label, detail: Option<String>) { |
180 | let start = self.starts.pop().unwrap(); | 180 | let start = self.starts.pop().unwrap(); |
181 | let duration = start.elapsed(); | 181 | let duration = start.elapsed(); |
182 | let level = self.starts.len(); | ||
183 | self.messages.finish(Message { duration, label, detail }); | 182 | self.messages.finish(Message { duration, label, detail }); |
184 | if level == 0 { | 183 | if self.starts.is_empty() { |
185 | let longer_than = self.filter.longer_than; | 184 | let longer_than = self.filter.longer_than; |
186 | // Convert to millis for comparison to avoid problems with rounding | 185 | // Convert to millis for comparison to avoid problems with rounding |
187 | // (otherwise we could print `0ms` despite user's `>0` filter when | 186 | // (otherwise we could print `0ms` despite user's `>0` filter when |
188 | // `duration` is just a few nanos). | 187 | // `duration` is just a few nanos). |
189 | if duration.as_millis() > longer_than.as_millis() { | 188 | if duration.as_millis() > longer_than.as_millis() { |
190 | let stderr = stderr(); | ||
191 | if let Some(root) = self.messages.root() { | 189 | 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()); |
193 | } | 191 | } |
194 | } | 192 | } |
195 | self.messages.clear(); | 193 | self.messages.clear(); |
196 | assert!(self.starts.is_empty()) | ||
197 | } | 194 | } |
198 | } | 195 | } |
199 | } | 196 | } |