Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 21 additions & 15 deletions src/link/link_info/infos.rs
Original file line number Diff line number Diff line change
Expand Up @@ -302,18 +302,9 @@ impl Nla for InfoKind {
}
}

impl<'a, T: AsRef<[u8]> + ?Sized> Parseable<NlaBuffer<&'a T>> for InfoKind {
fn parse(buf: &NlaBuffer<&'a T>) -> Result<InfoKind, DecodeError> {
if buf.kind() != IFLA_INFO_KIND {
return Err(format!(
"failed to parse IFLA_INFO_KIND: NLA type is {}",
buf.kind()
)
.into());
}
let s = parse_string(buf.value())
.context("invalid IFLA_INFO_KIND value")?;
Ok(match s.as_str() {
impl From<&str> for InfoKind {
fn from(s: &str) -> Self {
match s {
DUMMY => Self::Dummy,
IFB => Self::Ifb,
BRIDGE => Self::Bridge,
Expand All @@ -339,13 +330,28 @@ impl<'a, T: AsRef<[u8]> + ?Sized> Parseable<NlaBuffer<&'a T>> for InfoKind {
GTP => Self::Gtp,
IPOIB => Self::Ipoib,
WIREGUARD => Self::Wireguard,
MACSEC => Self::MacSec,
XFRM => Self::Xfrm,
MACSEC => Self::MacSec,
HSR => Self::Hsr,
GENEVE => Self::Geneve,
NETKIT => Self::Netkit,
VXCAN => Self::Vxcan,
_ => Self::Other(s),
})
_ => Self::Other(s.to_owned()),
}
}
}

impl<'a, T: AsRef<[u8]> + ?Sized> Parseable<NlaBuffer<&'a T>> for InfoKind {
fn parse(buf: &NlaBuffer<&'a T>) -> Result<InfoKind, DecodeError> {
if buf.kind() != IFLA_INFO_KIND {
return Err(format!(
"failed to parse IFLA_INFO_KIND: NLA type is {}",
buf.kind()
)
.into());
}
let s = parse_string(buf.value())
.context("invalid IFLA_INFO_KIND value")?;
Ok(s.as_str().into())

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

medium

The refactoring to use From<&str> introduces a double allocation when parsing an unknown InfoKind (the Other variant).

parse_string returns a String. Calling s.as_str().into() then invokes InfoKind::from(&str), which calls s.to_owned() to create a second String for the Other variant, after which the original String is dropped.

While this only affects unknown link types, it is a performance regression compared to the previous implementation which moved the String into the Other variant. To resolve this while keeping the code clean, you could implement From<String> for InfoKind (which would move the string in the Other case) and use Ok(s.into()) here.

}
}
Loading