Skip to content

Commit 9f821d9

Browse files
committed
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).
1 parent 4e5e829 commit 9f821d9

File tree

5 files changed

+6
-6
lines changed

5 files changed

+6
-6
lines changed

cosmic-applet-audio/src/mpris_subscription.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ impl State {
171171
filter_firefox_players(&mut players);
172172

173173
// pre-sort by path so that the same player is always selected
174-
players.sort_by(|a, b| a.name().cmp(b.name()));
174+
players.sort_unstable_by(|a, b| a.name().cmp(b.name()));
175175

176176
let mut state = Self {
177177
conn,

cosmic-applet-bluetooth/src/bluetooth.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -808,6 +808,6 @@ async fn build_device_list(mut devices: Vec<BluerDevice>, adapter: &Adapter) ->
808808
devices.push(device);
809809
}
810810

811-
devices.sort();
811+
devices.sort_unstable();
812812
devices
813813
}

cosmic-applet-network/src/network_manager/available_wifi.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ pub async fn handle_wireless_device(
7777
);
7878
}
7979
let mut aps = aps.into_values().collect::<Vec<_>>();
80-
aps.sort_by_key(|ap| ap.strength);
80+
aps.sort_unstable_by_key(|ap| ap.strength);
8181
Ok(aps)
8282
}
8383

cosmic-applet-network/src/network_manager/current_networks.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ pub async fn active_connections(
7171
}
7272
}
7373

74-
info.sort();
74+
info.sort_unstable();
7575
Ok(info)
7676
}
7777

cosmic-applet-network/src/network_manager/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -420,15 +420,15 @@ impl NetworkManagerState {
420420
let s = NetworkManagerSettings::new(conn).await?;
421421
_ = s.load_connections(&[]).await;
422422
let known_conns = s.list_connections().await.unwrap_or_default();
423-
let mut active_conns = active_connections(
423+
let active_conns = active_connections(
424424
network_manager
425425
.active_connections()
426426
.await
427427
.unwrap_or_default(),
428428
)
429429
.await
430430
.unwrap_or_default();
431-
active_conns.sort();
431+
// active_conns.sort(); active_connections should have already sorted the vector
432432
let devices = network_manager.devices().await.ok().unwrap_or_default();
433433
let wireless_access_point_futures: Vec<_> = devices
434434
.into_iter()

0 commit comments

Comments
 (0)