From d9507911f7f3667909df5ed2969ff3b1625c736b Mon Sep 17 00:00:00 2001 From: Ivan Smirnov Date: Wed, 1 Dec 2021 16:57:20 +0000 Subject: [PATCH] Encoding: mark unlikely and inline --- src/encode.rs | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/src/encode.rs b/src/encode.rs index c49e5df..c02f62e 100644 --- a/src/encode.rs +++ b/src/encode.rs @@ -8,6 +8,7 @@ use crate::consts::{ use crate::error::{Error, Result}; use crate::header::Header; use crate::pixel::{Pixel, SupportedChannels}; +use crate::utils::unlikely; struct WriteBuf { start: *const u8, @@ -129,14 +130,14 @@ where Pixel: SupportedChannels, { let max_len = encode_size_required(width, height, CHANNELS as u8); - if out.len() < max_len { + if unlikely(out.len() < max_len) { return Err(Error::OutputBufferTooSmall { size: out.len(), required: max_len }); } let n_pixels = (width as usize) * (height as usize); - if data.is_empty() { + if unlikely(data.is_empty()) { return Err(Error::EmptyImage { width, height }); - } else if n_pixels * CHANNELS != data.len() { + } else if unlikely(n_pixels * CHANNELS != data.len()) { return Err(Error::BadEncodingDataSize { size: data.len(), expected: n_pixels * CHANNELS }); } @@ -224,6 +225,7 @@ where Ok(buf.len()) } +#[inline] pub(crate) fn encode_to_buf_impl( out: &mut [u8], data: &[u8], width: u32, height: u32, channels: u8, colorspace: ColorSpace, ) -> Result { @@ -234,6 +236,7 @@ pub(crate) fn encode_to_buf_impl( } } +#[inline] pub(crate) fn encode_to_vec_impl( data: &[u8], width: u32, height: u32, channels: u8, colorspace: ColorSpace, ) -> Result> { @@ -247,6 +250,7 @@ pub(crate) fn encode_to_vec_impl( Ok(out) } +#[inline] pub fn encode_size_required(width: u32, height: u32, channels: u8) -> usize { let (width, height) = (width as usize, height as usize); let n_pixels = width.saturating_mul(height); @@ -256,6 +260,7 @@ pub fn encode_size_required(width: u32, height: u32, channels: u8) -> usize { + QOI_PADDING; } +#[inline] pub fn qoi_encode_to_vec( data: impl AsRef<[u8]>, width: u32, height: u32, channels: u8, colorspace: impl Into, @@ -263,6 +268,7 @@ pub fn qoi_encode_to_vec( encode_to_vec_impl::(data.as_ref(), width, height, channels, colorspace.into()) } +#[inline] pub fn qoi_encode_to_buf( mut out: impl AsMut<[u8]>, data: impl AsRef<[u8]>, width: u32, height: u32, channels: u8, colorspace: impl Into,