perf: refactor to reduce memory allocations and cpu work

This commit is contained in:
Cheong Lau 2025-10-05 16:36:59 +10:00 committed by Michael Murphy
parent 0c3e3c8629
commit dd0158d8f0
24 changed files with 234 additions and 242 deletions

View file

@ -71,15 +71,7 @@ pub async fn active_connections(
}
}
info.sort_by(|a, b| {
let helper = |conn: &ActiveConnectionInfo| match conn {
ActiveConnectionInfo::Vpn { name, .. } => format!("0{name}"),
ActiveConnectionInfo::Wired { name, .. } => format!("1{name}"),
ActiveConnectionInfo::WiFi { name, .. } => format!("2{name}"),
};
helper(a).cmp(&helper(b))
});
info.sort();
Ok(info)
}
@ -107,16 +99,74 @@ pub enum ActiveConnectionInfo {
impl ActiveConnectionInfo {
pub fn name(&self) -> String {
match &self {
Self::Wired { name, .. } => name.clone(),
Self::WiFi { name, .. } => name.clone(),
Self::Vpn { name, .. } => name.clone(),
Self::Wired { name, .. } | Self::WiFi { name, .. } | Self::Vpn { name, .. } => {
name.clone()
}
}
}
pub fn hw_address(&self) -> HwAddress {
match &self {
Self::Wired { hw_address, .. } => *hw_address,
Self::WiFi { hw_address, .. } => *hw_address,
Self::Wired { hw_address, .. } | Self::WiFi { hw_address, .. } => *hw_address,
Self::Vpn { .. } => HwAddress::default(),
}
}
}
impl std::cmp::Ord for ActiveConnectionInfo {
fn cmp(&self, other: &Self) -> std::cmp::Ordering {
match (self, other) {
(Self::Vpn { .. }, Self::Wired { .. } | Self::WiFi { .. })
| (Self::Wired { .. }, Self::WiFi { .. }) => std::cmp::Ordering::Greater,
(Self::WiFi { .. }, Self::Wired { .. } | Self::Vpn { .. })
| (Self::Wired { .. }, Self::Vpn { .. }) => std::cmp::Ordering::Less,
(Self::Vpn { name: n1, .. }, Self::Vpn { name: n2, .. })
| (Self::Wired { name: n1, .. }, Self::Wired { name: n2, .. })
| (Self::WiFi { name: n1, .. }, Self::WiFi { name: n2, .. }) => n1.cmp(n2),
}
}
}
impl std::cmp::Eq for ActiveConnectionInfo {}
impl std::cmp::PartialOrd for ActiveConnectionInfo {
fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
Some(self.cmp(other))
}
}
impl std::cmp::PartialEq for ActiveConnectionInfo {
fn eq(&self, other: &Self) -> bool {
match (self, other) {
(
Self::Wired {
name: n1,
hw_address: a1,
..
},
Self::Wired {
name: n2,
hw_address: a2,
..
},
)
| (
Self::WiFi {
name: n1,
hw_address: a1,
..
},
Self::WiFi {
name: n2,
hw_address: a2,
..
},
) => n1 == n2 && a1 == a2,
(Self::Vpn { name: n1, .. }, Self::Vpn { name: n2, .. }) => n1 == n2,
_ => false,
}
}
}