diff --git a/spdlog/src/formatter/local_time_cacher.rs b/spdlog/src/formatter/local_time_cacher.rs index 3abdff32..f8bba25e 100644 --- a/spdlog/src/formatter/local_time_cacher.rs +++ b/spdlog/src/formatter/local_time_cacher.rs @@ -1,6 +1,5 @@ use std::{ cell::{RefCell, RefMut}, - sync::Arc, time::SystemTime, }; @@ -36,25 +35,25 @@ struct CacheValues { is_leap_second: bool, full_second_str: RefCell>, year: RefCell>, - year_str: RefCell>>, - year_short_str: RefCell>>, + year_str: RefCell>, + year_short_str: RefCell>, month: RefCell>, - month_str: RefCell>>, + month_str: RefCell>, month_name: RefCell>>, weekday_name: RefCell>>, day: RefCell>, - day_str: RefCell>>, + day_str: RefCell>, hour: RefCell>, - hour_str: RefCell>>, + hour_str: RefCell>, hour12: RefCell>, - hour12_str: RefCell>>, + hour12_str: RefCell>, am_pm_str: RefCell>, minute: RefCell>, - minute_str: RefCell>>, + minute_str: RefCell>, second: RefCell>, - second_str: RefCell>>, - tz_offset_str: RefCell>>, - unix_timestamp_str: RefCell>>, + second_str: RefCell>, + tz_offset_str: RefCell>, + unix_timestamp_str: RefCell>, } #[derive(Clone, Copy, Eq, PartialEq)] @@ -119,12 +118,12 @@ macro_rules! impl_cache_fields_getter { macro_rules! impl_cache_fields_str_getter { ( $($field:ident => $str_field:ident : $fmt:literal),* $(,)? ) => { #[must_use] - $(pub(crate) fn $str_field(&self) -> Arc { - self.cached - .$str_field - .borrow_mut() - .get_or_insert_with(|| Arc::new(format!($fmt, self.cached.local_time.$field()))) - .clone() + $(pub(crate) fn $str_field(&self) -> RefMut { + let mut value = self.cached.$str_field.borrow_mut(); + if value.is_none() { + *value = Some(format!($fmt, self.cached.local_time.$field())); + } + RefMut::map(value, |value| value.as_deref_mut().unwrap()) })* }; } @@ -262,12 +261,12 @@ impl<'a> TimeDate<'a> { } #[must_use] - pub(crate) fn hour12_str(&self) -> Arc { - self.cached - .hour12_str - .borrow_mut() - .get_or_insert_with(|| Arc::new(format!("{:02}", self.hour12().1))) - .clone() + pub(crate) fn hour12_str(&self) -> RefMut { + let mut value = self.cached.hour12_str.borrow_mut(); + if value.is_none() { + *value = Some(format!("{:02}", self.hour12().1)); + } + RefMut::map(value, |value| value.as_deref_mut().unwrap()) } #[must_use] @@ -282,32 +281,33 @@ impl<'a> TimeDate<'a> { } #[must_use] - pub(crate) fn year_short_str(&self) -> Arc { - self.cached - .year_short_str - .borrow_mut() - .get_or_insert_with(|| Arc::new(format!("{:02}", self.year() % 100))) - .clone() + pub(crate) fn year_short_str(&self) -> RefMut { + let mut value = self.cached.year_short_str.borrow_mut(); + if value.is_none() { + *value = Some(format!("{:02}", self.year() % 100)); + } + RefMut::map(value, |value| value.as_deref_mut().unwrap()) } #[must_use] - pub(crate) fn tz_offset_str(&self) -> Arc { - self.cached - .tz_offset_str - .borrow_mut() - .get_or_insert_with(|| { + pub(crate) fn tz_offset_str(&self) -> RefMut { + let mut value = self.cached.tz_offset_str.borrow_mut(); + if value.is_none() { + *value = { let offset_secs = self.cached.local_time.offset().local_minus_utc(); let offset_secs_abs = offset_secs.abs(); let sign_str = if offset_secs >= 0 { "+" } else { "-" }; let offset_hours = offset_secs_abs / 3600; let offset_minutes = offset_secs_abs % 3600 / 60; - Arc::new(format!( + + Some(format!( "{}{:02}:{:02}", sign_str, offset_hours, offset_minutes )) - }) - .clone() + }; + } + RefMut::map(value, |value| value.as_deref_mut().unwrap()) } } diff --git a/spdlog/src/formatter/pattern_formatter/pattern/datetime.rs b/spdlog/src/formatter/pattern_formatter/pattern/datetime.rs index 2ffc8c58..2f1fedb2 100644 --- a/spdlog/src/formatter/pattern_formatter/pattern/datetime.rs +++ b/spdlog/src/formatter/pattern_formatter/pattern/datetime.rs @@ -108,42 +108,23 @@ impl Pattern for FullDateTime { dest: &mut StringBuf, _ctx: &mut PatternContext, ) -> crate::Result<()> { - let ( - abbr_weekday_name, - abbr_month_name, - day_str, - hour_str, - minute_str, - second_str, - year_str, - ) = { - let mut time_cacher_lock = LOCAL_TIME_CACHER.lock(); - let cached_time = time_cacher_lock.get(record.time()); - ( - cached_time.weekday_name().short, - cached_time.month_name().short, - cached_time.day_str(), - cached_time.hour_str(), - cached_time.minute_str(), - cached_time.second_str(), - cached_time.year_str(), - ) - }; + let mut time_cacher_lock = LOCAL_TIME_CACHER.lock(); + let cached_time = time_cacher_lock.get(record.time()); (|| { - dest.write_str(abbr_weekday_name)?; + dest.write_str(cached_time.weekday_name().short)?; dest.write_char(' ')?; - dest.write_str(abbr_month_name)?; + dest.write_str(cached_time.month_name().short)?; dest.write_char(' ')?; - dest.write_str(&day_str)?; + dest.write_str(&cached_time.day_str())?; dest.write_char(' ')?; - dest.write_str(&hour_str)?; + dest.write_str(&cached_time.hour_str())?; dest.write_char(':')?; - dest.write_str(&minute_str)?; + dest.write_str(&cached_time.minute_str())?; dest.write_char(':')?; - dest.write_str(&second_str)?; + dest.write_str(&cached_time.second_str())?; dest.write_char(' ')?; - dest.write_str(&year_str) + dest.write_str(&cached_time.year_str()) })() .map_err(Error::FormatRecord) } @@ -161,8 +142,8 @@ impl Pattern for ShortYear { dest: &mut StringBuf, _ctx: &mut PatternContext, ) -> crate::Result<()> { - let year_short_str = LOCAL_TIME_CACHER.lock().get(record.time()).year_short_str(); - dest.write_str(&year_short_str).map_err(Error::FormatRecord) + dest.write_str(&LOCAL_TIME_CACHER.lock().get(record.time()).year_short_str()) + .map_err(Error::FormatRecord) } } @@ -178,8 +159,8 @@ impl Pattern for Year { dest: &mut StringBuf, _ctx: &mut PatternContext, ) -> crate::Result<()> { - let year_str = LOCAL_TIME_CACHER.lock().get(record.time()).year_str(); - dest.write_str(&year_str).map_err(Error::FormatRecord) + dest.write_str(&LOCAL_TIME_CACHER.lock().get(record.time()).year_str()) + .map_err(Error::FormatRecord) } } @@ -195,22 +176,15 @@ impl Pattern for Date { dest: &mut StringBuf, _ctx: &mut PatternContext, ) -> crate::Result<()> { - let (month_str, day_str, year_str) = { - let mut local_cacher_lock = LOCAL_TIME_CACHER.lock(); - let cached_time = local_cacher_lock.get(record.time()); - ( - cached_time.month_str(), - cached_time.day_str(), - cached_time.year_str(), - ) - }; + let mut local_cacher_lock = LOCAL_TIME_CACHER.lock(); + let cached_time = local_cacher_lock.get(record.time()); (|| { - dest.write_str(&year_str)?; + dest.write_str(&cached_time.year_str())?; dest.write_char('-')?; - dest.write_str(&month_str)?; + dest.write_str(&cached_time.month_str())?; dest.write_char('-')?; - dest.write_str(&day_str) + dest.write_str(&cached_time.day_str()) })() .map_err(Error::FormatRecord) } @@ -228,22 +202,15 @@ impl Pattern for ShortDate { dest: &mut StringBuf, _ctx: &mut PatternContext, ) -> crate::Result<()> { - let (month_str, day_str, year_short_str) = { - let mut local_cacher_lock = LOCAL_TIME_CACHER.lock(); - let cached_time = local_cacher_lock.get(record.time()); - ( - cached_time.month_str(), - cached_time.day_str(), - cached_time.year_short_str(), - ) - }; + let mut local_cacher_lock = LOCAL_TIME_CACHER.lock(); + let cached_time = local_cacher_lock.get(record.time()); (|| { - dest.write_str(&month_str)?; + dest.write_str(&cached_time.month_str())?; dest.write_char('/')?; - dest.write_str(&day_str)?; + dest.write_str(&cached_time.day_str())?; dest.write_char('/')?; - dest.write_str(&year_short_str) + dest.write_str(&cached_time.year_short_str()) })() .map_err(Error::FormatRecord) } @@ -261,8 +228,8 @@ impl Pattern for Month { dest: &mut StringBuf, _ctx: &mut PatternContext, ) -> crate::Result<()> { - let month_str = LOCAL_TIME_CACHER.lock().get(record.time()).month_str(); - dest.write_str(&month_str).map_err(Error::FormatRecord) + dest.write_str(&LOCAL_TIME_CACHER.lock().get(record.time()).month_str()) + .map_err(Error::FormatRecord) } } @@ -278,8 +245,8 @@ impl Pattern for Day { dest: &mut StringBuf, _ctx: &mut PatternContext, ) -> crate::Result<()> { - let day_str = LOCAL_TIME_CACHER.lock().get(record.time()).day_str(); - dest.write_str(&day_str).map_err(Error::FormatRecord) + dest.write_str(&LOCAL_TIME_CACHER.lock().get(record.time()).day_str()) + .map_err(Error::FormatRecord) } } @@ -295,8 +262,8 @@ impl Pattern for Hour { dest: &mut StringBuf, _ctx: &mut PatternContext, ) -> crate::Result<()> { - let hour_str = LOCAL_TIME_CACHER.lock().get(record.time()).hour_str(); - dest.write_str(&hour_str).map_err(Error::FormatRecord) + dest.write_str(&LOCAL_TIME_CACHER.lock().get(record.time()).hour_str()) + .map_err(Error::FormatRecord) } } @@ -312,8 +279,8 @@ impl Pattern for Hour12 { dest: &mut StringBuf, _ctx: &mut PatternContext, ) -> crate::Result<()> { - let hour_12_str = LOCAL_TIME_CACHER.lock().get(record.time()).hour12_str(); - dest.write_str(&hour_12_str).map_err(Error::FormatRecord) + dest.write_str(&LOCAL_TIME_CACHER.lock().get(record.time()).hour12_str()) + .map_err(Error::FormatRecord) } } @@ -329,8 +296,8 @@ impl Pattern for Minute { dest: &mut StringBuf, _ctx: &mut PatternContext, ) -> crate::Result<()> { - let minute_str = LOCAL_TIME_CACHER.lock().get(record.time()).minute_str(); - dest.write_str(&minute_str).map_err(Error::FormatRecord) + dest.write_str(&LOCAL_TIME_CACHER.lock().get(record.time()).minute_str()) + .map_err(Error::FormatRecord) } } @@ -346,8 +313,8 @@ impl Pattern for Second { dest: &mut StringBuf, _ctx: &mut PatternContext, ) -> crate::Result<()> { - let second_str = LOCAL_TIME_CACHER.lock().get(record.time()).second_str(); - dest.write_str(&second_str).map_err(Error::FormatRecord) + dest.write_str(&LOCAL_TIME_CACHER.lock().get(record.time()).second_str()) + .map_err(Error::FormatRecord) } } @@ -435,25 +402,17 @@ impl Pattern for Time12 { dest: &mut StringBuf, _ctx: &mut PatternContext, ) -> crate::Result<()> { - let (hour_str, minute_str, second_str, am_pm_str) = { - let mut time_cacher_lock = LOCAL_TIME_CACHER.lock(); - let cached_time = time_cacher_lock.get(record.time()); - ( - cached_time.hour12_str(), - cached_time.minute_str(), - cached_time.second_str(), - cached_time.am_pm_str(), - ) - }; + let mut time_cacher_lock = LOCAL_TIME_CACHER.lock(); + let cached_time = time_cacher_lock.get(record.time()); (|| { - dest.write_str(&hour_str)?; + dest.write_str(&cached_time.hour12_str())?; dest.write_char(':')?; - dest.write_str(&minute_str)?; + dest.write_str(&cached_time.minute_str())?; dest.write_char(':')?; - dest.write_str(&second_str)?; + dest.write_str(&cached_time.second_str())?; dest.write_str(" ")?; - dest.write_str(am_pm_str) + dest.write_str(cached_time.am_pm_str()) })() .map_err(Error::FormatRecord) } @@ -471,16 +430,13 @@ impl Pattern for ShortTime { dest: &mut StringBuf, _ctx: &mut PatternContext, ) -> crate::Result<()> { - let (hour_str, minute_str) = { - let mut time_cacher_lock = LOCAL_TIME_CACHER.lock(); - let cached_time = time_cacher_lock.get(record.time()); - (cached_time.hour_str(), cached_time.minute_str()) - }; + let mut time_cacher_lock = LOCAL_TIME_CACHER.lock(); + let cached_time = time_cacher_lock.get(record.time()); (|| { - dest.write_str(&hour_str)?; + dest.write_str(&cached_time.hour_str())?; dest.write_char(':')?; - dest.write_str(&minute_str) + dest.write_str(&cached_time.minute_str()) })() .map_err(Error::FormatRecord) } @@ -498,22 +454,15 @@ impl Pattern for Time { dest: &mut StringBuf, _ctx: &mut PatternContext, ) -> crate::Result<()> { - let (hour_str, minute_str, second_str) = { - let mut time_cacher_lock = LOCAL_TIME_CACHER.lock(); - let cached_time = time_cacher_lock.get(record.time()); - ( - cached_time.hour_str(), - cached_time.minute_str(), - cached_time.second_str(), - ) - }; + let mut time_cacher_lock = LOCAL_TIME_CACHER.lock(); + let cached_time = time_cacher_lock.get(record.time()); (|| { - dest.write_str(&hour_str)?; + dest.write_str(&cached_time.hour_str())?; dest.write_char(':')?; - dest.write_str(&minute_str)?; + dest.write_str(&cached_time.minute_str())?; dest.write_char(':')?; - dest.write_str(&second_str) + dest.write_str(&cached_time.second_str()) })() .map_err(Error::FormatRecord) } @@ -531,8 +480,8 @@ impl Pattern for TzOffset { dest: &mut StringBuf, _ctx: &mut PatternContext, ) -> crate::Result<()> { - let tz_offset_str = LOCAL_TIME_CACHER.lock().get(record.time()).tz_offset_str(); - dest.write_str(&tz_offset_str).map_err(Error::FormatRecord) + dest.write_str(&LOCAL_TIME_CACHER.lock().get(record.time()).tz_offset_str()) + .map_err(Error::FormatRecord) } } @@ -548,11 +497,12 @@ impl Pattern for UnixTimestamp { dest: &mut StringBuf, _ctx: &mut PatternContext, ) -> crate::Result<()> { - let unix_timestamp_str = LOCAL_TIME_CACHER - .lock() - .get(record.time()) - .unix_timestamp_str(); - dest.write_str(&unix_timestamp_str) - .map_err(Error::FormatRecord) + dest.write_str( + &LOCAL_TIME_CACHER + .lock() + .get(record.time()) + .unix_timestamp_str(), + ) + .map_err(Error::FormatRecord) } }