Skip to content

Commit

Permalink
perf: improve inlining (#37)
Browse files Browse the repository at this point in the history
  • Loading branch information
jrandolf authored Feb 6, 2023
1 parent 914c23b commit c31d31f
Show file tree
Hide file tree
Showing 3 changed files with 0 additions and 35 deletions.
7 changes: 0 additions & 7 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,6 @@ impl AddrSpec {

/// Creates a new address specification. This will validate the local part
/// and domain and perform NFC-normalization.
#[inline]
pub fn new<LocalPart, Domain>(local_part: LocalPart, domain: Domain) -> Result<Self, ParseError>
where
LocalPart: AsRef<str>,
Expand All @@ -149,7 +148,6 @@ impl AddrSpec {
/// Creates a new address specification with a literal domain. This will
/// validate the local part and domain and perform NFC-normalization.
#[cfg(feature = "literals")]
#[inline]
pub fn with_literal<LocalPart, Domain>(
local_part: LocalPart,
domain: Domain,
Expand All @@ -161,7 +159,6 @@ impl AddrSpec {
Self::new_impl(local_part.as_ref(), domain.as_ref(), true)
}

#[inline]
fn new_impl(local_part: &str, domain: &str, literal: bool) -> Result<Self, ParseError> {
if let Some(index) = local_part.find(is_ascii_control_and_not_htab) {
return Err(ParseError("invalid character in local part", index));
Expand Down Expand Up @@ -234,7 +231,6 @@ impl AddrSpec {
Self::new_unchecked_impl(local_part.into(), domain.into(), true)
}

#[inline]
#[allow(unused_variables)]
unsafe fn new_unchecked_impl(local_part: String, domain: String, literal: bool) -> Self {
Self {
Expand Down Expand Up @@ -285,7 +281,6 @@ impl AddrSpec {
/// This is useful if you need to transport the address specification over
/// line-based protocols such as SMTP and need to ensure that the local part
/// and domain fit on a single line or require folding white-spaces.
#[inline]
pub fn into_serialized_parts(self) -> (String, String) {
// Note literals will be optimized away by the compiler if the feature
// is disabled.
Expand All @@ -306,7 +301,6 @@ impl AddrSpec {

#[cfg(feature = "nightly")]
impl ToString for AddrSpec {
#[inline]
fn to_string(&self) -> String {
// Note literals will be optimized away by the compiler if the feature
// is disabled.
Expand All @@ -320,7 +314,6 @@ impl ToString for AddrSpec {
}

impl fmt::Display for AddrSpec {
#[inline]
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
if !self.is_quoted() {
formatter.write_str(self.local_part())?;
Expand Down
26 changes: 0 additions & 26 deletions src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,14 @@ use std::{fmt, mem::ManuallyDrop, str::Chars};
use super::unicode;
use super::AddrSpec;

#[inline]
pub const fn is_ascii_control_and_not_htab(chr: char) -> bool {
chr.is_ascii_control() && chr != '\t'
}

#[inline]
pub const fn is_ascii_control_or_space(chr: char) -> bool {
chr.is_ascii_control() || chr == ' '
}

#[inline]
pub const fn is_not_atext(chr: char) -> bool {
is_ascii_control_or_space(chr)
|| matches!(
Expand All @@ -22,7 +19,6 @@ pub const fn is_not_atext(chr: char) -> bool {
)
}

#[inline]
pub const fn is_not_dtext(chr: char) -> bool {
is_ascii_control_or_space(chr) || matches!(chr, '[' | ']' | '\\')
}
Expand Down Expand Up @@ -70,7 +66,6 @@ impl<'a> Parser<'a> {
}
}

#[inline]
pub fn parse(mut self) -> Result<AddrSpec, ParseError> {
#[cfg(feature = "white-spaces")]
self.parse_cfws()?;
Expand All @@ -95,7 +90,6 @@ impl<'a> Parser<'a> {
}

#[cfg(feature = "white-spaces")]
#[inline]
fn parse_cfws(&mut self) -> Result<(), ParseError> {
self.skip_fws();
#[cfg(feature = "comments")]
Expand All @@ -107,7 +101,6 @@ impl<'a> Parser<'a> {
}

#[cfg(feature = "white-spaces")]
#[inline]
fn skip_fws(&mut self) {
self.skip_ws();
if !self.eat_str("\r\n") {
Expand All @@ -117,7 +110,6 @@ impl<'a> Parser<'a> {
}

#[cfg(feature = "white-spaces")]
#[inline]
fn skip_ws(&mut self) {
loop {
if !self.eat_slice([' ', '\t']) {
Expand All @@ -127,7 +119,6 @@ impl<'a> Parser<'a> {
}

#[cfg(feature = "white-spaces")]
#[inline]
fn eat_slice<const N: usize>(&mut self, pattern: [char; N]) -> bool {
if self.iterator.as_str().starts_with(pattern) {
self.iterator.next();
Expand All @@ -137,7 +128,6 @@ impl<'a> Parser<'a> {
}

#[cfg(feature = "white-spaces")]
#[inline]
fn eat_str(&mut self, pattern: &str) -> bool {
if let Some(input) = self.iterator.as_str().strip_prefix(pattern) {
self.iterator = input.chars();
Expand All @@ -146,7 +136,6 @@ impl<'a> Parser<'a> {
false
}

#[inline]
fn eat_chr(&mut self, pattern: char) -> bool {
if self.iterator.as_str().starts_with(pattern) {
self.iterator.next();
Expand All @@ -156,7 +145,6 @@ impl<'a> Parser<'a> {
}

#[cfg(feature = "comments")]
#[inline]
fn parse_comment(&mut self) -> Result<(), ParseError> {
#[cfg(feature = "white-spaces")]
self.skip_fws();
Expand Down Expand Up @@ -190,7 +178,6 @@ impl<'a> Parser<'a> {
Err(self.error("expected ')' for comment", 0))
}

#[inline]
fn parse_quoted_pair(&mut self) -> Result<char, ParseError> {
match self.iterator.next() {
Some(chr) if !is_ascii_control_and_not_htab(chr) => Ok(chr),
Expand All @@ -199,7 +186,6 @@ impl<'a> Parser<'a> {
}
}

#[inline]
fn parse_local_part(&mut self) -> Result<String, ParseError> {
if !self.eat_chr('"') {
return Ok(unicode::normalize(
Expand All @@ -212,7 +198,6 @@ impl<'a> Parser<'a> {
)?))
}

#[inline]
pub fn parse_dot_atom(
&mut self,
empty_label_error_text: &'static str,
Expand All @@ -233,7 +218,6 @@ impl<'a> Parser<'a> {
Ok(dot_atom)
}

#[inline]
fn parse_quoted_string(
&mut self,
invalid_character_error_text: &'static str,
Expand Down Expand Up @@ -263,15 +247,13 @@ impl<'a> Parser<'a> {
Err(self.error(expected_quote_error_text, 0))
}

#[inline]
fn skip_at(&mut self) -> Result<(), ParseError> {
if self.eat_chr('@') {
return Ok(());
}
Err(self.error("expected '@'", 1))
}

#[inline]
fn parse_domain(&mut self) -> Result<(String, bool), ParseError> {
#[cfg(feature = "literals")]
if self.eat_chr('[') {
Expand All @@ -284,7 +266,6 @@ impl<'a> Parser<'a> {
}

#[cfg(all(feature = "literals", not(feature = "white-spaces")))]
#[inline]
fn parse_domain_literal(&mut self) -> Result<&str, ParseError> {
let input = self.iterator.as_str();
let size = input.find(is_not_dtext).unwrap_or(input.len());
Expand All @@ -298,7 +279,6 @@ impl<'a> Parser<'a> {
}

#[cfg(all(feature = "literals", feature = "white-spaces"))]
#[inline]
fn parse_domain_literal(&mut self) -> Result<String, ParseError> {
#[cfg(feature = "white-spaces")]
self.skip_fws();
Expand Down Expand Up @@ -331,7 +311,6 @@ impl<'a> Parser<'a> {
Err(self.error(message, 0))
}

#[inline(always)]
fn error(&self, message: &'static str, offset: isize) -> ParseError {
ParseError(
message,
Expand All @@ -349,7 +328,6 @@ pub struct FixedVec<T> {
}

impl<T> FixedVec<T> {
#[inline]
pub unsafe fn new(cap: usize) -> Self {
Self {
ptr: unsafe { std::alloc::alloc(std::alloc::Layout::array::<T>(cap).unwrap()).cast() },
Expand All @@ -358,7 +336,6 @@ impl<T> FixedVec<T> {
}
}

#[inline]
unsafe fn extend_unchecked(&mut self, slice: &[T]) {
unsafe {
std::ptr::copy_nonoverlapping(slice.as_ptr(), self.ptr.add(self.len), slice.len());
Expand All @@ -369,14 +346,12 @@ impl<T> FixedVec<T> {
}

impl FixedVec<u8> {
#[inline]
unsafe fn extend_char_unchecked(&mut self, chr: char) {
self.extend_unchecked(chr.encode_utf8(&mut [0; 4]).as_bytes())
}
}

impl<T> Drop for FixedVec<T> {
#[inline]
fn drop(&mut self) {
unsafe {
std::alloc::dealloc(
Expand All @@ -388,7 +363,6 @@ impl<T> Drop for FixedVec<T> {
}

impl From<FixedVec<u8>> for String {
#[inline]
fn from(val: FixedVec<u8>) -> Self {
let val = ManuallyDrop::new(val);
unsafe { String::from_raw_parts(val.ptr, val.len, val.cap) }
Expand Down
2 changes: 0 additions & 2 deletions src/unicode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ mod icu {
}

#[cfg(feature = "normalization")]
#[inline]
pub fn normalize<S>(value: S) -> String
where
S: AsRef<str>,
Expand All @@ -23,7 +22,6 @@ where
}

#[cfg(not(feature = "normalization"))]
#[inline]
pub fn normalize<S>(value: S) -> String
where
S: Into<String>,
Expand Down

0 comments on commit c31d31f

Please sign in to comment.