1
1
//! Simple hierarchical profiler
2
- use once_cell:: sync:: Lazy ;
3
2
use std:: {
4
3
cell:: RefCell ,
5
4
collections:: { BTreeMap , HashSet } ,
@@ -12,6 +11,8 @@ use std::{
12
11
time:: { Duration , Instant } ,
13
12
} ;
14
13
14
+ use once_cell:: sync:: Lazy ;
15
+
15
16
use crate :: tree:: { Idx , Tree } ;
16
17
17
18
/// Filtering syntax
@@ -56,12 +57,12 @@ type Label = &'static str;
56
57
/// 0ms - profile
57
58
/// 0ms - profile2
58
59
/// ```
60
+ #[ inline]
59
61
pub fn span ( label : Label ) -> ProfileSpan {
60
- assert ! ( !label. is_empty( ) ) ;
62
+ debug_assert ! ( !label. is_empty( ) ) ;
61
63
62
- if PROFILING_ENABLED . load ( Ordering :: Relaxed )
63
- && PROFILE_STACK . with ( |stack| stack. borrow_mut ( ) . push ( label) )
64
- {
64
+ let enabled = PROFILING_ENABLED . load ( Ordering :: Relaxed ) ;
65
+ if enabled && with_profile_stack ( |stack| stack. push ( label) ) {
65
66
ProfileSpan ( Some ( ProfilerImpl { label, detail : None } ) )
66
67
} else {
67
68
ProfileSpan ( None )
@@ -85,14 +86,19 @@ impl ProfileSpan {
85
86
}
86
87
87
88
impl Drop for ProfilerImpl {
89
+ #[ inline]
88
90
fn drop ( & mut self ) {
89
- PROFILE_STACK . with ( |it| it. borrow_mut ( ) . pop ( self . label , self . detail . take ( ) ) ) ;
91
+ with_profile_stack ( |it| it. pop ( self . label , self . detail . take ( ) ) ) ;
90
92
}
91
93
}
92
94
93
95
static PROFILING_ENABLED : AtomicBool = AtomicBool :: new ( false ) ;
94
96
static FILTER : Lazy < RwLock < Filter > > = Lazy :: new ( Default :: default) ;
95
- thread_local ! ( static PROFILE_STACK : RefCell <ProfileStack > = RefCell :: new( ProfileStack :: new( ) ) ) ;
97
+
98
+ fn with_profile_stack < T > ( f : impl FnOnce ( & mut ProfileStack ) -> T ) -> T {
99
+ thread_local ! ( static STACK : RefCell <ProfileStack > = RefCell :: new( ProfileStack :: new( ) ) ) ;
100
+ STACK . with ( |it| f ( & mut * it. borrow_mut ( ) ) )
101
+ }
96
102
97
103
#[ derive( Default , Clone , Debug ) ]
98
104
struct Filter {
0 commit comments