Skip to content

Commit

Permalink
perf: add #[inline] to buffer() and buf()
Browse files Browse the repository at this point in the history
Co-authored-by: Markus Schlegel <[email protected]>
  • Loading branch information
markschl and Markus Schlegel authored Mar 4, 2024
1 parent 73b609d commit d0bcece
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 6 deletions.
22 changes: 16 additions & 6 deletions src/buffer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@ pub enum BufImpl {
}

macro_rules! forward_method {
(pub fn $fnname:ident(&self $($args:tt)*) [$($passargs:tt)*] $(-> $ret:ty)*) => {
($(#[$m:meta])*
pub fn $fnname:ident(&self $($args:tt)*) [$($passargs:tt)*] $(-> $ret:ty)*) => {
$(#[$m])*
pub fn $fnname(&self $($args)*) $(-> $ret)* {
match *self {
BufImpl::Std(ref buf) => buf.$fnname($($passargs)*),
Expand All @@ -27,7 +29,9 @@ macro_rules! forward_method {
}
};

(pub fn $fnname:ident(&mut self $($args:tt)*) [$($passargs:tt)*] $(-> $ret:ty)*) => {
($(#[$m:meta])*
pub fn $fnname:ident(&mut self $($args:tt)*) [$($passargs:tt)*] $(-> $ret:ty)*) => {
$(#[$m])*
pub fn $fnname(&mut self $($args)*) $(-> $ret)* {
match *self {
BufImpl::Std(ref mut buf) => buf.$fnname($($passargs)*),
Expand All @@ -37,7 +41,9 @@ macro_rules! forward_method {
}
};

(pub unsafe fn $fnname:ident(&self $($args:tt)*) [$($passargs:tt)*] $(-> $ret:ty)*) => {
($(#[$m:meta])*
pub unsafe fn $fnname:ident(&self $($args:tt)*) [$($passargs:tt)*] $(-> $ret:ty)*) => {
$(#[$m])*
pub unsafe fn $fnname(&self $($args)*) $(-> $ret)* {
match *self {
BufImpl::Std(ref buf) => buf.$fnname($($passargs)*),
Expand All @@ -47,7 +53,9 @@ macro_rules! forward_method {
}
};

(pub unsafe fn $fnname:ident(&mut self $($args:tt)*) [$($passargs:tt)*] $(-> $ret:ty)*) => {
($(#[$m:meta])*
pub unsafe fn $fnname:ident(&mut self $($args:tt)*) [$($passargs:tt)*] $(-> $ret:ty)*) => {
$(#[$m])*
pub unsafe fn $fnname(&mut self $($args)*) $(-> $ret)* {
match *self {
BufImpl::Std(ref mut buf) => buf.$fnname($($passargs)*),
Expand All @@ -59,9 +67,10 @@ macro_rules! forward_method {
}

macro_rules! forward_methods {
($($($qualifiers:ident)+ ($($args:tt)*) [$($passargs:tt)*] $(-> $ret:ty)*);+;) => (
($($(#[$m:meta])*
$($qualifiers:ident)+ ($($args:tt)*) [$($passargs:tt)*] $(-> $ret:ty)*);+;) => (
$(forward_method! {
$($qualifiers)+ ($($args)*) [$($passargs)*] $(-> $ret)*
$(#[$m])* $($qualifiers)+ ($($args)*) [$($passargs)*] $(-> $ret)*
})*
)
}
Expand Down Expand Up @@ -95,6 +104,7 @@ impl BufImpl {

pub fn make_room(&mut self)[];

#[inline]
pub fn buf(&self)[] -> &[u8];

pub fn buf_mut(&mut self)[] -> &mut [u8];
Expand Down
1 change: 1 addition & 0 deletions src/buffer/slice_deque_buf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ impl SliceDequeBuf {
/// This method is a no-op.
pub fn make_room(&mut self) {}

#[inline]
pub fn buf(&self) -> &[u8] {
&self.deque
}
Expand Down
2 changes: 2 additions & 0 deletions src/buffer/std_buf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ impl StdBuf {
self.end = len;
}

#[inline]
pub fn buf(&self) -> &[u8] {
unsafe { &self.buf.as_slice()[self.pos..self.end] }
}
Expand Down Expand Up @@ -152,6 +153,7 @@ mod impl_ {
false
}

#[inline]
pub unsafe fn as_slice(&self) -> &[u8] {
&self.buf
}
Expand Down
3 changes: 3 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,7 @@ impl<R, P> BufReader<R, P> {
/// Get the section of the buffer containing valid data; may be empty.
///
/// Call `.consume()` to remove bytes from the beginning of this section.
#[inline]
pub fn buffer(&self) -> &[u8] {
self.buf.buf()
}
Expand Down Expand Up @@ -1011,6 +1012,7 @@ impl Buffer {
/// Get an immutable slice of the available bytes in this buffer.
///
/// Call `.consume()` to remove bytes from the beginning of this slice.
#[inline]
pub fn buf(&self) -> &[u8] {
self.buf.buf()
}
Expand Down Expand Up @@ -1215,6 +1217,7 @@ impl<R> Unbuffer<R> {
}

/// Get a slice over the available bytes in the buffer.
#[inline]
pub fn buf(&self) -> &[u8] {
self.buf.as_ref().map_or(&[], Buffer::buf)
}
Expand Down

0 comments on commit d0bcece

Please sign in to comment.