diff options
| author | Daniel Jankowski <daniel.jankowski@rub.de> | 2023-12-26 12:07:33 +0100 |
|---|---|---|
| committer | Daniel Jankowski <daniel.jankowski@rub.de> | 2023-12-26 12:07:33 +0100 |
| commit | 7e939328f9ec45fbcd856474dd7c464e3ff402bb (patch) | |
| tree | 9bb682a5d63c4214d14d6ab25f80091a54c64bea | |
| parent | chore: update cargo deps (diff) | |
| download | sl0pstatus-7e939328f9ec45fbcd856474dd7c464e3ff402bb.tar.gz sl0pstatus-7e939328f9ec45fbcd856474dd7c464e3ff402bb.tar.xz sl0pstatus-7e939328f9ec45fbcd856474dd7c464e3ff402bb.zip | |
feat: implement cache for dynamic command rendering
Diffstat (limited to '')
| -rw-r--r-- | Cargo.lock | 120 | ||||
| -rw-r--r-- | Cargo.toml | 1 | ||||
| -rw-r--r-- | benches/benches.rs | 37 | ||||
| -rw-r--r-- | benches/widgets.rs | 2 | ||||
| -rw-r--r-- | src/render.rs | 24 | ||||
| -rw-r--r-- | src/widgets/command.rs | 11 |
6 files changed, 189 insertions, 6 deletions
@@ -72,6 +72,18 @@ dependencies = [ ] [[package]] +name = "ahash" +version = "0.8.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "91429305e9f0a25f6205c5b8e0d2db09e0708a7a6df0f42212bb56c32c8ac97a" +dependencies = [ + "cfg-if", + "once_cell", + "version_check", + "zerocopy", +] + +[[package]] name = "aho-corasick" version = "1.1.2" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -81,6 +93,12 @@ dependencies = [ ] [[package]] +name = "allocator-api2" +version = "0.2.16" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0942ffc6dcaadf03badf6e6a2d0228460359d5e34b57ccdc720b7382dfbd5ec5" + +[[package]] name = "android-tzdata" version = "0.1.1" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -455,6 +473,39 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a2bd12c1caf447e69cd4528f47f94d203fd2582878ecb9e9465484c4148a8223" [[package]] +name = "cached" +version = "0.46.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c7c8c50262271cdf5abc979a5f76515c234e764fa025d1ba4862c0f0bcda0e95" +dependencies = [ + "ahash", + "cached_proc_macro", + "cached_proc_macro_types", + "hashbrown 0.14.3", + "instant", + "once_cell", + "thiserror", +] + +[[package]] +name = "cached_proc_macro" +version = "0.18.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c878c71c2821aa2058722038a59a67583a4240524687c6028571c9b395ded61f" +dependencies = [ + "darling", + "proc-macro2", + "quote", + "syn 1.0.109", +] + +[[package]] +name = "cached_proc_macro_types" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3a4f925191b4367301851c6d99b09890311d74b0d43f274c0b34c86d308a3663" + +[[package]] name = "cast" version = "0.3.0" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -865,6 +916,41 @@ dependencies = [ ] [[package]] +name = "darling" +version = "0.14.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7b750cb3417fd1b327431a470f388520309479ab0bf5e323505daf0290cd3850" +dependencies = [ + "darling_core", + "darling_macro", +] + +[[package]] +name = "darling_core" +version = "0.14.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "109c1ca6e6b7f82cc233a97004ea8ed7ca123a9af07a8230878fcfda9b158bf0" +dependencies = [ + "fnv", + "ident_case", + "proc-macro2", + "quote", + "strsim", + "syn 1.0.109", +] + +[[package]] +name = "darling_macro" +version = "0.14.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a4aab4dbc9f7611d8b55048a3a16d2d010c2c8334e46304b40ac1cc14bf3b48e" +dependencies = [ + "darling_core", + "quote", + "syn 1.0.109", +] + +[[package]] name = "deltae" version = "0.3.2" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -1310,6 +1396,10 @@ name = "hashbrown" version = "0.14.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "290f1a1d9242c78d09ce40a5e87e7554ee637af1351968159f4952f028f75604" +dependencies = [ + "ahash", + "allocator-api2", +] [[package]] name = "heck" @@ -1453,6 +1543,12 @@ dependencies = [ ] [[package]] +name = "ident_case" +version = "1.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b9e0384b61958566e926dc50660321d12159025e767c18e043daf26b70104c39" + +[[package]] name = "idna" version = "0.5.0" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -1534,6 +1630,9 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "7a5bbe824c507c5da5956355e86a746d82e0e1464f65d862cc5e71da70e94b2c" dependencies = [ "cfg-if", + "js-sys", + "wasm-bindgen", + "web-sys", ] [[package]] @@ -3967,11 +4066,32 @@ dependencies = [ ] [[package]] +name = "zerocopy" +version = "0.7.32" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "74d4d3961e53fa4c9a25a8637fc2bfaf2595b3d3ae34875568a5cf64787716be" +dependencies = [ + "zerocopy-derive", +] + +[[package]] +name = "zerocopy-derive" +version = "0.7.32" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9ce1b18ccd8e73a9321186f97e46f9f04b778851177567b1975109d26a08d2a6" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.43", +] + +[[package]] name = "zjstatus" version = "0.11.0" dependencies = [ "anstyle", "anyhow", + "cached", "chrono", "chrono-tz", "criterion", @@ -17,6 +17,7 @@ anyhow = "1.0.75" anstyle = "1.0.2" uuid = { version = "1.6.1", features = ["v4"] } lazy_static = "1.4.0" +cached = { version = "0.46.1", features = ["wasm"] } [dev-dependencies] criterion = { version = "0.5.1", default-features = false, features = ["html_reports"] } diff --git a/benches/benches.rs b/benches/benches.rs index ee6d577..1435b87 100644 --- a/benches/benches.rs +++ b/benches/benches.rs @@ -4,7 +4,9 @@ use zellij_tile::prelude::{ModeInfo, TabInfo}; use zjstatus::{ config::{ModuleConfig, ZellijState}, - render::FormattedPart, + render::{ + formatted_part_from_string_cached, formatted_parts_from_string_cached, FormattedPart, + }, widgets::{datetime::DateTimeWidget, mode::ModeWidget, session::SessionWidget, widget::Widget}, }; @@ -108,6 +110,36 @@ fn bench_formattedpart_from_format_string(c: &mut Criterion) { }); } +fn bench_formattedpart_from_format_string_cached(c: &mut Criterion) { + c.bench_function("formatted_part_from_string_cached", |b| { + b.iter(|| { + formatted_part_from_string_cached( + "#[fg=#9399B2,bg=#181825,bold,italic] {index} {name} [] ", + ) + }) + }); +} + +fn bench_formattedpart_multiple_from_format_string(c: &mut Criterion) { + c.bench_function("FormattedPart::multiple_from_format_string", |b| { + b.iter(|| { + FormattedPart::multiple_from_format_string( + "#[fg=#9399B2,bg=#181825,bold,italic] {index} {name} [] #[fg=#9399B2,bg=#181825,bold,italic] {index} {name} [] ", + ) + }) + }); +} + +fn bench_formattedparts_from_format_string_cached(c: &mut Criterion) { + c.bench_function("formatted_parts_from_string_cached", |b| { + b.iter(|| { + formatted_parts_from_string_cached( + "#[fg=#9399B2,bg=#181825,bold,italic] {index} {name} [] #[fg=#9399B2,bg=#181825,bold,italic] {index} {name} [] ", + ) + }) + }); +} + fn bench_moduleconfig_new(c: &mut Criterion) { let mut config = BTreeMap::new(); @@ -122,6 +154,9 @@ fn bench_moduleconfig_new(c: &mut Criterion) { fn criterion_benchmark(c: &mut Criterion) { bench_formattedpart_from_format_string(c); + bench_formattedpart_from_format_string_cached(c); + bench_formattedpart_multiple_from_format_string(c); + bench_formattedparts_from_format_string_cached(c); bench_formattedpart_format_string_with_widgets(c); bench_moduleconfig_new(c); bench_moduleconfig_render_bar(c); diff --git a/benches/widgets.rs b/benches/widgets.rs index 1148cdb..d8d6212 100644 --- a/benches/widgets.rs +++ b/benches/widgets.rs @@ -63,7 +63,7 @@ fn bench_widget_command_dynamic(c: &mut Criterion) { "command_test".to_owned(), widgets::command::CommandResult { exit_code: Some(0), - stdout: "#[fg=#9399B2,bg=#181825,bold,italic] test".to_owned(), + stdout: "#[fg=#9399B2,bg=#181825,bold,italic] test #[fg=#9399B2,bg=#181825,bold,italic] test".to_owned(), stderr: "".to_owned(), context: BTreeMap::from([( "timestamp".to_owned(), diff --git a/src/render.rs b/src/render.rs index 3f645f6..048b82d 100644 --- a/src/render.rs +++ b/src/render.rs @@ -1,3 +1,4 @@ +use cached::{proc_macro::cached, SizedCache}; use lazy_static::lazy_static; use std::{collections::BTreeMap, sync::Arc}; @@ -20,6 +21,24 @@ pub struct FormattedPart { pub content: String, } +#[cached( + type = "SizedCache<String, FormattedPart>", + create = "{ SizedCache::with_size(100) }", + convert = r#"{ (format.to_owned()) }"# +)] +pub fn formatted_part_from_string_cached(format: &str) -> FormattedPart { + FormattedPart::from_format_string(format) +} + +#[cached( + type = "SizedCache<String, Vec<FormattedPart>>", + create = "{ SizedCache::with_size(100) }", + convert = r#"{ (config.to_owned()) }"# +)] +pub fn formatted_parts_from_string_cached(config: &str) -> Vec<FormattedPart> { + FormattedPart::multiple_from_format_string(config) +} + impl FormattedPart { pub fn multiple_from_format_string(config: &str) -> Vec<Self> { config @@ -144,6 +163,11 @@ fn hex_to_rgb(s: &str) -> anyhow::Result<Vec<u8>> { .collect() } +#[cached( + type = "SizedCache<String, Option<Color>>", + create = "{ SizedCache::with_size(100) }", + convert = r#"{ (color.to_owned()) }"# +)] fn parse_color(color: &str) -> Option<Color> { if color.starts_with('#') { let rgb = match hex_to_rgb(color.strip_prefix('#').unwrap()) { diff --git a/src/widgets/command.rs b/src/widgets/command.rs index 22b2892..b9e9aba 100644 --- a/src/widgets/command.rs +++ b/src/widgets/command.rs @@ -8,9 +8,10 @@ use std::{ use chrono::{DateTime, Duration, Local}; use regex::Regex; +#[cfg(not(feature = "bench"))] use zellij_tile::shim::run_command; -use crate::render::FormattedPart; +use crate::render::{formatted_parts_from_string_cached, FormattedPart}; use crate::{config::ZellijState, widgets::widget::Widget}; @@ -113,7 +114,7 @@ impl Widget for CommandWidget { } fn render_dynamic_formatted_content(content: &str) -> String { - FormattedPart::multiple_from_format_string(content) + formatted_parts_from_string_cached(content) .iter() .map(|fp| fp.format_string(&fp.content)) .collect::<Vec<String>>() @@ -133,9 +134,11 @@ fn run_command_if_needed(command_config: CommandConfig, name: &str, state: &Zell ); let command = commandline_parser(&command_config.command); - let command = command.iter().map(|x| x.as_str()).collect::<Vec<&str>>(); #[cfg(not(feature = "bench"))] - run_command(&command, context); + run_command( + &command.iter().map(|x| x.as_str()).collect::<Vec<&str>>(), + context, + ); } } |
