Skip to content

Commit 1ee803d

Browse files
committed
dns: Avoid allocating in Name::is_localhost (#1303)
`Name::is_localhost` implements comparison by parsing the string `localhost.` as a Name. This requires constructing a `String` as well as performing name parsing. We can avoid this work by obtaining a reference to the inner name string and using [`str::eq_ignore_ascii_case`][1]. [1]: https://doc.rust-lang.org/nightly/std/primitive.str.html#method.eq_ignore_ascii_case
1 parent e441233 commit 1ee803d

File tree

1 file changed

+5
-3
lines changed

1 file changed

+5
-3
lines changed

linkerd/dns/name/src/name.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,12 @@ pub struct Name(webpki::DNSName);
1313
pub struct InvalidName;
1414

1515
impl Name {
16+
#[inline]
1617
pub fn is_localhost(&self) -> bool {
17-
use std::str::FromStr;
18-
*self == Name::from_str("localhost.").unwrap()
18+
self.as_ref().eq_ignore_ascii_case("localhost.")
1919
}
2020

21+
#[inline]
2122
pub fn without_trailing_dot(&self) -> &str {
2223
self.as_ref().trim_end_matches('.')
2324
}
@@ -52,7 +53,7 @@ impl<'a> TryFrom<&'a [u8]> for Name {
5253
}
5354
}
5455

55-
impl<'a> std::str::FromStr for Name {
56+
impl std::str::FromStr for Name {
5657
type Err = InvalidName;
5758
fn from_str(s: &str) -> Result<Self, Self::Err> {
5859
Self::try_from(s.as_bytes())
@@ -72,6 +73,7 @@ impl<'t> From<&'t Name> for webpki::DNSNameRef<'t> {
7273
}
7374

7475
impl AsRef<str> for Name {
76+
#[inline]
7577
fn as_ref(&self) -> &str {
7678
<webpki::DNSName as AsRef<str>>::as_ref(&self.0)
7779
}

0 commit comments

Comments
 (0)