From 38c037c977cf50e33dda5e412186cd65ce3061d9 Mon Sep 17 00:00:00 2001 From: Cheong Lau <234708519+Cheong-Lau@users.noreply.github.com> Date: Tue, 7 Oct 2025 21:50:08 +1000 Subject: [PATCH] perf: use unstable sorting when async Unstable sorting should be slightly faster than stable sorting, and if the vector was built asynchronously then preserving the initial order doesn't matter too much. Continue to use stable sorting where the vector is not built asynchronously, or if the vector is partially sorted (e.g. when new elements are pushed to a sorted vector). --- cosmic-applet-audio/src/mpris_subscription.rs | 2 +- cosmic-applet-bluetooth/src/bluetooth.rs | 2 +- cosmic-applet-network/src/network_manager/available_wifi.rs | 2 +- cosmic-applet-network/src/network_manager/current_networks.rs | 2 +- cosmic-applet-network/src/network_manager/mod.rs | 4 ++-- 5 files changed, 6 insertions(+), 6 deletions(-) diff --git a/cosmic-applet-audio/src/mpris_subscription.rs b/cosmic-applet-audio/src/mpris_subscription.rs index 88de6144..c2e010dc 100644 --- a/cosmic-applet-audio/src/mpris_subscription.rs +++ b/cosmic-applet-audio/src/mpris_subscription.rs @@ -171,7 +171,7 @@ impl State { filter_firefox_players(&mut players); // pre-sort by path so that the same player is always selected - players.sort_by(|a, b| a.name().cmp(b.name())); + players.sort_unstable_by(|a, b| a.name().cmp(b.name())); let mut state = Self { conn, diff --git a/cosmic-applet-bluetooth/src/bluetooth.rs b/cosmic-applet-bluetooth/src/bluetooth.rs index 5aff2ab5..76bed728 100644 --- a/cosmic-applet-bluetooth/src/bluetooth.rs +++ b/cosmic-applet-bluetooth/src/bluetooth.rs @@ -808,6 +808,6 @@ async fn build_device_list(mut devices: Vec, adapter: &Adapter) -> devices.push(device); } - devices.sort(); + devices.sort_unstable(); devices } diff --git a/cosmic-applet-network/src/network_manager/available_wifi.rs b/cosmic-applet-network/src/network_manager/available_wifi.rs index 843dee12..c6be8762 100644 --- a/cosmic-applet-network/src/network_manager/available_wifi.rs +++ b/cosmic-applet-network/src/network_manager/available_wifi.rs @@ -77,7 +77,7 @@ pub async fn handle_wireless_device( ); } let mut aps = aps.into_values().collect::>(); - aps.sort_by_key(|ap| ap.strength); + aps.sort_unstable_by_key(|ap| ap.strength); Ok(aps) } diff --git a/cosmic-applet-network/src/network_manager/current_networks.rs b/cosmic-applet-network/src/network_manager/current_networks.rs index d16f1667..a92e0418 100644 --- a/cosmic-applet-network/src/network_manager/current_networks.rs +++ b/cosmic-applet-network/src/network_manager/current_networks.rs @@ -71,7 +71,7 @@ pub async fn active_connections( } } - info.sort(); + info.sort_unstable(); Ok(info) } diff --git a/cosmic-applet-network/src/network_manager/mod.rs b/cosmic-applet-network/src/network_manager/mod.rs index 88e9802b..96f28f2b 100644 --- a/cosmic-applet-network/src/network_manager/mod.rs +++ b/cosmic-applet-network/src/network_manager/mod.rs @@ -420,7 +420,7 @@ impl NetworkManagerState { let s = NetworkManagerSettings::new(conn).await?; _ = s.load_connections(&[]).await; let known_conns = s.list_connections().await.unwrap_or_default(); - let mut active_conns = active_connections( + let active_conns = active_connections( network_manager .active_connections() .await @@ -428,7 +428,7 @@ impl NetworkManagerState { ) .await .unwrap_or_default(); - active_conns.sort(); + // active_conns.sort(); active_connections should have already sorted the vector let devices = network_manager.devices().await.ok().unwrap_or_default(); let wireless_access_point_futures: Vec<_> = devices .into_iter()