Skip to content

Commit

Permalink
One more perf test
Browse files Browse the repository at this point in the history
  • Loading branch information
wcampbell0x2a committed Apr 29, 2024
1 parent 7f4783a commit 176383a
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 27 deletions.
16 changes: 8 additions & 8 deletions deku-derive/src/macros/deku_read.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ fn emit_struct(input: &DekuData) -> Result<TokenStream, syn::Error> {

tokens.extend(quote! {
impl #imp ::#crate_::DekuReader<#lifetime, #ctx_types> for #ident #wher {
#[inline(always)]
#[inline]
fn from_reader_with_ctx<R: ::#crate_::no_std_io::Read>(__deku_reader: &mut ::#crate_::reader::Reader<R>, #ctx_arg) -> core::result::Result<Self, ::#crate_::DekuError> {
#read_body
}
Expand All @@ -129,7 +129,7 @@ fn emit_struct(input: &DekuData) -> Result<TokenStream, syn::Error> {

tokens.extend(quote! {
impl #imp ::#crate_::DekuReader<#lifetime> for #ident #wher {
#[inline(always)]
#[inline]
fn from_reader_with_ctx<R: ::#crate_::no_std_io::Read>(__deku_reader: &mut ::#crate_::reader::Reader<R>, _: ()) -> core::result::Result<Self, ::#crate_::DekuError> {
#read_body
}
Expand Down Expand Up @@ -378,7 +378,7 @@ fn emit_enum(input: &DekuData) -> Result<TokenStream, syn::Error> {
tokens.extend(quote! {
#[allow(non_snake_case)]
impl #imp ::#crate_::DekuReader<#lifetime, #ctx_types> for #ident #wher {
#[inline(always)]
#[inline]
fn from_reader_with_ctx<R: ::#crate_::no_std_io::Read>(__deku_reader: &mut ::#crate_::reader::Reader<R>, #ctx_arg) -> core::result::Result<Self, ::#crate_::DekuError> {
#read_body
}
Expand All @@ -391,7 +391,7 @@ fn emit_enum(input: &DekuData) -> Result<TokenStream, syn::Error> {
tokens.extend(quote! {
#[allow(non_snake_case)]
impl #imp ::#crate_::DekuReader<#lifetime> for #ident #wher {
#[inline(always)]
#[inline]
fn from_reader_with_ctx<R: ::#crate_::no_std_io::Read>(__deku_reader: &mut ::#crate_::reader::Reader<R>, _: ()) -> core::result::Result<Self, ::#crate_::DekuError> {
#read_body
}
Expand All @@ -411,7 +411,7 @@ fn emit_enum(input: &DekuData) -> Result<TokenStream, syn::Error> {
if let Some(deku_id_type) = deku_id_type {
tokens.extend(quote! {
impl #imp DekuEnumExt<#lifetime, (#deku_id_type)> for #ident #wher {
#[inline(always)]
#[inline]
fn deku_id(&self) -> core::result::Result<(#deku_id_type), DekuError> {
match self {
#(#deku_ids ,)*
Expand Down Expand Up @@ -783,13 +783,13 @@ pub fn emit_container_read(
quote! {
impl #imp ::#crate_::DekuContainerRead<#lifetime> for #ident #wher {
#[allow(non_snake_case)]
#[inline(always)]
#[inline]
fn from_reader<'a, R: ::#crate_::no_std_io::Read>(__deku_input: (&'a mut R, usize)) -> core::result::Result<(usize, Self), ::#crate_::DekuError> {
#from_reader_body
}

#[allow(non_snake_case)]
#[inline(always)]
#[inline]
fn from_bytes(__deku_input: (&#lifetime [u8], usize)) -> core::result::Result<((&#lifetime [u8], usize), Self), ::#crate_::DekuError> {
#from_bytes_body
}
Expand All @@ -809,7 +809,7 @@ pub fn emit_try_from(
impl #imp core::convert::TryFrom<&#lifetime [u8]> for #ident #wher {
type Error = ::#crate_::DekuError;

#[inline(always)]
#[inline]
fn try_from(input: &#lifetime [u8]) -> core::result::Result<Self, Self::Error> {
let total_len = input.len();
let mut cursor = ::#crate_::no_std_io::Cursor::new(input);
Expand Down
20 changes: 10 additions & 10 deletions deku-derive/src/macros/deku_write.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ fn emit_struct(input: &DekuData) -> Result<TokenStream, syn::Error> {
impl #imp core::convert::TryFrom<#ident> for ::#crate_::bitvec::BitVec<u8, ::#crate_::bitvec::Msb0> #wher {
type Error = ::#crate_::DekuError;

#[inline(always)]
#[inline]
fn try_from(input: #ident) -> core::result::Result<Self, Self::Error> {
input.to_bits()
}
Expand All @@ -60,7 +60,7 @@ fn emit_struct(input: &DekuData) -> Result<TokenStream, syn::Error> {
impl #imp core::convert::TryFrom<#ident> for Vec<u8> #wher {
type Error = ::#crate_::DekuError;

#[inline(always)]
#[inline]
fn try_from(input: #ident) -> core::result::Result<Self, Self::Error> {
::#crate_::DekuContainerWrite::to_bytes(&input)
}
Expand Down Expand Up @@ -88,7 +88,7 @@ fn emit_struct(input: &DekuData) -> Result<TokenStream, syn::Error> {

tokens.extend(quote! {
impl #imp DekuUpdate for #ident #wher {
#[inline(always)]
#[inline]
fn update(&mut self) -> core::result::Result<(), ::#crate_::DekuError> {
#update_use
#(#field_updates)*
Expand All @@ -99,7 +99,7 @@ fn emit_struct(input: &DekuData) -> Result<TokenStream, syn::Error> {

impl #imp ::#crate_::DekuWriter<#ctx_types> for #ident #wher {
#[allow(unused_variables)]
#[inline(always)]
#[inline]
fn to_writer<W: ::#crate_::no_std_io::Write>(&self, __deku_writer: &mut ::#crate_::writer::Writer<W>, #ctx_arg) -> core::result::Result<(), ::#crate_::DekuError> {
#write_body
}
Expand All @@ -112,7 +112,7 @@ fn emit_struct(input: &DekuData) -> Result<TokenStream, syn::Error> {
tokens.extend(quote! {
impl #imp ::#crate_::DekuWriter for #ident #wher {
#[allow(unused_variables)]
#[inline(always)]
#[inline]
fn to_writer<W: ::#crate_::no_std_io::Write>(&self, __deku_writer: &mut ::#crate_::writer::Writer<W>, _: ()) -> core::result::Result<(), ::#crate_::DekuError> {
#write_body
}
Expand Down Expand Up @@ -247,7 +247,7 @@ fn emit_enum(input: &DekuData) -> Result<TokenStream, syn::Error> {
impl #imp core::convert::TryFrom<#ident> for ::#crate_::bitvec::BitVec<u8, ::#crate_::bitvec::Msb0> #wher {
type Error = ::#crate_::DekuError;

#[inline(always)]
#[inline]
fn try_from(input: #ident) -> core::result::Result<Self, Self::Error> {
input.to_bits()
}
Expand All @@ -256,7 +256,7 @@ fn emit_enum(input: &DekuData) -> Result<TokenStream, syn::Error> {
impl #imp core::convert::TryFrom<#ident> for Vec<u8> #wher {
type Error = ::#crate_::DekuError;

#[inline(always)]
#[inline]
fn try_from(input: #ident) -> core::result::Result<Self, Self::Error> {
::#crate_::DekuContainerWrite::to_bytes(&input)
}
Expand All @@ -283,7 +283,7 @@ fn emit_enum(input: &DekuData) -> Result<TokenStream, syn::Error> {

tokens.extend(quote! {
impl #imp DekuUpdate for #ident #wher {
#[inline(always)]
#[inline]
fn update(&mut self) -> core::result::Result<(), ::#crate_::DekuError> {
#update_use

Expand All @@ -297,7 +297,7 @@ fn emit_enum(input: &DekuData) -> Result<TokenStream, syn::Error> {

impl #imp ::#crate_::DekuWriter<#ctx_types> for #ident #wher {
#[allow(unused_variables)]
#[inline(always)]
#[inline]
fn to_writer<W: ::#crate_::no_std_io::Write>(&self, __deku_writer: &mut ::#crate_::writer::Writer<W>, #ctx_arg) -> core::result::Result<(), ::#crate_::DekuError> {
#write_body
}
Expand All @@ -310,7 +310,7 @@ fn emit_enum(input: &DekuData) -> Result<TokenStream, syn::Error> {
tokens.extend(quote! {
impl #imp ::#crate_::DekuWriter for #ident #wher {
#[allow(unused_variables)]
#[inline(always)]
#[inline]
fn to_writer<W: ::#crate_::no_std_io::Write>(&self, __deku_writer: &mut ::#crate_::writer::Writer<W>, _: ()) -> core::result::Result<(), ::#crate_::DekuError> {
#write_body
}
Expand Down
1 change: 0 additions & 1 deletion examples/ipv4.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use std::convert::TryInto;
use std::net::Ipv4Addr;

use deku::prelude::*;
Expand Down
3 changes: 3 additions & 0 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,19 @@ pub struct NeedSize {

impl NeedSize {
/// Create new [NeedSize] from bits
#[inline]
pub fn new(bits: usize) -> Self {
Self { bits }
}

/// Number of bits needed
#[inline]
pub fn bit_size(&self) -> usize {
self.bits
}

/// Number of bytes needed
#[inline]
pub fn byte_size(&self) -> usize {
(self.bits + 7) / 8
}
Expand Down
16 changes: 8 additions & 8 deletions src/impls/primitive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ where

// specialize u8 for ByteSize
impl DekuRead<'_, (Endian, ByteSize)> for u8 {
#[inline(always)]
#[inline]
fn read(
input: &BitSlice<u8, Msb0>,
(_, _): (Endian, ByteSize),
Expand Down Expand Up @@ -98,7 +98,7 @@ impl DekuReader<'_, (Endian, ByteSize)> for u8 {
macro_rules! ImplDekuReadBits {
($typ:ty, $inner:ty) => {
impl DekuRead<'_, (Endian, BitSize)> for $typ {
#[inline(always)]
#[inline(never)]
fn read(
input: &BitSlice<u8, Msb0>,
(endian, size): (Endian, BitSize),
Expand Down Expand Up @@ -200,7 +200,7 @@ macro_rules! ImplDekuReadBits {
macro_rules! ImplDekuReadBytes {
($typ:ty, $inner:ty) => {
impl DekuRead<'_, (Endian, ByteSize)> for $typ {
#[inline(always)]
#[inline(never)]
fn read(
input: &BitSlice<u8, Msb0>,
(endian, size): (Endian, ByteSize),
Expand Down Expand Up @@ -235,7 +235,7 @@ macro_rules! ImplDekuReadBytes {
size.0
)));
}
let mut buf = [0; core::mem::size_of::<$typ>()];
let mut buf = [0; MAX_TYPE_BYTES];
let ret = reader.read_bytes(size.0, &mut buf)?;
let a = match ret {
ReaderRet::Bytes => {
Expand Down Expand Up @@ -267,7 +267,7 @@ macro_rules! ImplDekuReadBytes {
macro_rules! ImplDekuReadSignExtend {
($typ:ty, $inner:ty) => {
impl DekuRead<'_, (Endian, ByteSize)> for $typ {
#[inline(always)]
#[inline(never)]
fn read(
input: &BitSlice<u8, Msb0>,
(endian, size): (Endian, ByteSize),
Expand Down Expand Up @@ -317,7 +317,7 @@ macro_rules! ImplDekuReadSignExtend {
}

impl DekuRead<'_, (Endian, BitSize)> for $typ {
#[inline(always)]
#[inline(never)]
fn read(
input: &BitSlice<u8, Msb0>,
(endian, size): (Endian, BitSize),
Expand Down Expand Up @@ -368,9 +368,9 @@ macro_rules! ForwardDekuRead {
reader: &mut Reader<R>,
endian: Endian,
) -> Result<$typ, DekuError> {
let byte_size = core::mem::size_of::<$typ>();
const BYTE_SIZE: usize = core::mem::size_of::<$typ>();

<$typ>::from_reader_with_ctx(reader, (endian, ByteSize(byte_size)))
<$typ>::from_reader_with_ctx(reader, (endian, ByteSize(BYTE_SIZE)))
}
}

Expand Down

0 comments on commit 176383a

Please sign in to comment.