From bd4bf60697584899b81344cd13aad223796870a5 Mon Sep 17 00:00:00 2001 From: Ben Kimock Date: Tue, 23 Feb 2021 17:23:53 -0500 Subject: [PATCH] more cold, faster length tracking --- crates/proto/src/error.rs | 12 ++++++++++++ crates/proto/src/rr/domain/name.rs | 4 +++- 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/crates/proto/src/error.rs b/crates/proto/src/error.rs index 71ccb21696..e6252cec24 100644 --- a/crates/proto/src/error.rs +++ b/crates/proto/src/error.rs @@ -271,18 +271,21 @@ impl From for ProtoError { } impl From<&'static str> for ProtoError { + #[cold] fn from(msg: &'static str) -> ProtoError { ProtoErrorKind::Message(msg).into() } } impl From for ProtoError { + #[cold] fn from(msg: String) -> ProtoError { ProtoErrorKind::Msg(msg).into() } } impl From for ProtoError { + #[cold] fn from(e: io::Error) -> ProtoError { match e.kind() { io::ErrorKind::TimedOut => ProtoErrorKind::Timeout.into(), @@ -292,36 +295,42 @@ impl From for ProtoError { } impl From> for ProtoError { + #[cold] fn from(_e: sync::PoisonError) -> ProtoError { ProtoErrorKind::Poisoned.into() } } impl From for ProtoError { + #[cold] fn from(e: Unspecified) -> ProtoError { ProtoErrorKind::from(e).into() } } impl From for ProtoError { + #[cold] fn from(e: SslErrorStack) -> ProtoError { ProtoErrorKind::from(e).into() } } impl From for ProtoError { + #[cold] fn from(e: url::ParseError) -> ProtoError { ProtoErrorKind::from(e).into() } } impl From for ProtoError { + #[cold] fn from(e: std::str::Utf8Error) -> ProtoError { ProtoErrorKind::from(e).into() } } impl From for ProtoError { + #[cold] fn from(e: std::num::ParseIntError) -> ProtoError { ProtoErrorKind::from(e).into() } @@ -372,6 +381,7 @@ pub mod not_ring { } impl From for io::Error { + #[cold] fn from(e: ProtoError) -> Self { match *e.kind() { ProtoErrorKind::Timeout => io::Error::new(io::ErrorKind::TimedOut, e), @@ -381,6 +391,7 @@ impl From for io::Error { } impl From for String { + #[cold] fn from(e: ProtoError) -> Self { e.to_string() } @@ -388,6 +399,7 @@ impl From for String { #[cfg(feature = "wasm-bindgen")] impl From for wasm_bindgen_crate::JsValue { + #[cold] fn from(e: ProtoError) -> Self { js_sys::Error::new(&e.to_string()).into() } diff --git a/crates/proto/src/rr/domain/name.rs b/crates/proto/src/rr/domain/name.rs index ebb3fcb37f..343dc6351f 100644 --- a/crates/proto/src/rr/domain/name.rs +++ b/crates/proto/src/rr/domain/name.rs @@ -1059,6 +1059,7 @@ fn read_inner( ) -> ProtoResult<()> { let mut state: LabelParseState = LabelParseState::LabelLengthOrPointer; let name_start = decoder.index(); + let mut run_len: usize = labels.iter().map(Label::len).sum(); // assume all chars are utf-8. We're doing byte-by-byte operations, no endianess issues... // reserved: (1000 0000 aka 0800) && (0100 0000 aka 0400) @@ -1078,7 +1079,7 @@ fn read_inner( } // enforce max length of name - let cur_len = labels.iter().map(|l| l.len()).sum::() + labels.len(); + let cur_len = run_len + labels.len(); if cur_len > 255 { return Err(ProtoErrorKind::DomainNameTooLong(cur_len).into()); } @@ -1103,6 +1104,7 @@ fn read_inner( .verify_unwrap(|l| l.len() <= 63) .map_err(|_| ProtoError::from("label exceeds maximum length of 63"))?; + run_len += label.len(); labels.push(Label::from_raw_bytes(label).unwrap()); // reset to collect more data