11#[ cfg( feature = "startup-trace" ) ]
2- use std:: sync:: { LazyLock , Mutex } ;
2+ use std:: sync:: { LazyLock , Mutex , OnceLock } ;
33#[ cfg( feature = "startup-trace" ) ]
44use std:: time:: { Duration , Instant } ;
55
@@ -13,7 +13,7 @@ pub static ENABLED: LazyLock<bool> = LazyLock::new(|| {
1313
1414// First-call timestamp (set exactly on the first trace call)
1515#[ cfg( feature = "startup-trace" ) ]
16- static START : LazyLock < Mutex < Option < Instant > > > = LazyLock :: new ( || Mutex :: new ( None ) ) ;
16+ static START : OnceLock < Instant > = OnceLock :: new ( ) ;
1717
1818// Previous-call timestamp
1919#[ cfg( feature = "startup-trace" ) ]
@@ -28,40 +28,32 @@ pub fn startup_trace_log(message: &str) {
2828 }
2929
3030 let now = Instant :: now ( ) ;
31- let mut start_guard = START . lock ( ) . expect ( "failed to lock START" ) ;
3231 let mut last_guard = LAST . lock ( ) . expect ( "failed to lock LAST" ) ;
32+ let start = START . get_or_init ( || {
33+ * last_guard = Some ( now) ;
34+ now
35+ } ) ;
3336
34- if start_guard. is_none ( ) {
35- * start_guard = Some ( now) ;
37+ if let Some ( last) = * last_guard {
38+ let delta_elapsed = now. checked_duration_since ( last) . unwrap_or ( Duration :: ZERO ) ;
39+ let start_elapsed = now. checked_duration_since ( * start) . unwrap_or ( Duration :: ZERO ) ;
40+ * last_guard = Some ( now) ;
41+ println ! ( "[startup]{message}: +{delta_elapsed:.3?} (Σ {start_elapsed:.3?})" ) ;
42+ } else {
3643 * last_guard = Some ( now) ;
3744 println ! (
3845 "[startup]{message}: +{:.3?} (Σ {:.3?})" ,
3946 Duration :: ZERO ,
4047 Duration :: ZERO
4148 ) ;
42- return ;
4349 }
44-
45- let start = start_guard. expect ( "START should be initialized" ) ;
46- let last = last_guard. expect ( "LAST should be initialized" ) ;
47- let delta_elapsed = now. checked_duration_since ( last) . unwrap_or ( Duration :: ZERO ) ;
48- let start_elapsed = now. checked_duration_since ( start) . unwrap_or ( Duration :: ZERO ) ;
49-
50- * last_guard = Some ( now) ;
51-
52- println ! ( "[startup]{message}: +{delta_elapsed:.3?} (Σ {start_elapsed:.3?})" ) ;
5350}
5451
55- /// No-op version when the feature is disabled; zero code generation.
56- #[ doc( hidden) ]
57- #[ cfg( not( feature = "startup-trace" ) ) ]
58- #[ inline( always) ]
59- pub fn startup_trace_log ( _message : & str ) { }
60-
6152/// Log a startup phase message and the time elapsed since the last `startup_trace!()` call.
6253#[ macro_export]
6354macro_rules! startup_trace {
6455 ( $msg: expr) => { {
56+ #[ cfg( feature = "startup-trace" ) ]
6557 $crate:: startup_trace:: startup_trace_log( $msg) ;
6658 } } ;
6759}
0 commit comments