time_util: cache parsed result of default time format

While measuring overhead of interpreter version of the template engine, I
noticed the templater spend some time in chrono. I don't think this would
matter in practice, but it's easy to cache the formatting items.

    % jj log -r'all()' -T'".\n"' --no-graph | wc -l
    2996

    % hyperfine --warmup 3 --runs 20 "jj log --ignore-working-copy -r 'all()' -Tshow --no-graph"
    (original)
      Time (mean ± σ):     120.0 ms ±  18.7 ms    [User: 97.5 ms, System: 22.5 ms]
      Range (min … max):    96.7 ms … 144.1 ms    20 runs
    (new)
      Time (mean ± σ):     106.2 ms ±  12.3 ms    [User: 86.1 ms, System: 20.1 ms]
      Range (min … max):    96.3 ms … 130.4 ms    20 runs

Regarding the template engine rewrites, I'm yet sure that the interpreter
version is strictly better. It's simpler, but could make some caching story
difficult. So I'm not gonna replace the engine anytime soon.
This commit is contained in:
Yuya Nishihara 2023-03-14 11:25:00 +09:00
parent 5afe5091a0
commit 731a94e4f2

View File

@ -1,5 +1,7 @@
use chrono::format::StrftimeItems;
use chrono::{DateTime, FixedOffset, LocalResult, TimeZone, Utc};
use jujutsu_lib::backend::Timestamp;
use once_cell::sync::Lazy;
fn datetime_from_timestamp(context: &Timestamp) -> Option<DateTime<FixedOffset>> {
let utc = match Utc.timestamp_opt(
@ -22,8 +24,10 @@ fn datetime_from_timestamp(context: &Timestamp) -> Option<DateTime<FixedOffset>>
}
pub fn format_absolute_timestamp(timestamp: &Timestamp) -> String {
static FORMAT_ITEMS: Lazy<Vec<chrono::format::Item>> =
Lazy::new(|| StrftimeItems::new("%Y-%m-%d %H:%M:%S.%3f %:z").collect());
match datetime_from_timestamp(timestamp) {
Some(datetime) => datetime.format("%Y-%m-%d %H:%M:%S.%3f %:z").to_string(),
Some(datetime) => datetime.format_with_items(FORMAT_ITEMS.iter()).to_string(),
None => "<out-of-range date>".to_string(),
}
}