Skip to content

Commit b71eef4

Browse files
committed
LocalTimeCacher return reference for string instead of Arc<String>
1 parent ca51dbb commit b71eef4

File tree

2 files changed

+96
-146
lines changed

2 files changed

+96
-146
lines changed

spdlog/src/formatter/local_time_cacher.rs

Lines changed: 37 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
use std::{
22
cell::{RefCell, RefMut},
3-
sync::Arc,
43
time::SystemTime,
54
};
65

@@ -36,25 +35,25 @@ struct CacheValues {
3635
is_leap_second: bool,
3736
full_second_str: RefCell<Option<String>>,
3837
year: RefCell<Option<i32>>,
39-
year_str: RefCell<Option<Arc<String>>>,
40-
year_short_str: RefCell<Option<Arc<String>>>,
38+
year_str: RefCell<Option<String>>,
39+
year_short_str: RefCell<Option<String>>,
4140
month: RefCell<Option<u32>>,
42-
month_str: RefCell<Option<Arc<String>>>,
41+
month_str: RefCell<Option<String>>,
4342
month_name: RefCell<Option<MultiName<&'static str>>>,
4443
weekday_name: RefCell<Option<MultiName<&'static str>>>,
4544
day: RefCell<Option<u32>>,
46-
day_str: RefCell<Option<Arc<String>>>,
45+
day_str: RefCell<Option<String>>,
4746
hour: RefCell<Option<u32>>,
48-
hour_str: RefCell<Option<Arc<String>>>,
47+
hour_str: RefCell<Option<String>>,
4948
hour12: RefCell<Option<(bool, u32)>>,
50-
hour12_str: RefCell<Option<Arc<String>>>,
49+
hour12_str: RefCell<Option<String>>,
5150
am_pm_str: RefCell<Option<&'static str>>,
5251
minute: RefCell<Option<u32>>,
53-
minute_str: RefCell<Option<Arc<String>>>,
52+
minute_str: RefCell<Option<String>>,
5453
second: RefCell<Option<u32>>,
55-
second_str: RefCell<Option<Arc<String>>>,
56-
tz_offset_str: RefCell<Option<Arc<String>>>,
57-
unix_timestamp_str: RefCell<Option<Arc<String>>>,
54+
second_str: RefCell<Option<String>>,
55+
tz_offset_str: RefCell<Option<String>>,
56+
unix_timestamp_str: RefCell<Option<String>>,
5857
}
5958

6059
#[derive(Clone, Copy, Eq, PartialEq)]
@@ -119,12 +118,12 @@ macro_rules! impl_cache_fields_getter {
119118
macro_rules! impl_cache_fields_str_getter {
120119
( $($field:ident => $str_field:ident : $fmt:literal),* $(,)? ) => {
121120
#[must_use]
122-
$(pub(crate) fn $str_field(&self) -> Arc<String> {
123-
self.cached
124-
.$str_field
125-
.borrow_mut()
126-
.get_or_insert_with(|| Arc::new(format!($fmt, self.cached.local_time.$field())))
127-
.clone()
121+
$(pub(crate) fn $str_field(&self) -> RefMut<str> {
122+
let mut value = self.cached.$str_field.borrow_mut();
123+
if value.is_none() {
124+
*value = Some(format!($fmt, self.cached.local_time.$field()));
125+
}
126+
RefMut::map(value, |value| value.as_deref_mut().unwrap())
128127
})*
129128
};
130129
}
@@ -262,12 +261,12 @@ impl<'a> TimeDate<'a> {
262261
}
263262

264263
#[must_use]
265-
pub(crate) fn hour12_str(&self) -> Arc<String> {
266-
self.cached
267-
.hour12_str
268-
.borrow_mut()
269-
.get_or_insert_with(|| Arc::new(format!("{:02}", self.hour12().1)))
270-
.clone()
264+
pub(crate) fn hour12_str(&self) -> RefMut<str> {
265+
let mut value = self.cached.hour12_str.borrow_mut();
266+
if value.is_none() {
267+
*value = Some(format!("{:02}", self.hour12().1));
268+
}
269+
RefMut::map(value, |value| value.as_deref_mut().unwrap())
271270
}
272271

273272
#[must_use]
@@ -282,32 +281,33 @@ impl<'a> TimeDate<'a> {
282281
}
283282

284283
#[must_use]
285-
pub(crate) fn year_short_str(&self) -> Arc<String> {
286-
self.cached
287-
.year_short_str
288-
.borrow_mut()
289-
.get_or_insert_with(|| Arc::new(format!("{:02}", self.year() % 100)))
290-
.clone()
284+
pub(crate) fn year_short_str(&self) -> RefMut<str> {
285+
let mut value = self.cached.year_short_str.borrow_mut();
286+
if value.is_none() {
287+
*value = Some(format!("{:02}", self.year() % 100));
288+
}
289+
RefMut::map(value, |value| value.as_deref_mut().unwrap())
291290
}
292291

293292
#[must_use]
294-
pub(crate) fn tz_offset_str(&self) -> Arc<String> {
295-
self.cached
296-
.tz_offset_str
297-
.borrow_mut()
298-
.get_or_insert_with(|| {
293+
pub(crate) fn tz_offset_str(&self) -> RefMut<str> {
294+
let mut value = self.cached.tz_offset_str.borrow_mut();
295+
if value.is_none() {
296+
*value = {
299297
let offset_secs = self.cached.local_time.offset().local_minus_utc();
300298
let offset_secs_abs = offset_secs.abs();
301299

302300
let sign_str = if offset_secs >= 0 { "+" } else { "-" };
303301
let offset_hours = offset_secs_abs / 3600;
304302
let offset_minutes = offset_secs_abs % 3600 / 60;
305-
Arc::new(format!(
303+
304+
Some(format!(
306305
"{}{:02}:{:02}",
307306
sign_str, offset_hours, offset_minutes
308307
))
309-
})
310-
.clone()
308+
};
309+
}
310+
RefMut::map(value, |value| value.as_deref_mut().unwrap())
311311
}
312312
}
313313

0 commit comments

Comments
 (0)