Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optimize name parsing #1388

Merged
merged 4 commits into from Mar 8, 2021
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 1 addition & 10 deletions crates/client/src/rr/lower_name.rs
Expand Up @@ -11,14 +11,13 @@ use std::borrow::Borrow;
use std::cmp::{Ordering, PartialEq};
use std::fmt;
use std::hash::{Hash, Hasher};
use std::ops::Index;
use std::str::FromStr;

use crate::proto::error::*;
#[cfg(feature = "serde-config")]
use serde::{de, Deserialize, Deserializer, Serialize, Serializer};

use crate::rr::{Label, Name};
use crate::rr::Name;
use crate::serialize::binary::*;

/// them should be through references. As a workaround the Strings are all Rc as well as the array
Expand Down Expand Up @@ -191,14 +190,6 @@ impl fmt::Display for LowerName {
}
}

impl Index<usize> for LowerName {
type Output = Label;

fn index(&self, _index: usize) -> &Label {
&(self.0[_index])
}
}

impl PartialOrd<LowerName> for LowerName {
fn partial_cmp(&self, other: &LowerName) -> Option<Ordering> {
Some(self.cmp(other))
Expand Down
1 change: 1 addition & 0 deletions crates/proto/Cargo.toml
Expand Up @@ -78,6 +78,7 @@ serde = { version = "1.0", features = ["derive"], optional = true }
smallvec = "1.6"
socket2 = { version = "0.3.16", optional = true }
thiserror = "1.0.20"
tinyvec = { version = "1.1.1", features = ["alloc"] }
tokio = { version = "1.0", optional = true }
url = "2.1.0"
wasm-bindgen-crate = { version = "0.2.58", optional = true, package = "wasm-bindgen" }
Expand Down
21 changes: 21 additions & 0 deletions crates/proto/benches/lib.rs
Expand Up @@ -121,3 +121,24 @@ fn bench_parse_message(b: &mut Bencher) {
Message::read(&mut decoder)
})
}

#[bench]
fn bench_parse_real_message(b: &mut Bencher) {
let bytes = [
145, 188, 129, 128, 0, 1, 0, 6, 0, 0, 0, 0, 5, 118, 105, 100, 101, 111, 5, 116, 119, 105,
109, 103, 3, 99, 111, 109, 0, 0, 1, 0, 1, 192, 12, 0, 5, 0, 1, 0, 0, 0, 245, 0, 11, 8, 118,
105, 100, 101, 111, 45, 97, 107, 192, 18, 192, 45, 0, 5, 0, 1, 0, 0, 12, 213, 0, 24, 5,
118, 105, 100, 101, 111, 5, 116, 119, 105, 109, 103, 6, 97, 107, 97, 100, 110, 115, 3, 110,
101, 116, 0, 192, 68, 0, 5, 0, 1, 0, 0, 0, 57, 0, 28, 5, 118, 105, 100, 101, 111, 5, 116,
119, 105, 109, 103, 3, 99, 111, 109, 9, 97, 107, 97, 109, 97, 105, 122, 101, 100, 192, 87,
192, 104, 0, 5, 0, 1, 0, 0, 2, 194, 0, 22, 5, 118, 105, 100, 101, 111, 5, 116, 119, 105,
109, 103, 3, 99, 111, 109, 3, 101, 105, 112, 192, 80, 192, 144, 0, 5, 0, 1, 0, 0, 0, 43, 0,
35, 8, 101, 105, 112, 45, 116, 97, 116, 97, 5, 118, 105, 100, 101, 111, 5, 116, 119, 105,
109, 103, 3, 99, 111, 109, 7, 97, 107, 97, 104, 111, 115, 116, 192, 87, 192, 178, 0, 1, 0,
1, 0, 0, 0, 23, 0, 4, 184, 31, 3, 236,
];
b.iter(|| {
let mut decoder = BinDecoder::new(&bytes[..]);
Message::read(&mut decoder);
})
}
12 changes: 6 additions & 6 deletions crates/proto/src/rr/domain/label.rs
Expand Up @@ -17,7 +17,7 @@ use std::borrow::Borrow;
use std::cmp::{Ordering, PartialEq};
use std::fmt::{self, Debug, Display, Formatter, Write};
use std::hash::{Hash, Hasher};
use std::sync::Arc as Rc;
use tinyvec::TinyVec;

use idna;
use log::debug;
Expand All @@ -29,7 +29,7 @@ const IDNA_PREFIX: &[u8] = b"xn--";

/// Labels are always stored as ASCII, unicode characters must be encoded with punycode
#[derive(Clone, Eq)]
pub struct Label(Rc<[u8]>);
pub struct Label(TinyVec<[u8; 24]>);

impl Label {
/// These must only be ASCII, with unicode encoded to PunyCode, or other such transformation.
Expand All @@ -40,7 +40,7 @@ impl Label {
if bytes.len() > 63 {
return Err(format!("Label exceeds maximum length 63: {}", bytes.len()).into());
};
Ok(Label(Rc::from(bytes)))
Ok(Label(TinyVec::from(bytes)))
}

/// Translates this string into IDNA safe name, encoding to punycode as necessary.
Expand Down Expand Up @@ -86,7 +86,7 @@ impl Label {

/// Returns a new Label of the Wildcard, i.e. "*"
pub fn wildcard() -> Self {
Label(Rc::from(WILDCARD.to_vec()))
Label(TinyVec::from(WILDCARD))
}

/// Converts this label to lowercase
Expand All @@ -100,7 +100,7 @@ impl Label {
{
let mut lower_label: Vec<u8> = self.0.to_vec();
lower_label[idx..].make_ascii_lowercase();
Label(Rc::from(lower_label))
Label(TinyVec::from(lower_label.as_slice()))
} else {
self.clone()
}
Expand Down Expand Up @@ -201,7 +201,7 @@ impl Label {

impl AsRef<[u8]> for Label {
fn as_ref(&self) -> &[u8] {
&self.0
self.as_bytes()
}
}

Expand Down