Skip to content

port PyObject::downcast to Bound API#3856

Merged
davidhewitt merged 4 commits intoPyO3:mainfrom
Icxolu:pyobject-downcast
Feb 18, 2024
Merged

port PyObject::downcast to Bound API#3856
davidhewitt merged 4 commits intoPyO3:mainfrom
Icxolu:pyobject-downcast

Conversation

@Icxolu
Copy link
Member

@Icxolu Icxolu commented Feb 17, 2024

Part of #3684

This ports the PyObject::downcast(_unchecked) methods to the Bound API.

Edit:
I also added included Python::(checked_)cast_as here. We decided to deprecate these in favor of the PyObject methods.

@codspeed-hq
Copy link

codspeed-hq bot commented Feb 17, 2024

CodSpeed Performance Report

Merging #3856 will degrade performances by 15.63%

Comparing Icxolu:pyobject-downcast (e507287) with main (5f42c02)

Summary

⚡ 1 improvements
❌ 3 regressions
✅ 75 untouched benchmarks

⚠️ Please fix the performance issues or acknowledge them on CodSpeed.

Benchmarks breakdown

Benchmark main Icxolu:pyobject-downcast Change
mapping_from_dict 300 ns 355.6 ns -15.63%
f64_from_pyobject 516.7 ns 461.1 ns +12.05%
sequence_from_list 300 ns 355.6 ns -15.63%
sequence_from_tuple 232.8 ns 267.2 ns -12.89%

@davidhewitt davidhewitt added the CI-skip-changelog Skip checking changelog entry label Feb 17, 2024
Copy link
Member

@davidhewitt davidhewitt left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for picking this one up! Just one piece which I think we can tweak further.

Comment on lines +65 to +72
/// Compatibility API to convert the Bound variant `DowncastError` into the
/// gil-ref variant
pub(crate) fn from_downcast_err(DowncastError { from, to }: DowncastError<'a, 'a>) -> Self {
Self {
from: from.as_gil_ref(),
to,
}
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good idea, I also had some similar pieces in #3706 and maybe I can build on top of this API 👍

Copy link
Member

@davidhewitt davidhewitt left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

downcast APIs, look good! I wonder about a different strategy for the cast_as APIs? (See comment.)

src/marker.rs Outdated
not(feature = "gil-refs"),
deprecated(
since = "0.21.0",
note = "`Python::checked_cast_as` will be replaced by `PyObject::downcast_bound` in a future PyO3 version"
Copy link
Member

@davidhewitt davidhewitt Feb 18, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Given the semantics of the "replacement" is slightly different, maybe something like the following wording is more helpful to users:

Suggested change
note = "`Python::checked_cast_as` will be replaced by `PyObject::downcast_bound` in a future PyO3 version"
note = "part of the deprecated GIL Ref API, to migrate use `obj.downcast_bound::<T>(py)` instead of `py.cast_as::<T>(obj)`"

What do you think of this (and similar below)?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sounds good

Copy link
Member

@davidhewitt davidhewitt left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks great! Thanks 🚀

@davidhewitt davidhewitt added this pull request to the merge queue Feb 18, 2024
Merged via the queue into PyO3:main with commit 1d295a1 Feb 18, 2024
@Icxolu Icxolu deleted the pyobject-downcast branch February 18, 2024 09:55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CI-skip-changelog Skip checking changelog entry

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants