Skip to content

Conversation

Cheong-Lau
Copy link
Contributor

@Cheong-Lau Cheong-Lau commented Oct 4, 2025

Some minor refactors like removing clone()s, replacing with references, avoiding allocating Vecs when they weren't necessary, etc.

There's also a new std::cmp::Ord impl for ActiveConnectionInfo, which should be heaps better than using format!. I had to also impl std::cmp::PartialCmp manually to do that, but the actual logic there shouldn't matter as long as it agrees with std::cmp::Ord.

@Cheong-Lau
Copy link
Contributor Author

oops, used str::from_utf8 instead of std::str::from_utf8. Turns out the former was only added in 1.87...

Comment on lines -180 to +196
let dots = if toplevels.is_empty() {
(0..1)
.map(|_| {
container(vertical_space().height(Length::Fixed(0.0)))
.padding(app_icon.dot_radius)
.into()
})
.collect_vec()
} else {
(0..1)
.map(|_| {
container(if toplevels.len() == 1 {
vertical_space().height(Length::Fixed(0.0))
} else {
match applet.anchor {
PanelAnchor::Left | PanelAnchor::Right => {
vertical_space().height(app_icon.bar_size)
}
PanelAnchor::Top | PanelAnchor::Bottom => {
horizontal_space().width(app_icon.bar_size)
}
}
})
.padding(app_icon.dot_radius)
.class(theme::style::Container::Custom(Box::new(move |theme| {
let dot_constructor = || {
let space = if toplevels.len() <= 1 {
vertical_space().height(Length::Fixed(0.0))
} else {
match applet.anchor {
PanelAnchor::Left | PanelAnchor::Right => {
vertical_space().height(app_icon.bar_size)
}
PanelAnchor::Top | PanelAnchor::Bottom => {
horizontal_space().width(app_icon.bar_size)
}
}
};
let mut container = container(space).padding(app_icon.dot_radius);

if !toplevels.is_empty() {
container =
container.class(theme::style::Container::Custom(Box::new(move |theme| {
Copy link
Contributor Author

@Cheong-Lau Cheong-Lau Oct 5, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I refactored this section completely, so I'd like a more thorough check of this part in particular.

I moved the logic of making the Elements into its own closure (better variable names are very welcome!), which should make the same Elements as before, just separate to the collection part and hopefully clearer. Then just make an iterator with std::iter::repeat_with, which avoids the whole mess of mapping and collecting an iterator.

@Cheong-Lau Cheong-Lau force-pushed the small-optimise branch 2 times, most recently from 772262f to 9f821d9 Compare October 7, 2025 11:54
@Cheong-Lau Cheong-Lau changed the title perf: slightly reduce memory allocations and cpu work perf: refactor to reduce memory allocations Oct 7, 2025
@Cheong-Lau
Copy link
Contributor Author

Cheong-Lau commented Oct 7, 2025

v2: I have now split the PR into two commits, one "safe" commit that should not change any behaviour, and one that uses unstable sorting, which could theoretically change behaviour but shouldn't in the places I've used them in.

This PR probably still needs testing, but this should hopefully make that easier to do.

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).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant