Skip to content

Commit

Permalink
perf: use slice::Iter where possible (#256)
Browse files Browse the repository at this point in the history
  • Loading branch information
DaniPopes authored Aug 30, 2023
1 parent 1fbb091 commit 5f12652
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 8 deletions.
12 changes: 9 additions & 3 deletions crates/primitives/src/bits/fixed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,8 @@ impl<const N: usize> ops::BitAnd for FixedBytes<N> {
impl<const N: usize> ops::BitAndAssign for FixedBytes<N> {
#[inline]
fn bitand_assign(&mut self, rhs: Self) {
iter::zip(self, rhs).for_each(|(a, b)| *a &= b);
// Note: `slice::Iter` has better codegen than `array::IntoIter`
iter::zip(self, &rhs).for_each(|(a, b)| *a &= *b);
}
}

Expand All @@ -263,28 +264,33 @@ impl<const N: usize> ops::BitOr for FixedBytes<N> {
impl<const N: usize> ops::BitOrAssign for FixedBytes<N> {
#[inline]
fn bitor_assign(&mut self, rhs: Self) {
iter::zip(self, rhs).for_each(|(a, b)| *a |= b);
// Note: `slice::Iter` has better codegen than `array::IntoIter`
iter::zip(self, &rhs).for_each(|(a, b)| *a |= *b);
}
}

impl<const N: usize> ops::BitXor for FixedBytes<N> {
type Output = Self;

#[inline]
fn bitxor(mut self, rhs: Self) -> Self::Output {
self ^= rhs;
self
}
}

impl<const N: usize> ops::BitXorAssign for FixedBytes<N> {
#[inline]
fn bitxor_assign(&mut self, rhs: Self) {
iter::zip(self, rhs).for_each(|(a, b)| *a ^= b);
// Note: `slice::Iter` has better codegen than `array::IntoIter`
iter::zip(self, &rhs).for_each(|(a, b)| *a ^= *b);
}
}

impl<const N: usize> core::str::FromStr for FixedBytes<N> {
type Err = hex::FromHexError;

#[inline]
fn from_str(s: &str) -> Result<Self, Self::Err> {
let mut buf = [0u8; N];
hex::decode_to_slice(s, &mut buf)?;
Expand Down
4 changes: 2 additions & 2 deletions crates/sol-macro/src/expand/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -184,8 +184,8 @@ impl ExpCtxt<'_> {

for functions in self.functions.values().filter(|fs| fs.len() >= 2) {
// check for same parameters
for (i, a) in functions.iter().enumerate() {
for b in functions.iter().skip(i + 1) {
for (i, &a) in functions.iter().enumerate() {
for &b in functions.iter().skip(i + 1) {
if a.arguments.types().eq(b.arguments.types()) {
let msg = "function with same name and parameter types defined twice";
let mut err = syn::Error::new(a.span(), msg);
Expand Down
6 changes: 3 additions & 3 deletions crates/sol-types/src/coder/decoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,12 @@ impl fmt::Display for Decoder<'_> {
writeln!(f, "Abi Decode Buffer")?;

for (i, chunk) in self.buf.chunks(32).enumerate() {
let idx = i * 32;
writeln!(
f,
"0x{:04x}: {} {}",
i * 32,
"0x{idx:04x}: {}{}",
hex::encode_prefixed(chunk),
if i * 32 == self.offset {
if idx == self.offset {
" <-- Next Word"
} else {
""
Expand Down

0 comments on commit 5f12652

Please sign in to comment.