From 971374f60ba701cb2a8728985c9d2742c590b036 Mon Sep 17 00:00:00 2001 From: Michael Aaron Murphy Date: Tue, 14 Apr 2026 16:27:53 +0200 Subject: [PATCH] perf: use atomics for controller progress and state --- Cargo.lock | 8 ++++++++ Cargo.toml | 2 ++ src/operation/controller.rs | 29 +++++++++++++++++++---------- 3 files changed, 29 insertions(+), 10 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 55dead6..9a0d3f9 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -538,6 +538,12 @@ version = "1.1.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "1505bd5d3d116872e7271a6d4e16d81d0c8570876c8de68093a09ac269d8aac0" +[[package]] +name = "atomic_float" +version = "1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "628d228f918ac3b82fe590352cc719d30664a0c13ca3a60266fe02c7132d480a" + [[package]] name = "atomicwrites" version = "0.4.2" @@ -1349,6 +1355,7 @@ name = "cosmic-files" version = "1.0.9" dependencies = [ "anyhow", + "atomic_float", "bzip2", "compio", "cosmic-client-toolkit", @@ -1378,6 +1385,7 @@ dependencies = [ "notify-debouncer-full", "notify-rust", "num_cpus", + "num_enum", "open", "ordermap", "paste", diff --git a/Cargo.toml b/Cargo.toml index 9447640..74e554b 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -64,6 +64,8 @@ filetime = "0.2" tracing = "0.1.44" tracing-subscriber = { version = "0.3.22", features = ["env-filter"] } thiserror = "2.0.18" +atomic_float = "1.1.0" +num_enum = "0.7.6" # Completion-based IO runtime to enable io_uring / IOCP file IO support. [dependencies.compio] diff --git a/src/operation/controller.rs b/src/operation/controller.rs index 960a28d..691316c 100644 --- a/src/operation/controller.rs +++ b/src/operation/controller.rs @@ -1,7 +1,11 @@ -use std::sync::{Arc, Mutex}; +use atomic_float::AtomicF32; +use num_enum::{IntoPrimitive, TryFromPrimitive}; +use std::sync::Arc; +use std::sync::atomic::{self, AtomicU16}; use tokio::sync::Notify; -#[derive(Clone, Copy, Debug, PartialEq, Eq)] +#[derive(Clone, Copy, Debug, PartialEq, Eq, IntoPrimitive, TryFromPrimitive)] +#[repr(u16)] pub enum ControllerState { Cancelled, Failed, @@ -11,8 +15,8 @@ pub enum ControllerState { #[derive(Debug)] struct ControllerInner { - state: Mutex, - progress: Mutex, + state: AtomicU16, + progress: AtomicF32, notify: Notify, } @@ -27,8 +31,8 @@ impl Default for Controller { Self { primary: true, inner: Arc::new(ControllerInner { - state: Mutex::new(ControllerState::Running), - progress: Mutex::new(0.0), + state: AtomicU16::new(ControllerState::Running.into()), + progress: AtomicF32::new(0.0), notify: Notify::new(), }), } @@ -50,19 +54,24 @@ impl Controller { } pub fn progress(&self) -> f32 { - *self.inner.progress.lock().unwrap() + self.inner.progress.load(atomic::Ordering::Relaxed) } pub fn set_progress(&self, progress: f32) { - *self.inner.progress.lock().unwrap() = progress; + self.inner + .progress + .swap(progress, atomic::Ordering::Relaxed); } pub fn state(&self) -> ControllerState { - *self.inner.state.lock().unwrap() + ControllerState::try_from(self.inner.state.load(atomic::Ordering::Relaxed)) + .unwrap_or(ControllerState::Failed) } pub fn set_state(&self, state: ControllerState) { - *self.inner.state.lock().unwrap() = state; + self.inner + .state + .store(state.into(), atomic::Ordering::Relaxed); self.inner.notify.notify_waiters(); }