From be95b7812372a2e0798b59e6c0b0aed479e383f4 Mon Sep 17 00:00:00 2001 From: Alissa Rao Date: Tue, 31 Aug 2021 03:47:41 -0700 Subject: [PATCH] Tag very simple functions with `#[inline(always)]` to improve speed on non-LTO builds. --- enumset/src/lib.rs | 38 ++++++++++++++++++++++++++++++++++++++ enumset/src/repr.rs | 34 ++++++++++++++++++++++++++++++++++ 2 files changed, 72 insertions(+) diff --git a/enumset/src/lib.rs b/enumset/src/lib.rs index 0de89ac..ab3b76c 100644 --- a/enumset/src/lib.rs +++ b/enumset/src/lib.rs @@ -222,16 +222,19 @@ pub struct EnumSet { } impl EnumSet { // Returns all bits valid for the enum + #[inline(always)] fn all_bits() -> T::Repr { T::ALL_BITS } /// Creates an empty `EnumSet`. + #[inline(always)] pub fn new() -> Self { EnumSet { __priv_repr: T::Repr::empty() } } /// Returns an `EnumSet` containing a single element. + #[inline(always)] pub fn only(t: T) -> Self { let mut set = Self::new(); set.insert(t); @@ -241,11 +244,13 @@ impl EnumSet { /// Creates an empty `EnumSet`. /// /// This is an alias for [`EnumSet::new`]. + #[inline(always)] pub fn empty() -> Self { Self::new() } /// Returns an `EnumSet` containing all valid variants of the enum. + #[inline(always)] pub fn all() -> Self { EnumSet { __priv_repr: Self::all_bits() } } @@ -255,6 +260,7 @@ impl EnumSet { /// /// This is the same as [`EnumSet::variant_count`] except in enums with "sparse" variants. /// (e.g. `enum Foo { A = 10, B = 20 }`) + #[inline(always)] pub fn bit_width() -> u32 { T::Repr::WIDTH - T::ALL_BITS.leading_zeros() } @@ -263,62 +269,75 @@ impl EnumSet { /// /// This is the same as [`EnumSet::bit_width`] except in enums with "sparse" variants. /// (e.g. `enum Foo { A = 10, B = 20 }`) + #[inline(always)] pub fn variant_count() -> u32 { T::ALL_BITS.count_ones() } /// Returns the number of elements in this set. + #[inline(always)] pub fn len(&self) -> usize { self.__priv_repr.count_ones() as usize } /// Returns `true` if the set contains no elements. + #[inline(always)] pub fn is_empty(&self) -> bool { self.__priv_repr.is_empty() } /// Removes all elements from the set. + #[inline(always)] pub fn clear(&mut self) { self.__priv_repr = T::Repr::empty() } /// Returns `true` if `self` has no elements in common with `other`. This is equivalent to /// checking for an empty intersection. + #[inline(always)] pub fn is_disjoint(&self, other: Self) -> bool { (*self & other).is_empty() } /// Returns `true` if the set is a superset of another, i.e., `self` contains at least all the /// values in `other`. + #[inline(always)] pub fn is_superset(&self, other: Self) -> bool { (*self & other).__priv_repr == other.__priv_repr } /// Returns `true` if the set is a subset of another, i.e., `other` contains at least all /// the values in `self`. + #[inline(always)] pub fn is_subset(&self, other: Self) -> bool { other.is_superset(*self) } /// Returns a set containing any elements present in either set. + #[inline(always)] pub fn union(&self, other: Self) -> Self { EnumSet { __priv_repr: self.__priv_repr | other.__priv_repr } } /// Returns a set containing every element present in both sets. + #[inline(always)] pub fn intersection(&self, other: Self) -> Self { EnumSet { __priv_repr: self.__priv_repr & other.__priv_repr } } /// Returns a set containing element present in `self` but not in `other`. + #[inline(always)] pub fn difference(&self, other: Self) -> Self { EnumSet { __priv_repr: self.__priv_repr.and_not(other.__priv_repr) } } /// Returns a set containing every element present in either `self` or `other`, but is not /// present in both. + #[inline(always)] pub fn symmetrical_difference(&self, other: Self) -> Self { EnumSet { __priv_repr: self.__priv_repr ^ other.__priv_repr } } /// Returns a set containing all enum variants not in this set. + #[inline(always)] pub fn complement(&self) -> Self { EnumSet { __priv_repr: !self.__priv_repr & Self::all_bits() } } /// Checks whether this set contains a value. + #[inline(always)] pub fn contains(&self, value: T) -> bool { self.__priv_repr.has_bit(value.enum_into_u32()) } @@ -328,12 +347,14 @@ impl EnumSet { /// If the set did not have this value present, `true` is returned. /// /// If the set did have this value present, `false` is returned. + #[inline(always)] pub fn insert(&mut self, value: T) -> bool { let contains = !self.contains(value); self.__priv_repr.add_bit(value.enum_into_u32()); contains } /// Removes a value from this set. Returns whether the value was present in the set. + #[inline(always)] pub fn remove(&mut self, value: T) -> bool { let contains = self.contains(value); self.__priv_repr.remove_bit(value.enum_into_u32()); @@ -341,10 +362,12 @@ impl EnumSet { } /// Adds all elements in another set to this one. + #[inline(always)] pub fn insert_all(&mut self, other: Self) { self.__priv_repr = self.__priv_repr | other.__priv_repr } /// Removes all values in another set from this one. + #[inline(always)] pub fn remove_all(&mut self, other: Self) { self.__priv_repr = self.__priv_repr.and_not(other.__priv_repr); } @@ -375,6 +398,7 @@ macro_rules! conversion_impls { not fit in a `"] #[doc = $underlying_str] #[doc = "`, this method will panic."] + #[inline(always)] pub fn $to(&self) -> $underlying { self.$try_to().expect("Bitset will not fit into this type.") } @@ -385,6 +409,7 @@ macro_rules! conversion_impls { not fit in a `"] #[doc = $underlying_str] #[doc = "`, this method will instead return `None`."] + #[inline(always)] pub fn $try_to(&self) -> Option<$underlying> { EnumSetTypeRepr::$to_fn_opt(&self.__priv_repr) } @@ -395,6 +420,7 @@ macro_rules! conversion_impls { not fit in a `"] #[doc = $underlying_str] #[doc = "`, this method will truncate any bits that don't fit."] + #[inline(always)] pub fn $to_truncated(&self) -> $underlying { EnumSetTypeRepr::$to_fn(&self.__priv_repr) } @@ -403,6 +429,7 @@ macro_rules! conversion_impls { #[doc = $underlying_str] #[doc = "`.\n\nIf a bit that doesn't correspond to an enum variant is set, this \ method will panic."] + #[inline(always)] pub fn $from(bits: $underlying) -> Self { Self::$try_from(bits).expect("Bitset contains invalid variants.") } @@ -411,6 +438,7 @@ macro_rules! conversion_impls { #[doc = $underlying_str] #[doc = "`.\n\nIf a bit that doesn't correspond to an enum variant is set, this \ method will return `None`."] + #[inline(always)] pub fn $try_from(bits: $underlying) -> Option { let bits = T::Repr::$from_fn_opt(bits); let mask = Self::all().__priv_repr; @@ -424,6 +452,7 @@ macro_rules! conversion_impls { #[doc = "Constructs a bitset from a `"] #[doc = $underlying_str] #[doc = "`, ignoring invalid variants."] + #[inline(always)] pub fn $from_truncated(bits: $underlying) -> Self { let mask = Self::all().$to_truncated(); let bits = ::$from_fn(bits & mask); @@ -486,45 +515,53 @@ impl <'a, T: EnumSetType> Sum<&'a T> for EnumSet { impl >> Sub for EnumSet { type Output = Self; + #[inline(always)] fn sub(self, other: O) -> Self::Output { self.difference(other.into()) } } impl >> BitAnd for EnumSet { type Output = Self; + #[inline(always)] fn bitand(self, other: O) -> Self::Output { self.intersection(other.into()) } } impl >> BitOr for EnumSet { type Output = Self; + #[inline(always)] fn bitor(self, other: O) -> Self::Output { self.union(other.into()) } } impl >> BitXor for EnumSet { type Output = Self; + #[inline(always)] fn bitxor(self, other: O) -> Self::Output { self.symmetrical_difference(other.into()) } } impl >> SubAssign for EnumSet { + #[inline(always)] fn sub_assign(&mut self, rhs: O) { *self = *self - rhs; } } impl >> BitAndAssign for EnumSet { + #[inline(always)] fn bitand_assign(&mut self, rhs: O) { *self = *self & rhs; } } impl >> BitOrAssign for EnumSet { + #[inline(always)] fn bitor_assign(&mut self, rhs: O) { *self = *self | rhs; } } impl >> BitXorAssign for EnumSet { + #[inline(always)] fn bitxor_assign(&mut self, rhs: O) { *self = *self ^ rhs; } @@ -532,6 +569,7 @@ impl >> BitXorAssign for EnumSet { impl Not for EnumSet { type Output = Self; + #[inline(always)] fn not(self) -> Self::Output { self.complement() } diff --git a/enumset/src/repr.rs b/enumset/src/repr.rs index 5fc2cd0..7dfa89b 100644 --- a/enumset/src/repr.rs +++ b/enumset/src/repr.rs @@ -67,57 +67,91 @@ macro_rules! prim { impl EnumSetTypeRepr for $name { const WIDTH: u32 = $width; + #[inline(always)] fn is_empty(&self) -> bool { *self == 0 } + #[inline(always)] fn empty() -> Self { 0 } + #[inline(always)] fn add_bit(&mut self, bit: u32) { *self |= 1 << bit as $name; } + #[inline(always)] fn remove_bit(&mut self, bit: u32) { *self &= !(1 << bit as $name); } + #[inline(always)] fn has_bit(&self, bit: u32) -> bool { (self & (1 << bit as $name)) != 0 } + #[inline(always)] fn count_ones(&self) -> u32 { (*self).count_ones() } + #[inline(always)] fn leading_zeros(&self) -> u32 { (*self).leading_zeros() } + #[inline(always)] fn trailing_zeros(&self) -> u32 { (*self).trailing_zeros() } + #[inline(always)] fn and_not(&self, other: Self) -> Self { (*self) & !other } + #[inline(always)] fn count_remaining_ones(&self, cursor: u32) -> usize { let left_mask = !((1 as $name).checked_shl(cursor).unwrap_or(0).wrapping_sub(1)); (*self & left_mask).count_ones() as usize } + #[inline(always)] fn from_u8(v: u8) -> Self { v as $name } + #[inline(always)] fn from_u16(v: u16) -> Self { v as $name } + #[inline(always)] fn from_u32(v: u32) -> Self { v as $name } + #[inline(always)] fn from_u64(v: u64) -> Self { v as $name } + #[inline(always)] fn from_u128(v: u128) -> Self { v as $name } + #[inline(always)] fn from_usize(v: usize) -> Self { v as $name } + #[inline(always)] fn to_u8(&self) -> u8 { (*self) as u8 } + #[inline(always)] fn to_u16(&self) -> u16 { (*self) as u16 } + #[inline(always)] fn to_u32(&self) -> u32 { (*self) as u32 } + #[inline(always)] fn to_u64(&self) -> u64 { (*self) as u64 } + #[inline(always)] fn to_u128(&self) -> u128 { (*self) as u128 } + #[inline(always)] fn to_usize(&self) -> usize { (*self) as usize } + #[inline(always)] fn from_u8_opt(v: u8) -> Option { v.try_into().ok() } + #[inline(always)] fn from_u16_opt(v: u16) -> Option { v.try_into().ok() } + #[inline(always)] fn from_u32_opt(v: u32) -> Option { v.try_into().ok() } + #[inline(always)] fn from_u64_opt(v: u64) -> Option { v.try_into().ok() } + #[inline(always)] fn from_u128_opt(v: u128) -> Option { v.try_into().ok() } + #[inline(always)] fn from_usize_opt(v: usize) -> Option { v.try_into().ok() } + #[inline(always)] fn to_u8_opt(&self) -> Option { (*self).try_into().ok() } + #[inline(always)] fn to_u16_opt(&self) -> Option { (*self).try_into().ok() } + #[inline(always)] fn to_u32_opt(&self) -> Option { (*self).try_into().ok() } + #[inline(always)] fn to_u64_opt(&self) -> Option { (*self).try_into().ok() } + #[inline(always)] fn to_u128_opt(&self) -> Option { (*self).try_into().ok() } + #[inline(always)] fn to_usize_opt(&self) -> Option { (*self).try_into().ok() } } } -- 2.44.0