Skip to content

Commit 33a38b1

Browse files
committed
annotations to mitigate performance changes
1 parent 7ecd127 commit 33a38b1

File tree

5 files changed

+9
-4
lines changed

5 files changed

+9
-4
lines changed

src/err/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ pub struct PyDowncastError<'a> {
5353
impl<'a> PyDowncastError<'a> {
5454
/// Create a new `PyDowncastError` representing a failure to convert the object
5555
/// `from` into the type named in `to`.
56+
#[cold]
5657
pub fn new(from: &'a PyAny, to: impl Into<Cow<'static, str>>) -> Self {
5758
PyDowncastError {
5859
from,

src/types/any.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -847,6 +847,7 @@ impl PyAny {
847847
/// Extracts some type from the Python object.
848848
///
849849
/// This is a wrapper function around [`FromPyObject::extract()`].
850+
#[inline]
850851
pub fn extract<'a, D>(&'a self) -> PyResult<D>
851852
where
852853
D: FromPyObject<'a>,

src/types/mapping.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,10 @@ fn get_mapping_abc(py: Python<'_>) -> PyResult<&PyType> {
123123
impl PyTypeCheck for PyMapping {
124124
const NAME: &'static str = "Mapping";
125125

126+
#[inline]
126127
fn type_check(object: &PyAny) -> bool {
128+
// Using `is_instance` for `collections.abc.Mapping` is slow, so provide
129+
// optimized case dict as a well-known mapping
127130
PyDict::is_type_of(object)
128131
|| get_mapping_abc(object.py())
129132
.and_then(|abc| object.is_instance(abc))
@@ -139,8 +142,6 @@ impl<'v> crate::PyTryFrom<'v> for PyMapping {
139142
fn try_from<V: Into<&'v PyAny>>(value: V) -> Result<&'v PyMapping, PyDowncastError<'v>> {
140143
let value = value.into();
141144

142-
// Using `is_instance` for `collections.abc.Mapping` is slow, so provide
143-
// optimized case dict as a well-known mapping
144145
if PyMapping::type_check(value) {
145146
unsafe { return Ok(value.downcast_unchecked()) }
146147
}

src/types/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,7 @@ macro_rules! pyobject_native_type_info(
219219
macro_rules! pyobject_native_type_extract {
220220
($name:ty $(;$generics:ident)*) => {
221221
impl<'py, $($generics,)*> $crate::FromPyObject<'py> for &'py $name {
222+
#[inline]
222223
fn extract(obj: &'py $crate::PyAny) -> $crate::PyResult<Self> {
223224
obj.downcast().map_err(::std::convert::Into::into)
224225
}

src/types/sequence.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -327,7 +327,10 @@ fn get_sequence_abc(py: Python<'_>) -> PyResult<&PyType> {
327327
impl PyTypeCheck for PySequence {
328328
const NAME: &'static str = "Sequence";
329329

330+
#[inline]
330331
fn type_check(object: &PyAny) -> bool {
332+
// Using `is_instance` for `collections.abc.Sequence` is slow, so provide
333+
// optimized cases for list and tuples as common well-known sequences
331334
PyList::is_type_of(object)
332335
|| PyTuple::is_type_of(object)
333336
|| get_sequence_abc(object.py())
@@ -344,8 +347,6 @@ impl<'v> crate::PyTryFrom<'v> for PySequence {
344347
fn try_from<V: Into<&'v PyAny>>(value: V) -> Result<&'v PySequence, PyDowncastError<'v>> {
345348
let value = value.into();
346349

347-
// Using `is_instance` for `collections.abc.Sequence` is slow, so provide
348-
// optimized cases for list and tuples as common well-known sequences
349350
if PySequence::type_check(value) {
350351
unsafe { return Ok(value.downcast_unchecked::<PySequence>()) }
351352
}

0 commit comments

Comments
 (0)