diff --git a/src/err/mod.rs b/src/err/mod.rs index cd2139b607e..53d59929e60 100644 --- a/src/err/mod.rs +++ b/src/err/mod.rs @@ -53,6 +53,7 @@ pub struct PyDowncastError<'a> { impl<'a> PyDowncastError<'a> { /// Create a new `PyDowncastError` representing a failure to convert the object /// `from` into the type named in `to`. + #[cold] pub fn new(from: &'a PyAny, to: impl Into>) -> Self { PyDowncastError { from, diff --git a/src/types/any.rs b/src/types/any.rs index 98f4a8d4df0..dc29b1dea56 100644 --- a/src/types/any.rs +++ b/src/types/any.rs @@ -847,6 +847,7 @@ impl PyAny { /// Extracts some type from the Python object. /// /// This is a wrapper function around [`FromPyObject::extract()`]. + #[inline] pub fn extract<'a, D>(&'a self) -> PyResult where D: FromPyObject<'a>, diff --git a/src/types/mapping.rs b/src/types/mapping.rs index 3b189d33494..e837ef911e9 100644 --- a/src/types/mapping.rs +++ b/src/types/mapping.rs @@ -123,7 +123,10 @@ fn get_mapping_abc(py: Python<'_>) -> PyResult<&PyType> { impl PyTypeCheck for PyMapping { const NAME: &'static str = "Mapping"; + #[inline] fn type_check(object: &PyAny) -> bool { + // Using `is_instance` for `collections.abc.Mapping` is slow, so provide + // optimized case dict as a well-known mapping PyDict::is_type_of(object) || get_mapping_abc(object.py()) .and_then(|abc| object.is_instance(abc)) @@ -139,8 +142,6 @@ impl<'v> crate::PyTryFrom<'v> for PyMapping { fn try_from>(value: V) -> Result<&'v PyMapping, PyDowncastError<'v>> { let value = value.into(); - // Using `is_instance` for `collections.abc.Mapping` is slow, so provide - // optimized case dict as a well-known mapping if PyMapping::type_check(value) { unsafe { return Ok(value.downcast_unchecked()) } } diff --git a/src/types/mod.rs b/src/types/mod.rs index 807653550f0..b20fdf37305 100644 --- a/src/types/mod.rs +++ b/src/types/mod.rs @@ -219,6 +219,7 @@ macro_rules! pyobject_native_type_info( macro_rules! pyobject_native_type_extract { ($name:ty $(;$generics:ident)*) => { impl<'py, $($generics,)*> $crate::FromPyObject<'py> for &'py $name { + #[inline] fn extract(obj: &'py $crate::PyAny) -> $crate::PyResult { obj.downcast().map_err(::std::convert::Into::into) } diff --git a/src/types/sequence.rs b/src/types/sequence.rs index 100a8750639..6515f6b4bf3 100644 --- a/src/types/sequence.rs +++ b/src/types/sequence.rs @@ -313,7 +313,10 @@ fn get_sequence_abc(py: Python<'_>) -> PyResult<&PyType> { impl PyTypeCheck for PySequence { const NAME: &'static str = "Sequence"; + #[inline] fn type_check(object: &PyAny) -> bool { + // Using `is_instance` for `collections.abc.Sequence` is slow, so provide + // optimized cases for list and tuples as common well-known sequences PyList::is_type_of(object) || PyTuple::is_type_of(object) || get_sequence_abc(object.py()) @@ -330,8 +333,6 @@ impl<'v> crate::PyTryFrom<'v> for PySequence { fn try_from>(value: V) -> Result<&'v PySequence, PyDowncastError<'v>> { let value = value.into(); - // Using `is_instance` for `collections.abc.Sequence` is slow, so provide - // optimized cases for list and tuples as common well-known sequences if PySequence::type_check(value) { unsafe { return Ok(value.downcast_unchecked::()) } }