Mark error construction as cold code

This eliminates 12% of the Serde-related code in the Xi release binary
as measured by:

nm -S target/release/xi-core \
    | awk '/serde/{sum += strtonum("0x"$2)} END{print sum}'
This commit is contained in:
David Tolnay 2018-04-21 14:02:04 -07:00
parent b07a208716
commit c455720f81
No known key found for this signature in database
GPG Key ID: F9BA143B95FF6D82
3 changed files with 11 additions and 0 deletions

View File

@ -196,6 +196,7 @@ macro_rules! declare_error_trait {
/// For example if we try to deserialize a String out of a JSON file /// For example if we try to deserialize a String out of a JSON file
/// containing an integer, the unexpected type is the integer and the /// containing an integer, the unexpected type is the integer and the
/// expected type is the string. /// expected type is the string.
#[cold]
fn invalid_type(unexp: Unexpected, exp: &Expected) -> Self { fn invalid_type(unexp: Unexpected, exp: &Expected) -> Self {
Error::custom(format_args!("invalid type: {}, expected {}", unexp, exp)) Error::custom(format_args!("invalid type: {}, expected {}", unexp, exp))
} }
@ -213,6 +214,7 @@ macro_rules! declare_error_trait {
/// For example if we try to deserialize a String out of some binary data /// For example if we try to deserialize a String out of some binary data
/// that is not valid UTF-8, the unexpected value is the bytes and the /// that is not valid UTF-8, the unexpected value is the bytes and the
/// expected value is a string. /// expected value is a string.
#[cold]
fn invalid_value(unexp: Unexpected, exp: &Expected) -> Self { fn invalid_value(unexp: Unexpected, exp: &Expected) -> Self {
Error::custom(format_args!("invalid value: {}, expected {}", unexp, exp)) Error::custom(format_args!("invalid value: {}, expected {}", unexp, exp))
} }
@ -226,12 +228,14 @@ macro_rules! declare_error_trait {
/// The `exp` argument provides information about what data was being /// The `exp` argument provides information about what data was being
/// expected. For example `exp` might say that a tuple of size 6 was /// expected. For example `exp` might say that a tuple of size 6 was
/// expected. /// expected.
#[cold]
fn invalid_length(len: usize, exp: &Expected) -> Self { fn invalid_length(len: usize, exp: &Expected) -> Self {
Error::custom(format_args!("invalid length {}, expected {}", len, exp)) Error::custom(format_args!("invalid length {}, expected {}", len, exp))
} }
/// Raised when a `Deserialize` enum type received a variant with an /// Raised when a `Deserialize` enum type received a variant with an
/// unrecognized name. /// unrecognized name.
#[cold]
fn unknown_variant(variant: &str, expected: &'static [&'static str]) -> Self { fn unknown_variant(variant: &str, expected: &'static [&'static str]) -> Self {
if expected.is_empty() { if expected.is_empty() {
Error::custom(format_args!("unknown variant `{}`, there are no variants", Error::custom(format_args!("unknown variant `{}`, there are no variants",
@ -245,6 +249,7 @@ macro_rules! declare_error_trait {
/// Raised when a `Deserialize` struct type received a field with an /// Raised when a `Deserialize` struct type received a field with an
/// unrecognized name. /// unrecognized name.
#[cold]
fn unknown_field(field: &str, expected: &'static [&'static str]) -> Self { fn unknown_field(field: &str, expected: &'static [&'static str]) -> Self {
if expected.is_empty() { if expected.is_empty() {
Error::custom(format_args!("unknown field `{}`, there are no fields", Error::custom(format_args!("unknown field `{}`, there are no fields",
@ -259,12 +264,14 @@ macro_rules! declare_error_trait {
/// Raised when a `Deserialize` struct type expected to receive a required /// Raised when a `Deserialize` struct type expected to receive a required
/// field with a particular name but that field was not present in the /// field with a particular name but that field was not present in the
/// input. /// input.
#[cold]
fn missing_field(field: &'static str) -> Self { fn missing_field(field: &'static str) -> Self {
Error::custom(format_args!("missing field `{}`", field)) Error::custom(format_args!("missing field `{}`", field))
} }
/// Raised when a `Deserialize` struct type received more than one of the /// Raised when a `Deserialize` struct type received more than one of the
/// same field. /// same field.
#[cold]
fn duplicate_field(field: &'static str) -> Self { fn duplicate_field(field: &'static str) -> Self {
Error::custom(format_args!("duplicate field `{}`", field)) Error::custom(format_args!("duplicate field `{}`", field))
} }

View File

@ -58,6 +58,7 @@ type ErrorImpl = ();
impl de::Error for Error { impl de::Error for Error {
#[cfg(any(feature = "std", feature = "alloc"))] #[cfg(any(feature = "std", feature = "alloc"))]
#[cold]
fn custom<T>(msg: T) -> Self fn custom<T>(msg: T) -> Self
where where
T: Display, T: Display,
@ -68,6 +69,7 @@ impl de::Error for Error {
} }
#[cfg(not(any(feature = "std", feature = "alloc")))] #[cfg(not(any(feature = "std", feature = "alloc")))]
#[cold]
fn custom<T>(msg: T) -> Self fn custom<T>(msg: T) -> Self
where where
T: Display, T: Display,
@ -78,6 +80,7 @@ impl de::Error for Error {
} }
impl ser::Error for Error { impl ser::Error for Error {
#[cold]
fn custom<T>(msg: T) -> Self fn custom<T>(msg: T) -> Self
where where
T: Display, T: Display,

View File

@ -277,6 +277,7 @@ mod content {
} }
} }
#[cold]
fn unexpected(&self) -> Unexpected { fn unexpected(&self) -> Unexpected {
match *self { match *self {
Content::Bool(b) => Unexpected::Bool(b), Content::Bool(b) => Unexpected::Bool(b),