Skip to content

Commit

Permalink
Cold/inline
Browse files Browse the repository at this point in the history
  • Loading branch information
kornelski committed Feb 2, 2021
1 parent 3a0bdab commit d62fc87
Show file tree
Hide file tree
Showing 12 changed files with 220 additions and 41 deletions.
10 changes: 10 additions & 0 deletions rmp/src/decode/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -204,51 +204,61 @@ impl From<ValueReadError> for NumValueReadError {
// Helper functions to map I/O error into the `InvalidDataRead` error.

#[doc(hidden)]
#[inline]
pub fn read_data_u8<R: Read>(rd: &mut R) -> Result<u8, ValueReadError> {
rd.read_u8().map_err(ValueReadError::InvalidDataRead)
}

#[doc(hidden)]
#[inline]
pub fn read_data_u16<R: Read>(rd: &mut R) -> Result<u16, ValueReadError> {
rd.read_u16::<byteorder::BigEndian>().map_err(ValueReadError::InvalidDataRead)
}

#[doc(hidden)]
#[inline]
pub fn read_data_u32<R: Read>(rd: &mut R) -> Result<u32, ValueReadError> {
rd.read_u32::<byteorder::BigEndian>().map_err(ValueReadError::InvalidDataRead)
}

#[doc(hidden)]
#[inline]
pub fn read_data_u64<R: Read>(rd: &mut R) -> Result<u64, ValueReadError> {
rd.read_u64::<byteorder::BigEndian>().map_err(ValueReadError::InvalidDataRead)
}

#[doc(hidden)]
#[inline]
pub fn read_data_i8<R: Read>(rd: &mut R) -> Result<i8, ValueReadError> {
rd.read_i8().map_err(ValueReadError::InvalidDataRead)
}

#[doc(hidden)]
#[inline]
pub fn read_data_i16<R: Read>(rd: &mut R) -> Result<i16, ValueReadError> {
rd.read_i16::<byteorder::BigEndian>().map_err(ValueReadError::InvalidDataRead)
}

#[doc(hidden)]
#[inline]
pub fn read_data_i32<R: Read>(rd: &mut R) -> Result<i32, ValueReadError> {
rd.read_i32::<byteorder::BigEndian>().map_err(ValueReadError::InvalidDataRead)
}

#[doc(hidden)]
#[inline]
pub fn read_data_i64<R: Read>(rd: &mut R) -> Result<i64, ValueReadError> {
rd.read_i64::<byteorder::BigEndian>().map_err(ValueReadError::InvalidDataRead)
}

#[doc(hidden)]
#[inline]
pub fn read_data_f32<R: Read>(rd: &mut R) -> Result<f32, ValueReadError> {
rd.read_f32::<byteorder::BigEndian>().map_err(ValueReadError::InvalidDataRead)
}

#[doc(hidden)]
#[inline]
pub fn read_data_f64<R: Read>(rd: &mut R) -> Result<f64, ValueReadError> {
rd.read_f64::<byteorder::BigEndian>().map_err(ValueReadError::InvalidDataRead)
}
Expand Down
4 changes: 4 additions & 0 deletions rmp/src/decode/str.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ pub enum DecodeStringError<'a> {
}

