Skip to content

Commit 1429c31

Browse files
committed
perf: slightly reduce memory allocations and cpu work
1 parent 0c3e3c8 commit 1429c31

File tree

15 files changed

+136
-122
lines changed

15 files changed

+136
-122
lines changed

cosmic-app-list/src/app.rs

Lines changed: 23 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -590,7 +590,7 @@ fn find_desktop_entries<'a>(
590590
app_ids.iter().map(|fav| {
591591
let unicase_fav = fde::unicase::Ascii::new(fav.as_str());
592592
fde::find_app_by_id(desktop_entries, unicase_fav).map_or_else(
593-
|| fde::DesktopEntry::from_appid(fav.clone()).clone(),
593+
|| fde::DesktopEntry::from_appid(fav.clone()),
594594
ToOwned::to_owned,
595595
)
596596
})
@@ -612,7 +612,7 @@ impl CosmicAppList {
612612
.map(|(pinned_ctr, (e, original_id))| DockItem {
613613
id: pinned_ctr as u32,
614614
toplevels: Vec::new(),
615-
desktop_info: e.clone(),
615+
desktop_info: e,
616616
original_app_id: original_id.clone(),
617617
})
618618
.collect();
@@ -1490,7 +1490,10 @@ impl cosmic::Application for CosmicAppList {
14901490
} else {
14911491
0
14921492
};
1493-
let favorites: Vec<_> = (&mut self.pinned_list.iter().rev())
1493+
let favorites: Vec<_> = self
1494+
.pinned_list
1495+
.iter()
1496+
.rev()
14941497
.filter(|f| {
14951498
if favorite_to_remove > 0 && f.toplevels.is_empty() {
14961499
favorite_to_remove -= 1;
@@ -1812,11 +1815,7 @@ impl cosmic::Application for CosmicAppList {
18121815
Message::Exec(exec.to_string(), None, desktop_info.terminal()),
18131816
));
18141817
} else if let Some(gpus) = self.gpus.as_ref() {
1815-
let default_idx = if desktop_info.prefers_non_default_gpu() {
1816-
gpus.iter().position(|gpu| !gpu.default).unwrap_or(0)
1817-
} else {
1818-
gpus.iter().position(|gpu| gpu.default).unwrap_or(0)
1819-
};
1818+
let default_idx = preferred_gpu_idx(desktop_info, gpus.iter());
18201819
for (i, gpu) in gpus.iter().enumerate() {
18211820
content = content.push(
18221821
menu_button(text::body(format!(
@@ -2101,7 +2100,10 @@ impl cosmic::Application for CosmicAppList {
21012100
0
21022101
};
21032102
let mut favorites_extra = Vec::with_capacity(favorite_to_remove);
2104-
let mut favorites: Vec<_> = (&mut self.pinned_list.iter().rev())
2103+
let mut favorites: Vec<_> = self
2104+
.pinned_list
2105+
.iter()
2106+
.rev()
21052107
.filter(|f| {
21062108
if favorite_to_remove > 0 && f.toplevels.is_empty() {
21072109
favorite_to_remove -= 1;
@@ -2299,9 +2301,9 @@ impl CosmicAppList {
22992301
if self.active_workspaces.is_empty() {
23002302
return Vec::new();
23012303
}
2302-
let current_output = self.core.applet.output_name.clone();
2304+
let current_output = self.core.applet.output_name.as_ref();
23032305
let mut focused_toplevels: Vec<ExtForeignToplevelHandleV1> = Vec::new();
2304-
let active_workspaces = self.active_workspaces.clone();
2306+
let active_workspaces = &self.active_workspaces;
23052307
for toplevel_list in self.active_list.iter().chain(self.pinned_list.iter()) {
23062308
for (t_info, _) in &toplevel_list.toplevels {
23072309
if t_info.state.contains(&State::Activated)
@@ -2378,13 +2380,7 @@ impl CosmicAppList {
23782380
fn launch_on_preferred_gpu(desktop_info: &DesktopEntry, gpus: Option<&[Gpu]>) -> Option<Message> {
23792381
let exec = desktop_info.exec()?;
23802382

2381-
let gpu_idx = gpus.map(|gpus| {
2382-
if desktop_info.prefers_non_default_gpu() {
2383-
gpus.iter().position(|gpu| !gpu.default).unwrap_or(0)
2384-
} else {
2385-
gpus.iter().position(|gpu| gpu.default).unwrap_or(0)
2386-
}
2387-
});
2383+
let gpu_idx = gpus.map(|gpus| preferred_gpu_idx(desktop_info, gpus.iter()));
23882384

23892385
Some(Message::Exec(
23902386
exec.to_string(),
@@ -2393,6 +2389,14 @@ fn launch_on_preferred_gpu(desktop_info: &DesktopEntry, gpus: Option<&[Gpu]>) ->
23932389
))
23942390
}
23952391

2392+
fn preferred_gpu_idx<'a, I>(desktop_info: &DesktopEntry, mut gpus: I) -> usize
2393+
where
2394+
I: Iterator<Item = &'a Gpu>,
2395+
{
2396+
gpus.position(|gpu| gpu.default ^ desktop_info.prefers_non_default_gpu())
2397+
.unwrap_or(0)
2398+
}
2399+
23962400
#[derive(Debug, Default, Clone)]
23972401
pub struct DndPathBuf(PathBuf);
23982402

@@ -2428,8 +2432,6 @@ impl AsMimeTypes for DndPathBuf {
24282432
}
24292433

24302434
fn as_bytes(&self, _mime_type: &str) -> Option<std::borrow::Cow<'static, [u8]>> {
2431-
Some(Cow::Owned(
2432-
self.0.clone().to_str()?.to_string().into_bytes(),
2433-
))
2435+
Some(Cow::Owned(self.0.to_str()?.as_bytes().to_vec()))
24342436
}
24352437
}

cosmic-app-list/src/wayland_handler.rs

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -87,10 +87,7 @@ impl OutputHandler for AppData {
8787
if let Some(info) = self.output_state.info(&output) {
8888
let _ = self
8989
.tx
90-
.unbounded_send(WaylandUpdate::Output(OutputUpdate::Add(
91-
output.clone(),
92-
info.clone(),
93-
)));
90+
.unbounded_send(WaylandUpdate::Output(OutputUpdate::Add(output, info)));
9491
}
9592
}
9693

@@ -103,10 +100,7 @@ impl OutputHandler for AppData {
103100
if let Some(info) = self.output_state.info(&output) {
104101
let _ = self
105102
.tx
106-
.unbounded_send(WaylandUpdate::Output(OutputUpdate::Update(
107-
output.clone(),
108-
info.clone(),
109-
)));
103+
.unbounded_send(WaylandUpdate::Output(OutputUpdate::Update(output, info)));
110104
}
111105
}
112106

@@ -118,7 +112,7 @@ impl OutputHandler for AppData {
118112
) {
119113
let _ = self
120114
.tx
121-
.unbounded_send(WaylandUpdate::Output(OutputUpdate::Remove(output.clone())));
115+
.unbounded_send(WaylandUpdate::Output(OutputUpdate::Remove(output)));
122116
}
123117
}
124118

@@ -136,12 +130,12 @@ impl WorkspaceHandler for AppData {
136130
.iter()
137131
.filter_map(|handle| self.workspace_state.workspace_info(handle))
138132
.find(|w| w.state.contains(WorkspaceUpdateState::Active))
133+
.map(|workspace| workspace.handle.clone())
139134
})
140-
.map(|workspace| workspace.handle.clone())
141135
.collect::<Vec<_>>();
142136
let _ = self
143137
.tx
144-
.unbounded_send(WaylandUpdate::Workspace(active_workspaces.clone()));
138+
.unbounded_send(WaylandUpdate::Workspace(active_workspaces));
145139
}
146140
}
147141

@@ -699,7 +693,6 @@ pub(crate) fn wayland_handler(
699693
exit: false,
700694
tx,
701695
conn,
702-
queue_handle: qh.clone(),
703696
output_state: OutputState::new(&globals, &qh),
704697
workspace_state: WorkspaceState::new(&registry_state, &qh),
705698
toplevel_info_state: ToplevelInfoState::new(&registry_state, &qh),
@@ -709,6 +702,7 @@ pub(crate) fn wayland_handler(
709702
seat_state: SeatState::new(&globals, &qh),
710703
shm_state: Shm::bind(&globals, &qh).unwrap(),
711704
activation_state: ActivationState::bind::<AppData>(&globals, &qh).ok(),
705+
queue_handle: qh,
712706
};
713707

714708
loop {

cosmic-applet-audio/src/lib.rs

Lines changed: 10 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -499,11 +499,7 @@ impl cosmic::Application for Audio {
499499
self.inputs = sources
500500
.into_iter()
501501
.filter(|source| {
502-
!source
503-
.name
504-
.as_ref()
505-
.unwrap_or(&String::from("Generic"))
506-
.contains("monitor")
502+
!source.name.as_ref().is_some_and(|n| n.contains("monitor"))
507503
})
508504
.collect()
509505
}
@@ -830,15 +826,15 @@ impl cosmic::Application for Audio {
830826
self.is_open == IsOpen::Output,
831827
fl!("output"),
832828
match &self.current_output {
833-
Some(output) => pretty_name(output.description.clone()),
834-
None => String::from("No device selected"),
835-
},
829+
Some(output) => output.description.as_deref().unwrap_or("Generic"),
830+
None => "No device selected",
831+
}
832+
.to_string(),
836833
self.outputs
837-
.clone()
838-
.into_iter()
834+
.iter()
839835
.map(|output| (
840836
output.name.clone().unwrap_or_default(),
841-
pretty_name(output.description)
837+
output.description.clone().unwrap_or("Generic".into())
842838
))
843839
.collect(),
844840
Message::OutputToggle,
@@ -848,15 +844,14 @@ impl cosmic::Application for Audio {
848844
self.is_open == IsOpen::Input,
849845
fl!("input"),
850846
match &self.current_input {
851-
Some(input) => pretty_name(input.description.clone()),
847+
Some(input) => input.description.clone().unwrap_or("Generic".into()),
852848
None => fl!("no-device"),
853849
},
854850
self.inputs
855-
.clone()
856-
.into_iter()
851+
.iter()
857852
.map(|input| (
858853
input.name.clone().unwrap_or_default(),
859-
pretty_name(input.description)
854+
input.description.clone().unwrap_or("Generic".into())
860855
))
861856
.collect(),
862857
Message::InputToggle,
@@ -1020,13 +1015,6 @@ fn revealer_head(
10201015
.on_press(toggle)
10211016
}
10221017

1023-
fn pretty_name(name: Option<String>) -> String {
1024-
match name {
1025-
Some(n) => n,
1026-
None => String::from("Generic"),
1027-
}
1028-
}
1029-
10301018
#[derive(Default)]
10311019
enum PulseState {
10321020
#[default]

cosmic-applet-audio/src/mpris_subscription.rs

Lines changed: 2 additions & 2 deletions
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,
@@ -196,7 +196,7 @@ impl State {
196196
};
197197
self.players.push(player);
198198
filter_firefox_players(&mut self.players);
199-
self.players.sort_by(|a, b| a.name().cmp(b.name()));
199+
self.players.sort_unstable_by(|a, b| a.name().cmp(b.name()));
200200
self.update_any_player_state_stream().await;
201201
}
202202

cosmic-applet-bluetooth/src/app.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -370,7 +370,7 @@ impl cosmic::Application for CosmicBluetoothApplet {
370370
}) {
371371
let mut row = row![
372372
icon::from_name(dev.icon).size(16).symbolic(true),
373-
text::body(dev.name.clone())
373+
text::body(dev.name.as_str())
374374
.align_x(Alignment::Start)
375375
.align_y(Alignment::Center)
376376
.width(Length::Fill)

cosmic-applet-bluetooth/src/bluetooth.rs

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -249,12 +249,7 @@ impl Ord for BluerDevice {
249249
impl PartialOrd for BluerDevice {
250250
#[inline]
251251
fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
252-
match self.status.cmp(&other.status) {
253-
std::cmp::Ordering::Equal => {
254-
Some(self.name.to_lowercase().cmp(&other.name.to_lowercase()))
255-
}
256-
o => Some(o),
257-
}
252+
Some(self.cmp(other))
258253
}
259254
}
260255

@@ -813,6 +808,6 @@ async fn build_device_list(mut devices: Vec<BluerDevice>, adapter: &Adapter) ->
813808
devices.push(device);
814809
}
815810

816-
devices.sort();
811+
devices.sort_unstable();
817812
devices
818813
}

cosmic-applet-input-sources/src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -242,8 +242,8 @@ impl cosmic::Application for Window {
242242
widget::column::with_capacity(4 + self.active_layouts.len()).padding([8, 0]);
243243
for (id, layout) in self.active_layouts.iter().enumerate() {
244244
let group = widget::column::with_capacity(2)
245-
.push(widget::text::body(layout.description.clone()))
246-
.push(widget::text::caption(layout.layout.clone()));
245+
.push(widget::text::body(layout.description.as_str()))
246+
.push(widget::text::caption(layout.layout.as_str()));
247247
content_list = content_list
248248
.push(applet::menu_button(group).on_press(Message::SetActiveLayout(id)));
249249
}

cosmic-applet-minimize/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ impl cosmic::Application for Minimize {
173173
// Temporarily take ownership to appease the borrow checker.
174174
let mut apps = std::mem::take(&mut self.apps);
175175

176-
if let Some(pos) = apps.iter_mut().position(|a| {
176+
if let Some(pos) = apps.iter().position(|a| {
177177
a.toplevel_info.foreign_toplevel == toplevel_info.foreign_toplevel
178178
}) {
179179
if apps[pos].toplevel_info.app_id != toplevel_info.app_id {

cosmic-applet-minimize/src/wayland_handler.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -475,12 +475,12 @@ pub(crate) fn wayland_handler(
475475
exit: false,
476476
tx,
477477
conn,
478-
queue_handle: qh.clone(),
479478
shm_state,
480479
screencopy_state,
481480
seat_state: SeatState::new(&globals, &qh),
482481
toplevel_info_state: ToplevelInfoState::new(&registry_state, &qh),
483482
toplevel_manager_state: ToplevelManagerState::new(&registry_state, &qh),
483+
queue_handle: qh,
484484
registry_state,
485485
};
486486

cosmic-applet-network/src/app.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -370,7 +370,7 @@ impl cosmic::Application for CosmicNetworkApplet {
370370
} = &req
371371
{
372372
if let Some(NewConnectionState::Waiting(access_point)) =
373-
self.new_connection.clone()
373+
self.new_connection.as_ref()
374374
{
375375
if !success
376376
&& ssid == &access_point.ssid
@@ -384,7 +384,7 @@ impl cosmic::Application for CosmicNetworkApplet {
384384
}
385385
} else if let Some(NewConnectionState::EnterPassword {
386386
access_point, ..
387-
}) = self.new_connection.clone()
387+
}) = self.new_connection.as_ref()
388388
{
389389
if success && ssid == &access_point.ssid && *hw_address == access_point.hw_address {
390390
self.new_connection = None;

0 commit comments

Comments
 (0)