Skip to content

Commit

Permalink
Re-order ReaderRet::Bytes to be first cmp
Browse files Browse the repository at this point in the history
80% Speedup in some of my benchmarks
  • Loading branch information
wcampbell0x2a committed Dec 28, 2023
1 parent f9bc99f commit 6610a40
Showing 1 changed file with 15 additions and 15 deletions.
30 changes: 15 additions & 15 deletions src/impls/primitive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,14 +82,14 @@ impl DekuReader<'_, (Endian, ByteSize)> for u8 {
let mut buf = [0; core::mem::size_of::<u8>()];
let ret = reader.read_bytes(size.0, &mut buf)?;
let a = match ret {
ReaderRet::Bytes => <u8>::from_be_bytes(buf),
ReaderRet::Bits(bits) => {
let Some(bits) = bits else {
return Err(DekuError::Parse("no bits read from reader".to_string()));
};
let a = <u8>::read(&bits, (endian, size))?;
a.1
}
ReaderRet::Bytes => <u8>::from_be_bytes(buf),
};
Ok(a)
}
Expand Down Expand Up @@ -238,13 +238,6 @@ macro_rules! ImplDekuReadBytes {
let mut buf = [0; core::mem::size_of::<$typ>()];
let ret = reader.read_bytes(size.0, &mut buf)?;
let a = match ret {
ReaderRet::Bits(Some(bits)) => {
let a = <$typ>::read(&bits, (endian, size))?;
a.1
}
ReaderRet::Bits(None) => {
return Err(DekuError::Parse(format!("no bits read from reader")));
}
ReaderRet::Bytes => {
if endian.is_le() {
<$typ>::from_le_bytes(buf.try_into().unwrap())
Expand All @@ -257,6 +250,13 @@ macro_rules! ImplDekuReadBytes {
<$typ>::from_be_bytes(buf.try_into().unwrap())
}
}
ReaderRet::Bits(Some(bits)) => {
let a = <$typ>::read(&bits, (endian, size))?;
a.1
}
ReaderRet::Bits(None) => {
return Err(DekuError::Parse(format!("no bits read from reader")));
}
};
Ok(a)
}
Expand Down Expand Up @@ -292,20 +292,20 @@ macro_rules! ImplDekuReadSignExtend {
let mut buf = [0; core::mem::size_of::<$typ>()];
let ret = reader.read_bytes(size.0, &mut buf)?;
let a = match ret {
ReaderRet::Bits(bits) => {
let Some(bits) = bits else {
return Err(DekuError::Parse("no bits read from reader".to_string()));
};
let a = <$typ>::read(&bits, (endian, size))?;
a.1
}
ReaderRet::Bytes => {
if endian.is_le() {
<$typ>::from_le_bytes(buf.try_into()?)
} else {
<$typ>::from_be_bytes(buf.try_into()?)
}
}
ReaderRet::Bits(bits) => {
let Some(bits) = bits else {
return Err(DekuError::Parse("no bits read from reader".to_string()));
};
let a = <$typ>::read(&bits, (endian, size))?;
a.1
}
};

const MAX_TYPE_BITS: usize = BitSize::of::<$typ>().0;
Expand Down

0 comments on commit 6610a40

Please sign in to comment.