git.lucas.co / cce-system-interface
system settings
git clone https://git.lucas.co/cce-system-interface.git

commit1a0b9d5e3d76e99dbbe64046310da7a52cfc229d
parentd0a4bae028
authorLucas Galante <[email protected]>
date2026-06-24 22:49
Optimize performance: cache text buffers and offload blocking reloads to background threads

 src/main.rs              | 50 +++++++++++++++++++++++++++++++++++++++++-------
 src/pages/interface.rs   | 36 ++++++++++++++++++----------------
 src/pages/system_info.rs | 12 +++++++-----
 3 files changed, 70 insertions(+), 28 deletions(-)

diff --git a/src/main.rs b/src/main.rs
index ebbbdad..e4bfab7 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -6,14 +6,22 @@ use cce_system_interface::pages::{self, Page};
 mod input_handler;
 mod renderer;
 
+#[derive(Hash, PartialEq, Eq, Clone)]
+struct BufferCacheKey {
+    text: String,
+    size_milli: u32,
+    font: Option<String>,
+    sans_fallback: String,
+    serif_fallback: String,
+    mono_fallback: String,
+}
+
+std::thread_local! {
+    static BUFFER_CACHE: std::cell::RefCell<std::collections::HashMap<BufferCacheKey, Buffer>> = std::cell::RefCell::new(std::collections::HashMap::new());
+}
+
 fn make_text_buffer(fs: &mut FontSystem, text: &str, size: f32) -> Buffer {
-    let scale = cce_ui::scale::scale_factor();
-    let physical_size = size * scale;
-    let metrics = Metrics::new(physical_size, physical_size * 1.4);
-    let mut buf = Buffer::new(fs, metrics);
-    buf.set_text(fs, text, Attrs::new(), glyphon::Shaping::Advanced);
-    buf.shape_until_scroll(fs, true);
-    buf
+    make_text_buffer_with_font(fs, text, size, None, "", "", "")
 }
 
 fn find_cased_family(fs: &FontSystem, name: &str) -> Option<String> {
@@ -50,6 +58,25 @@ fn make_text_buffer_with_font(
     }
 
     let physical_size = font_size * scale;
+    let size_key = (physical_size * 1000.0).round() as u32;
+
+    let key = BufferCacheKey {
+        text: text.to_string(),
+        size_milli: size_key,
+        font: family_name.clone(),
+        sans_fallback: sans_fallback.to_string(),
+        serif_fallback: serif_fallback.to_string(),
+        mono_fallback: mono_fallback.to_string(),
+    };
+
+    let cached = BUFFER_CACHE.with(|cache| {
+        cache.borrow().get(&key).cloned()
+    });
+
+    if let Some(buf) = cached {
+        return buf;
+    }
+
     let metrics = Metrics::new(physical_size, physical_size * 1.4);
     let mut buf = Buffer::new(fs, metrics);
     let mut attrs = Attrs::new();
@@ -103,6 +130,15 @@ fn make_text_buffer_with_font(
 
     buf.set_text(fs, text, attrs, glyphon::Shaping::Advanced);
     buf.shape_until_scroll(fs, true);
+
+    BUFFER_CACHE.with(|cache| {
+        let mut cache = cache.borrow_mut();
+        if cache.len() > 3000 {
+            cache.clear();
+        }
+        cache.insert(key, buf.clone());
+    });
+
     buf
 }
 
diff --git a/src/pages/interface.rs b/src/pages/interface.rs
index 7daeee7..5dada46 100644
--- a/src/pages/interface.rs
+++ b/src/pages/interface.rs
@@ -1740,18 +1740,20 @@ fn send_ipc_command(cmd: &str) {
 }
 
 fn cce_graph_reload() {
-    let is_running = std::process::Command::new("pgrep")
-        .args(["-f", "cce-graph"])
-        .output()
-        .map(|o| !o.stdout.is_empty())
-        .unwrap_or(false);
-    if is_running {
-        let _ = std::process::Command::new("pkill")
+    std::thread::spawn(|| {
+        let is_running = std::process::Command::new("pgrep")
             .args(["-f", "cce-graph"])
-            .status();
-        std::thread::sleep(std::time::Duration::from_millis(150));
-        send_ipc_command("spawn cce-graph");
-    }
+            .output()
+            .map(|o| !o.stdout.is_empty())
+            .unwrap_or(false);
+        if is_running {
+            let _ = std::process::Command::new("pkill")
+                .args(["-f", "cce-graph"])
+                .status();
+            std::thread::sleep(std::time::Duration::from_millis(150));
+            send_ipc_command("spawn cce-graph");
+        }
+    });
 }
 
 fn apply_desktop_background(rgb: [u8; 3]) {
@@ -1773,11 +1775,13 @@ fn apply_disabled_color(rgb: [u8; 3]) {
 }
 
 pub fn status_interface_reload() {
-    let _ = std::process::Command::new("pkill")
-        .args(["-f", "cce-status-interface"])
-        .status();
-    std::thread::sleep(std::time::Duration::from_millis(150));
-    send_ipc_command("spawn cce-status-interface");
+    std::thread::spawn(|| {
+        let _ = std::process::Command::new("pkill")
+            .args(["-f", "cce-status-interface"])
+            .status();
+        std::thread::sleep(std::time::Duration::from_millis(150));
+        send_ipc_command("spawn cce-status-interface");
+    });
 }
 
 fn apply_visual_guides_color(rgb: [u8; 3]) {
diff --git a/src/pages/system_info.rs b/src/pages/system_info.rs
index 982bc94..8e1488a 100644
--- a/src/pages/system_info.rs
+++ b/src/pages/system_info.rs
@@ -1020,11 +1020,13 @@ fn write_status_underline(val: bool) {
 }
 
 fn status_interface_reload() {
-    let _ = std::process::Command::new("pkill")
-        .args(["-f", "cce-status-interface"])
-        .status();
-    std::thread::sleep(std::time::Duration::from_millis(150));
-    send_ipc_command("spawn cce-status-interface");
+    std::thread::spawn(|| {
+        let _ = std::process::Command::new("pkill")
+            .args(["-f", "cce-status-interface"])
+            .status();
+        std::thread::sleep(std::time::Duration::from_millis(150));
+        send_ipc_command("spawn cce-status-interface");
+    });
 }
 
 pub async fn fetch_status_state() -> StatusData {