Skip to content

Commit 9bd1073

Browse files
committed
perf(bar): add icon cache
This commit adds an icon cache which is indexed by executable name to avoid unnecessary calls to windows_icons::get_icon_by_process_id, which is known to start failing after the komorebi-bar process has been running for a certain (unknown) period of time.
1 parent 53c1990 commit 9bd1073

File tree

2 files changed

+56
-6
lines changed

2 files changed

+56
-6
lines changed

komorebi-bar/src/komorebi.rs

+49-6
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ use crate::render::RenderConfig;
66
use crate::selected_frame::SelectableFrame;
77
use crate::ui::CustomUi;
88
use crate::widget::BarWidget;
9+
use crate::ICON_CACHE;
910
use crate::MAX_LABEL_WIDTH;
1011
use crate::MONITOR_INDEX;
1112
use crossbeam_channel::Receiver;
@@ -611,27 +612,69 @@ impl From<&Workspace> for KomorebiNotificationStateContainerInformation {
611612

612613
impl From<&Container> for KomorebiNotificationStateContainerInformation {
613614
fn from(value: &Container) -> Self {
615+
let windows = value.windows().iter().collect::<Vec<_>>();
616+
let mut icons = vec![];
617+
618+
for window in windows {
619+
let mut icon_cache = ICON_CACHE.lock().unwrap();
620+
let mut update_cache = false;
621+
let exe = window.exe().unwrap_or_default();
622+
623+
match icon_cache.get(&exe) {
624+
None => {
625+
icons.push(windows_icons::get_icon_by_process_id(window.process_id()));
626+
update_cache = true;
627+
}
628+
Some(icon) => {
629+
icons.push(Some(icon.clone()));
630+
}
631+
}
632+
633+
if update_cache {
634+
if let Some(Some(icon)) = icons.last() {
635+
icon_cache.insert(exe, icon.clone());
636+
}
637+
}
638+
}
639+
614640
Self {
615641
titles: value
616642
.windows()
617643
.iter()
618644
.map(|w| w.title().unwrap_or_default())
619645
.collect::<Vec<_>>(),
620-
icons: value
621-
.windows()
622-
.iter()
623-
.map(|w| windows_icons::get_icon_by_process_id(w.process_id()))
624-
.collect::<Vec<_>>(),
646+
icons,
625647
focused_window_idx: value.focused_window_idx(),
626648
}
627649
}
628650
}
629651

630652
impl From<&Window> for KomorebiNotificationStateContainerInformation {
631653
fn from(value: &Window) -> Self {
654+
let mut icon_cache = ICON_CACHE.lock().unwrap();
655+
let mut update_cache = false;
656+
let mut icons = vec![];
657+
let exe = value.exe().unwrap_or_default();
658+
659+
match icon_cache.get(&exe) {
660+
None => {
661+
icons.push(windows_icons::get_icon_by_process_id(value.process_id()));
662+
update_cache = true;
663+
}
664+
Some(icon) => {
665+
icons.push(Some(icon.clone()));
666+
}
667+
}
668+
669+
if update_cache {
670+
if let Some(Some(icon)) = icons.last() {
671+
icon_cache.insert(exe, icon.clone());
672+
}
673+
}
674+
632675
Self {
633676
titles: vec![value.title().unwrap_or_default()],
634-
icons: vec![windows_icons::get_icon_by_process_id(value.process_id())],
677+
icons,
635678
focused_window_idx: 0,
636679
}
637680
}

komorebi-bar/src/main.rs

+7
Original file line numberDiff line numberDiff line change
@@ -24,16 +24,20 @@ use eframe::egui::ViewportBuilder;
2424
use font_loader::system_fonts;
2525
use hotwatch::EventKind;
2626
use hotwatch::Hotwatch;
27+
use image::RgbaImage;
2728
use komorebi_client::SocketMessage;
2829
use komorebi_client::SubscribeOptions;
2930
use schemars::gen::SchemaSettings;
31+
use std::collections::HashMap;
3032
use std::io::BufReader;
3133
use std::io::Read;
3234
use std::path::PathBuf;
3335
use std::sync::atomic::AtomicI32;
3436
use std::sync::atomic::AtomicUsize;
3537
use std::sync::atomic::Ordering;
3638
use std::sync::Arc;
39+
use std::sync::LazyLock;
40+
use std::sync::Mutex;
3741
use std::time::Duration;
3842
use tracing_subscriber::EnvFilter;
3943
use windows::Win32::Foundation::BOOL;
@@ -53,6 +57,9 @@ pub static MONITOR_RIGHT: AtomicI32 = AtomicI32::new(0);
5357
pub static MONITOR_INDEX: AtomicUsize = AtomicUsize::new(0);
5458
pub static BAR_HEIGHT: f32 = 50.0;
5559

60+
pub static ICON_CACHE: LazyLock<Mutex<HashMap<String, RgbaImage>>> =
61+
LazyLock::new(|| Mutex::new(HashMap::new()));
62+
5663
#[derive(Parser)]
5764
#[clap(author, about, version)]
5865
struct Opts {

0 commit comments

Comments
 (0)