Skip to content

Commit

Permalink
[WIP] Cache time only once
Browse files Browse the repository at this point in the history
  • Loading branch information
SpriteOvO committed Aug 17, 2024
1 parent bf30def commit 67f889b
Show file tree
Hide file tree
Showing 4 changed files with 127 additions and 161 deletions.
8 changes: 7 additions & 1 deletion spdlog/src/formatter/local_time_cacher.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::time::SystemTime;
use std::{fmt, time::SystemTime};

use chrono::prelude::*;
use once_cell::sync::Lazy;
Expand All @@ -20,6 +20,12 @@ pub(crate) struct TimeDate<'a> {
millisecond: u32,
}

impl fmt::Debug for TimeDate<'_> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("TimeDate").finish()
}
}

#[derive(Clone, Eq, PartialEq)]
enum CacheKey {
NonLeap(i64),
Expand Down
10 changes: 7 additions & 3 deletions spdlog/src/formatter/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,15 +95,19 @@ clone_trait_object!(Formatter);

/// Provides context for formatters.
#[derive(Debug, Default)]
pub struct FormatterContext {
pub struct FormatterContext<'a> {
style_range: Option<Range<usize>>,
time_date: Option<TimeDate<'a>>,
}

impl FormatterContext {
impl FormatterContext<'_> {
/// Constructs a `FormatterContext`.
#[must_use]
pub fn new() -> Self {
Self { style_range: None }
Self {
style_range: None,
time_date: None,
}
}

/// Sets style range (in bytes) of the formatted text.
Expand Down
32 changes: 18 additions & 14 deletions spdlog/src/formatter/pattern_formatter/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,17 @@ pub mod __pattern;
#[cfg(feature = "runtime-pattern")]
mod runtime;

use std::{fmt::Write, sync::Arc};
use std::{
fmt::{self, Write},
sync::Arc,
};

use dyn_clone::*;
#[cfg(feature = "runtime-pattern")]
pub use runtime::*;

use crate::{
formatter::{Formatter, FormatterContext},
formatter::{Formatter, FormatterContext, TimeDate, LOCAL_TIME_CACHER},
Error, Record, StringBuf,
};

Expand Down Expand Up @@ -373,15 +376,19 @@ where
&self,
record: &Record,
dest: &mut StringBuf,
ctx: &mut FormatterContext,
fmt_ctx: &mut FormatterContext,
) -> crate::Result<()> {
cfg_if::cfg_if! {
if #[cfg(not(feature = "flexible-string"))] {
dest.reserve(crate::string_buf::RESERVE_SIZE);
}
};

let mut ctx = PatternContext::new(ctx);
let mut local_time_cacher = LOCAL_TIME_CACHER.lock();
let mut ctx = PatternContext {
fmt_ctx,
time_date: local_time_cacher.get(record.time()),
};
self.pattern.format(record, dest, &mut ctx)?;
Ok(())
}
Expand All @@ -392,15 +399,8 @@ where
/// There is nothing to set up here at the moment, reserved for future use.
#[derive(Debug)]
pub struct PatternContext<'a> {
fmt_ctx: &'a mut FormatterContext,
}

impl<'a> PatternContext<'a> {
/// Creates a new `PatternContext` object.
#[must_use]
fn new(fmt_ctx: &'a mut FormatterContext) -> Self {
Self { fmt_ctx }
}
fmt_ctx: &'a mut FormatterContext<'a>,
time_date: TimeDate<'a>,
}

/// Represents a pattern for replacing a placeholder in templates.
Expand Down Expand Up @@ -1221,7 +1221,11 @@ pub mod tests {
let record = get_mock_record();
let mut output = StringBuf::new();
let mut fmt_ctx = FormatterContext::new();
let mut pat_ctx = PatternContext::new(&mut fmt_ctx);
let mut local_time_cacher = LOCAL_TIME_CACHER.lock();
let mut pat_ctx = PatternContext {
fmt_ctx: &mut fmt_ctx,
time_date: local_time_cacher.get(record.time()),
};

let format_result = pattern.format(&record, &mut output, &mut pat_ctx);
assert!(format_result.is_ok());
Expand Down
Loading

0 comments on commit 67f889b

Please sign in to comment.