From 731a94e4f20a75de65032928f847f07e70eac5ae Mon Sep 17 00:00:00 2001 From: Yuya Nishihara Date: Tue, 14 Mar 2023 11:25:00 +0900 Subject: [PATCH] time_util: cache parsed result of default time format MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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. --- src/time_util.rs | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/time_util.rs b/src/time_util.rs index 1db20ba84..f94517717 100644 --- a/src/time_util.rs +++ b/src/time_util.rs @@ -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> { let utc = match Utc.timestamp_opt( @@ -22,8 +24,10 @@ fn datetime_from_timestamp(context: &Timestamp) -> Option> } pub fn format_absolute_timestamp(timestamp: &Timestamp) -> String { + static FORMAT_ITEMS: Lazy> = + 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 => "".to_string(), } }