Skip to content

Commit

Permalink
Improve performance after impl-writer and impl-reader (#393)
Browse files Browse the repository at this point in the history
* Add read_bytes inline always

* More inline

* Re-order ReaderRet::Bytes to be first cmp

80% Speedup in some of my benchmarks

* Inline to_bytes and to_bits

* More always inline

* Inline everything

* Change [cold] to [inline(never)]

* One more perf test
  • Loading branch information
wcampbell0x2a authored Apr 30, 2024
1 parent bf83f55 commit 5799ff4
Show file tree
Hide file tree
Showing 8 changed files with 63 additions and 40 deletions.
8 changes: 8 additions & 0 deletions deku-derive/src/macros/deku_read.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +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]
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 @@ -128,6 +129,7 @@ fn emit_struct(input: &DekuData) -> Result<TokenStream, syn::Error> {

tokens.extend(quote! {
impl #imp ::#crate_::DekuReader<#lifetime> for #ident #wher {
#[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 @@ -376,6 +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]
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 @@ -388,6 +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]
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 @@ -407,6 +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]
fn deku_id(&self) -> core::result::Result<(#deku_id_type), DekuError> {
match self {
#(#deku_ids ,)*
Expand Down Expand Up @@ -778,11 +783,13 @@ pub fn emit_container_read(
quote! {
impl #imp ::#crate_::DekuContainerRead<#lifetime> for #ident #wher {
#[allow(non_snake_case)]
#[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]
fn from_bytes(__deku_input: (&#lifetime [u8], usize)) -> core::result::Result<((&#lifetime [u8], usize), Self), ::#crate_::DekuError> {
#from_bytes_body
}
Expand All @@ -802,6 +809,7 @@ pub fn emit_try_from(
impl #imp core::convert::TryFrom<&#lifetime [u8]> for #ident #wher {
type Error = ::#crate_::DekuError;

#[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
10 changes: 10 additions & 0 deletions deku-derive/src/macros/deku_write.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +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]
fn try_from(input: #ident) -> core::result::Result<Self, Self::Error> {
input.to_bits()
}
Expand All @@ -59,6 +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]
fn try_from(input: #ident) -> core::result::Result<Self, Self::Error> {
::#crate_::DekuContainerWrite::to_bytes(&input)
}
Expand Down Expand Up @@ -86,6 +88,7 @@ fn emit_struct(input: &DekuData) -> Result<TokenStream, syn::Error> {

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

impl #imp ::#crate_::DekuWriter<#ctx_types> for #ident #wher {
#[allow(unused_variables)]
#[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 @@ -108,6 +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]
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 @@ -242,6 +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]
fn try_from(input: #ident) -> core::result::Result<Self, Self::Error> {
input.to_bits()
}
Expand All @@ -250,6 +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]
fn try_from(input: #ident) -> core::result::Result<Self, Self::Error> {
::#crate_::DekuContainerWrite::to_bytes(&input)
}
Expand All @@ -276,6 +283,7 @@ fn emit_enum(input: &DekuData) -> Result<TokenStream, syn::Error> {

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

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

impl #imp ::#crate_::DekuWriter<#ctx_types> for #ident #wher {
#[allow(unused_variables)]
#[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 @@ -301,6 +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]
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
Loading

0 comments on commit 5799ff4

Please sign in to comment.