impl<'a> error::Error for DecodeStringError<'a> {
#[cold]
fn source(&self) -> Option<&(dyn error::Error + 'static)> {
match *self {
DecodeStringError::InvalidMarkerRead(ref err) |
Expand All @@ -29,12 +30,14 @@ impl<'a> error::Error for DecodeStringError<'a> {
}

impl<'a> Display for DecodeStringError<'a> {
#[cold]
fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), fmt::Error> {
f.write_str("error while decoding string")
}
}

impl<'a> From<ValueReadError> for DecodeStringError<'a> {
#[cold]
fn from(err: ValueReadError) -> DecodeStringError<'a> {
match err {
ValueReadError::InvalidMarkerRead(err) => DecodeStringError::InvalidMarkerRead(err),
Expand All @@ -57,6 +60,7 @@ impl<'a> From<ValueReadError> for DecodeStringError<'a> {
///
/// It also returns `ValueReadError::TypeMismatch` if the actual type is not equal with the
/// expected one, indicating you with the actual type.
#[inline]
pub fn read_str_len<R: Read>(rd: &mut R) -> Result<u32, ValueReadError> {
Ok(read_str_len_with_nread(rd)?.0)
}
Expand Down
23 changes: 23 additions & 0 deletions rmp/src/encode/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,14 @@ pub type Error = ::std::io::Error;
struct MarkerWriteError(Error);

impl From<Error> for MarkerWriteError {
#[cold]
fn from(err: Error) -> MarkerWriteError {
MarkerWriteError(err)
}
}

impl From<MarkerWriteError> for Error {
#[cold]
fn from(err: MarkerWriteError) -> Error {
match err {
MarkerWriteError(err) => err
Expand All @@ -52,12 +54,16 @@ fn write_marker<W: Write>(wr: &mut W, marker: Marker) -> Result<(), MarkerWriteE
struct DataWriteError(Error);

impl From<Error> for DataWriteError {
#[cold]
#[inline]
fn from(err: Error) -> DataWriteError {
DataWriteError(err)
}
}

impl From<DataWriteError> for Error {
#[cold]
#[inline]
fn from(err: DataWriteError) -> Error {
err.0
}
Expand All @@ -80,6 +86,7 @@ impl From<DataWriteError> for Error {
///
/// assert_eq!(vec![0xc0], buf);
/// ```
#[inline]
pub fn write_nil<W: Write>(wr: &mut W) -> Result<(), Error> {
write_marker(wr, Marker::Null).map_err(From::from)
}
Expand All @@ -93,6 +100,7 @@ pub fn write_nil<W: Write>(wr: &mut W) -> Result<(), Error> {
///
/// Each call to this function may generate an I/O error indicating that the operation could not be
/// completed.
#[inline]
pub fn write_bool<W: Write>(wr: &mut W, val: bool) -> Result<(), Error> {
let marker = if val {
Marker::True
Expand All @@ -103,42 +111,52 @@ pub fn write_bool<W: Write>(wr: &mut W, val: bool) -> Result<(), Error> {
write_marker(wr, marker).map_err(From::from)
}

#[inline]
fn write_data_u8<W: Write>(wr: &mut W, val: u8) -> Result<(), DataWriteError> {
wr.write_u8(val).map_err(DataWriteError)
}

#[inline]
fn write_data_u16<W: Write>(wr: &mut W, val: u16) -> Result<(), DataWriteError> {
wr.write_u16::<byteorder::BigEndian>(val).map_err(DataWriteError)
}

#[inline]
fn write_data_u32<W: Write>(wr: &mut W, val: u32) -> Result<(), DataWriteError> {
wr.write_u32::<byteorder::BigEndian>(val).map_err(DataWriteError)
}

#[inline]
fn write_data_u64<W: Write>(wr: &mut W, val: u64) -> Result<(), DataWriteError> {
wr.write_u64::<byteorder::BigEndian>(val).map_err(DataWriteError)
}

#[inline]
fn write_data_i8<W: Write>(wr: &mut W, val: i8) -> Result<(), DataWriteError> {
wr.write_i8(val).map_err(DataWriteError)
}

#[inline]
fn write_data_i16<W: Write>(wr: &mut W, val: i16) -> Result<(), DataWriteError> {
wr.write_i16::<byteorder::BigEndian>(val).map_err(DataWriteError)
}

#[inline]
fn write_data_i32<W: Write>(wr: &mut W, val: i32) -> Result<(), DataWriteError> {
wr.write_i32::<byteorder::BigEndian>(val).map_err(DataWriteError)
}

#[inline]
fn write_data_i64<W: Write>(wr: &mut W, val: i64) -> Result<(), DataWriteError> {
wr.write_i64::<byteorder::BigEndian>(val).map_err(DataWriteError)
}

#[inline]
fn write_data_f32<W: Write>(wr: &mut W, val: f32) -> Result<(), DataWriteError> {
wr.write_f32::<byteorder::BigEndian>(val).map_err(DataWriteError)
}

#[inline]
fn write_data_f64<W: Write>(wr: &mut W, val: f64) -> Result<(), DataWriteError> {
wr.write_f64::<byteorder::BigEndian>(val).map_err(DataWriteError)
}
Expand All @@ -153,6 +171,7 @@ pub enum ValueWriteError {
}

impl From<MarkerWriteError> for ValueWriteError {
#[cold]
fn from(err: MarkerWriteError) -> ValueWriteError {
match err {
MarkerWriteError(err) => ValueWriteError::InvalidMarkerWrite(err),
Expand All @@ -161,6 +180,7 @@ impl From<MarkerWriteError> for ValueWriteError {
}

impl From<DataWriteError> for ValueWriteError {
#[cold]
fn from(err: DataWriteError) -> ValueWriteError {
match err {
DataWriteError(err) => ValueWriteError::InvalidDataWrite(err),
Expand All @@ -169,6 +189,7 @@ impl From<DataWriteError> for ValueWriteError {
}

impl From<ValueWriteError> for Error {
#[cold]
fn from(err: ValueWriteError) -> Error {
match err {
ValueWriteError::InvalidMarkerWrite(err) |
Expand All @@ -178,6 +199,7 @@ impl From<ValueWriteError> for Error {
}

impl error::Error for ValueWriteError {
#[cold]
fn source(&self) -> Option<&(dyn error::Error + 'static)> {
match *self {
ValueWriteError::InvalidMarkerWrite(ref err) |
Expand All @@ -187,6 +209,7 @@ impl error::Error for ValueWriteError {
}

impl Display for ValueWriteError {
#[cold]
fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), fmt::Error> {
f.write_str("error while writing multi-byte MessagePack value")
}
Expand Down
1 change: 1 addition & 0 deletions rmp/src/encode/uint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ use super::{write_data_u8, write_data_u16, write_data_u32, write_data_u64, write
/// # Panics
///
/// Panics if `val` is greater than 127.
#[inline]
pub fn write_pfix<W: Write>(wr: &mut W, val: u8) -> Result<(), Error> {
assert!(val < 128);
write_marker(wr, Marker::FixPos(val))?;
Expand Down
2 changes: 2 additions & 0 deletions rmp/src/marker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -145,12 +145,14 @@ impl Marker {
}

impl From<u8> for Marker {
#[inline]
fn from(val: u8) -> Marker {
Marker::from_u8(val)
}
}

impl Into<u8> for Marker {
#[inline]
fn into(self) -> u8 {
self.to_u8()
}
Expand Down
6 changes: 6 additions & 0 deletions rmpv/src/decode/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ pub enum Error {
}

impl Error {
#[cold]
pub fn kind(&self) -> ErrorKind {
match *self {
Error::InvalidMarkerRead(ref err) => err.kind(),
Expand All @@ -29,6 +30,7 @@ impl Error {
}

impl error::Error for Error {
#[cold]
fn source(&self) -> Option<&(dyn error::Error + 'static)> {
match *self {
Error::InvalidMarkerRead(ref err) => Some(err),
Expand All @@ -38,6 +40,7 @@ impl error::Error for Error {
}

impl Display for Error {
#[cold]
fn fmt(&self, fmt: &mut Formatter<'_>) -> Result<(), fmt::Error> {
match *self {
Error::InvalidMarkerRead(ref err) => {
Expand All @@ -51,12 +54,14 @@ impl Display for Error {
}

impl From<MarkerReadError> for Error {
#[cold]
fn from(err: MarkerReadError) -> Error {
Error::InvalidMarkerRead(err.0)
}
}

impl From<ValueReadError> for Error {
#[cold]
fn from(err: ValueReadError) -> Error {
match err {
ValueReadError::InvalidMarkerRead(err) => Error::InvalidMarkerRead(err),
Expand All @@ -69,6 +74,7 @@ impl From<ValueReadError> for Error {
}

impl Into<io::Error> for Error {
#[cold]
fn into(self) -> io::Error {
match self {
Error::InvalidMarkerRead(err) |
Expand Down
1 change: 1 addition & 0 deletions rmpv/src/decode/value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ fn read_ext_body<R: Read>(rd: &mut R, len: usize) -> Result<(i8, Vec<u8>), Error
/// This function will return `Error` on any I/O error while either reading or decoding a `Value`.
/// All instances of `ErrorKind::Interrupted` are handled by this function and the underlying
/// operation is retried.
#[inline(never)]
pub fn read_value<R>(rd: &mut R) -> Result<Value, Error>
where R: Read
{
Expand Down
1 change: 1 addition & 0 deletions rmpv/src/decode/value_ref.rs
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,7 @@ impl<'a> BorrowRead<'a> for Cursor<&'a [u8]> {
///
/// assert_eq!(ValueRef::from("le message"), read_value_ref(&mut rd).unwrap());
/// ```
#[inline(never)]
pub fn read_value_ref<'a, R>(rd: &mut R) -> Result<ValueRef<'a>, Error>
where R: BorrowRead<'a>
{
Expand Down
9 changes: 6 additions & 3 deletions rmpv/src/ext/de.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,14 @@ use crate::{Integer, IntPriv, Utf8String, Utf8StringRef, Value, ValueRef};
use super::{Error, ValueExt};
use crate::MSGPACK_EXT_STRUCT_NAME;

#[inline]
pub fn from_value<T>(val: Value) -> Result<T, Error>
where T: for<'de> Deserialize<'de>
{
deserialize_from(val)
}

#[inline]
pub fn deserialize_from<'de, T, D>(val: D) -> Result<T, Error>
where T: Deserialize<'de>,
D: Deserializer<'de, Error = Error>
Expand All @@ -26,6 +28,7 @@ pub fn deserialize_from<'de, T, D>(val: D) -> Result<T, Error>
}

impl de::Error for Error {
#[cold]
fn custom<T: Display>(msg: T) -> Self {
Error::Syntax(format!("{}", msg))
}
Expand All @@ -41,6 +44,7 @@ impl<'de> Deserialize<'de> for Value {
impl<'de> serde::de::Visitor<'de> for ValueVisitor {
type Value = Value;

#[cold]
fn expecting(&self, fmt: &mut Formatter<'_>) -> Result<(), fmt::Error> {
"any valid MessagePack value".fmt(fmt)
}
Expand Down Expand Up @@ -138,14 +142,14 @@ impl<'de> Deserialize<'de> for Value {
Ok(Value::Map(pairs))
}

#[inline]
fn visit_newtype_struct<D>(self, deserializer: D) -> Result<Self::Value, D::Error>
where D: Deserializer<'de>,
{
struct ExtValueVisitor;
impl<'de> serde::de::Visitor<'de> for ExtValueVisitor {
type Value = Value;

#[cold]
fn expecting(&self, fmt: &mut Formatter<'_>) -> Result<(), fmt::Error> {
"a valid MessagePack Ext".fmt(fmt)
}
Expand Down Expand Up @@ -181,6 +185,7 @@ impl<'de> Deserialize<'de> for ValueRef<'de> {
impl<'de> de::Visitor<'de> for ValueVisitor {
type Value = ValueRef<'de>;

#[cold]
fn expecting(&self, fmt: &mut Formatter<'_>) -> Result<(), fmt::Error> {
"any valid MessagePack value".fmt(fmt)
}
Expand Down Expand Up @@ -268,7 +273,6 @@ impl<'de> Deserialize<'de> for ValueRef<'de> {
Ok(ValueRef::Map(vec))
}

#[inline]
fn visit_newtype_struct<D>(self, deserializer: D) -> Result<Self::Value, D::Error>
where D: Deserializer<'de>,
{
Expand Down Expand Up @@ -304,7 +308,6 @@ impl<'de> Deserialize<'de> for ValueRef<'de> {
impl<'de> Deserializer<'de> for Value {
type Error = Error;

#[inline]
fn deserialize_any<V>(self, visitor: V) -> Result<V::Value, Self::Error>
where V: Visitor<'de>
{
Expand Down
Loading

0 comments on commit d62fc87

Please sign in to comment.