Skip to content

Commit cf8f32d

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

File tree

20 files changed

+189
-187
lines changed

20 files changed

+189
-187
lines changed

cosmic-app-list/src/app.rs

Lines changed: 53 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -177,31 +177,24 @@ impl DockItem {
177177
.width(app_icon.icon_size.into())
178178
.height(app_icon.icon_size.into());
179179

180-
let dots = if toplevels.is_empty() {
181-
(0..1)
182-
.map(|_| {
183-
container(vertical_space().height(Length::Fixed(0.0)))
184-
.padding(app_icon.dot_radius)
185-
.into()
186-
})
187-
.collect_vec()
188-
} else {
189-
(0..1)
190-
.map(|_| {
191-
container(if toplevels.len() == 1 {
192-
vertical_space().height(Length::Fixed(0.0))
193-
} else {
194-
match applet.anchor {
195-
PanelAnchor::Left | PanelAnchor::Right => {
196-
vertical_space().height(app_icon.bar_size)
197-
}
198-
PanelAnchor::Top | PanelAnchor::Bottom => {
199-
horizontal_space().width(app_icon.bar_size)
200-
}
201-
}
202-
})
203-
.padding(app_icon.dot_radius)
204-
.class(theme::style::Container::Custom(Box::new(move |theme| {
180+
let dot_constructor = || {
181+
let space = if toplevels.len() <= 1 {
182+
vertical_space().height(Length::Fixed(0.0))
183+
} else {
184+
match applet.anchor {
185+
PanelAnchor::Left | PanelAnchor::Right => {
186+
vertical_space().height(app_icon.bar_size)
187+
}
188+
PanelAnchor::Top | PanelAnchor::Bottom => {
189+
horizontal_space().width(app_icon.bar_size)
190+
}
191+
}
192+
};
193+
let mut container = container(space).padding(app_icon.dot_radius);
194+
195+
if !toplevels.is_empty() {
196+
container =
197+
container.class(theme::style::Container::Custom(Box::new(move |theme| {
205198
container::Style {
206199
text_color: Some(Color::TRANSPARENT),
207200
background: if is_focused {
@@ -218,34 +211,35 @@ impl DockItem {
218211
icon_color: Some(Color::TRANSPARENT),
219212
}
220213
})))
221-
.into()
222-
})
223-
.collect_vec()
214+
}
215+
container.into()
224216
};
225217

218+
let dots = [dot_constructor(), dot_constructor()];
219+
226220
let icon_wrapper: Element<_> = match applet.anchor {
227-
PanelAnchor::Left => row(vec![
221+
PanelAnchor::Left => row([
228222
column(dots).into(),
229223
horizontal_space().width(Length::Fixed(1.0)).into(),
230224
cosmic_icon.clone().into(),
231225
])
232226
.align_y(Alignment::Center)
233227
.into(),
234-
PanelAnchor::Right => row(vec![
228+
PanelAnchor::Right => row([
235229
cosmic_icon.clone().into(),
236230
horizontal_space().width(Length::Fixed(1.0)).into(),
237231
column(dots).into(),
238232
])
239233
.align_y(Alignment::Center)
240234
.into(),
241-
PanelAnchor::Top => column(vec![
235+
PanelAnchor::Top => column([
242236
row(dots).into(),
243237
vertical_space().height(Length::Fixed(1.0)).into(),
244238
cosmic_icon.clone().into(),
245239
])
246240
.align_x(Alignment::Center)
247241
.into(),
248-
PanelAnchor::Bottom => column(vec![
242+
PanelAnchor::Bottom => column([
249243
cosmic_icon.clone().into(),
250244
vertical_space().height(Length::Fixed(1.0)).into(),
251245
row(dots).into(),
@@ -470,7 +464,7 @@ where
470464
img.img.clone(),
471465
)))
472466
} else {
473-
Image::new(Handle::from_rgba(1, 1, vec![0, 0, 0, 255])).into()
467+
Image::new(Handle::from_rgba(1, 1, [0u8, 0u8, 0u8, 255u8].as_slice())).into()
474468
})
475469
.class(Container::Custom(Box::new(move |theme| {
476470
container::Style {
@@ -590,7 +584,7 @@ fn find_desktop_entries<'a>(
590584
app_ids.iter().map(|fav| {
591585
let unicase_fav = fde::unicase::Ascii::new(fav.as_str());
592586
fde::find_app_by_id(desktop_entries, unicase_fav).map_or_else(
593-
|| fde::DesktopEntry::from_appid(fav.clone()).clone(),
587+
|| fde::DesktopEntry::from_appid(fav.clone()),
594588
ToOwned::to_owned,
595589
)
596590
})
@@ -612,7 +606,7 @@ impl CosmicAppList {
612606
.map(|(pinned_ctr, (e, original_id))| DockItem {
613607
id: pinned_ctr as u32,
614608
toplevels: Vec::new(),
615-
desktop_info: e.clone(),
609+
desktop_info: e,
616610
original_app_id: original_id.clone(),
617611
})
618612
.collect();
@@ -673,7 +667,7 @@ impl cosmic::Application for CosmicAppList {
673667
} else {
674668
self.overflow_active_popup = None;
675669
self.overflow_favorites_popup = None;
676-
return Task::batch(vec![destroy_popup(popup_id), destroy_popup(parent)]);
670+
return Task::batch([destroy_popup(popup_id), destroy_popup(parent)]);
677671
}
678672
}
679673
if let Some(toplevel_group) = self
@@ -733,7 +727,7 @@ impl cosmic::Application for CosmicAppList {
733727
} else {
734728
self.overflow_active_popup = None;
735729
self.overflow_favorites_popup = None;
736-
return Task::batch(vec![destroy_popup(popup_id), destroy_popup(parent)]);
730+
return Task::batch([destroy_popup(popup_id), destroy_popup(parent)]);
737731
}
738732
}
739733
if let Some(toplevel_group) = self
@@ -1490,7 +1484,10 @@ impl cosmic::Application for CosmicAppList {
14901484
} else {
14911485
0
14921486
};
1493-
let favorites: Vec<_> = (&mut self.pinned_list.iter().rev())
1487+
let favorites: Vec<_> = self
1488+
.pinned_list
1489+
.iter()
1490+
.rev()
14941491
.filter(|f| {
14951492
if favorite_to_remove > 0 && f.toplevels.is_empty() {
14961493
favorite_to_remove -= 1;
@@ -1812,11 +1809,7 @@ impl cosmic::Application for CosmicAppList {
18121809
Message::Exec(exec.to_string(), None, desktop_info.terminal()),
18131810
));
18141811
} 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-
};
1812+
let default_idx = preferred_gpu_idx(desktop_info, gpus.iter());
18201813
for (i, gpu) in gpus.iter().enumerate() {
18211814
content = content.push(
18221815
menu_button(text::body(format!(
@@ -2101,7 +2094,10 @@ impl cosmic::Application for CosmicAppList {
21012094
0
21022095
};
21032096
let mut favorites_extra = Vec::with_capacity(favorite_to_remove);
2104-
let mut favorites: Vec<_> = (&mut self.pinned_list.iter().rev())
2097+
let mut favorites: Vec<_> = self
2098+
.pinned_list
2099+
.iter()
2100+
.rev()
21052101
.filter(|f| {
21062102
if favorite_to_remove > 0 && f.toplevels.is_empty() {
21072103
favorite_to_remove -= 1;
@@ -2190,7 +2186,7 @@ impl cosmic::Application for CosmicAppList {
21902186
}
21912187

21922188
fn subscription(&self) -> Subscription<Message> {
2193-
Subscription::batch(vec![
2189+
Subscription::batch([
21942190
wayland_subscription().map(Message::Wayland),
21952191
listen_with(|e, _, id| match e {
21962192
cosmic::iced_runtime::core::Event::PlatformSpecific(
@@ -2299,9 +2295,9 @@ impl CosmicAppList {
22992295
if self.active_workspaces.is_empty() {
23002296
return Vec::new();
23012297
}
2302-
let current_output = self.core.applet.output_name.clone();
2298+
let current_output = self.core.applet.output_name.as_ref();
23032299
let mut focused_toplevels: Vec<ExtForeignToplevelHandleV1> = Vec::new();
2304-
let active_workspaces = self.active_workspaces.clone();
2300+
let active_workspaces = &self.active_workspaces;
23052301
for toplevel_list in self.active_list.iter().chain(self.pinned_list.iter()) {
23062302
for (t_info, _) in &toplevel_list.toplevels {
23072303
if t_info.state.contains(&State::Activated)
@@ -2378,13 +2374,7 @@ impl CosmicAppList {
23782374
fn launch_on_preferred_gpu(desktop_info: &DesktopEntry, gpus: Option<&[Gpu]>) -> Option<Message> {
23792375
let exec = desktop_info.exec()?;
23802376

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-
});
2377+
let gpu_idx = gpus.map(|gpus| preferred_gpu_idx(desktop_info, gpus.iter()));
23882378

23892379
Some(Message::Exec(
23902380
exec.to_string(),
@@ -2393,6 +2383,14 @@ fn launch_on_preferred_gpu(desktop_info: &DesktopEntry, gpus: Option<&[Gpu]>) ->
23932383
))
23942384
}
23952385

2386+
fn preferred_gpu_idx<'a, I>(desktop_info: &DesktopEntry, mut gpus: I) -> usize
2387+
where
2388+
I: Iterator<Item = &'a Gpu>,
2389+
{
2390+
gpus.position(|gpu| gpu.default ^ desktop_info.prefers_non_default_gpu())
2391+
.unwrap_or(0)
2392+
}
2393+
23962394
#[derive(Debug, Default, Clone)]
23972395
pub struct DndPathBuf(PathBuf);
23982396

@@ -2428,8 +2426,6 @@ impl AsMimeTypes for DndPathBuf {
24282426
}
24292427

24302428
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-
))
2429+
Some(Cow::Owned(self.0.to_str()?.as_bytes().to_vec()))
24342430
}
24352431
}

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-a11y/src/app.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -401,7 +401,7 @@ impl cosmic::Application for CosmicA11yApplet {
401401
}
402402

403403
fn subscription(&self) -> Subscription<Message> {
404-
Subscription::batch(vec![
404+
Subscription::batch([
405405
accessibility::subscription().map(Message::DBusUpdate),
406406
backend::wayland::a11y_subscription().map(Message::WaylandUpdate),
407407
self.timeline

0 commit comments

Comments
 (0)