Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

deprecate PyCell::new in favor of Py::new or Bound::new #3872

Merged
merged 2 commits into from
Feb 20, 2024

Conversation

Icxolu
Copy link
Contributor

@Icxolu Icxolu commented Feb 19, 2024

Part of #3684

This deprecates PyCell::new in favor of Py::new or Bound::new, depending on whether the GIL attachment is required.

For the tests I used Py if that was enough, and Bound otherwise. In the docs I updated the test-only code and examples unrelated to PyCell. The PyCell specific sections need separate updating, so I #[allow(deprecated)] usage there, same for PyCell specific tests.

Copy link

codspeed-hq bot commented Feb 19, 2024

CodSpeed Performance Report

Merging #3872 will degrade performances by 15.02%

Comparing Icxolu:pycell-new (5207092) with main (0bb9cab)

Summary

⚡ 3 improvements
❌ 2 regressions
✅ 74 untouched benchmarks

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

Benchmarks breakdown

Benchmark main Icxolu:pycell-new Change
mapping_from_dict 327.8 ns 272.2 ns +20.41%
f64_from_pyobject 405.6 ns 461.1 ns -12.05%
list_via_downcast 157.2 ns 185 ns -15.02%
not_a_list_via_downcast 244.4 ns 216.7 ns +12.82%
sequence_from_list 355.6 ns 272.2 ns +30.61%

@davidhewitt davidhewitt added the CI-skip-changelog Skip checking changelog entry label Feb 19, 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! What did you think of moving from PyCell to Py / Bound.

I'm a little torn. I like the fact that it simplifies PyO3's API and makes Py / Bound the only way to interact with all Python objects, regardless of whether they are native or #[pyclass] types.

On the other hand, the "PyCell" concept is easy for users to see the link to RefCell, and I think we will lose this a bit by removing this type. To be honest, that might not matter so much if we also go further and make #[pyclass(frozen)] the default, because then users will see this concept less anyway. Then we can have a section of the guide dedicated to #[pyclass(frozen = false)] to explain it in detail, even if it's hidden away a bit.

I guess even further down the road, with nogil I half expect that PyCell borrow tracking doesn't work so well because it'll be much easier to hit conflicts with concurrency.

... so maybe in the end I convinced myself it's not such a bad thing if we start slowly killing off the "PyCell" concept now 😂


Other than that, just two small comments.

@@ -217,8 +217,8 @@ impl Number {

# fn main() -> PyResult<()> {
# Python::with_gil(|py| {
# let x = PyCell::new(py, Number(4))?;
# let y = PyCell::new(py, Number(4))?;
# let x = &Bound::new(py, Number(4))?.into_any();
Copy link
Member

Choose a reason for hiding this comment

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

These .as_any() and .into_any() calls are probably a consequence of the same thing as #3867 (comment)

I suggest we merge here anyway and then can tidy up a bunch of .as_any() and .into_any() calls after figuring out a solution.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, I've just added some thoughts to that thread.

src/pycell.rs Outdated Show resolved Hide resolved
Co-authored-by: David Hewitt <mail@davidhewitt.dev>
@Icxolu
Copy link
Contributor Author

Icxolu commented Feb 19, 2024

I think it makes sense to try to push to make Py and Bound to be the only way of interacting with Python object. I believe this simplifies the mental model, because the only decision to make is whether the object needs to be attached to the GIL or not.

The functionality will still be there with .borrow() and .borrow_mut(), from the users perspective it will just be combined into Bound. So I guess the concept and mental model of RefCell can still be applied, it might just be not obvious from the name itself.

@davidhewitt davidhewitt added this pull request to the merge queue Feb 19, 2024
@github-merge-queue github-merge-queue bot removed this pull request from the merge queue due to no response for status checks Feb 20, 2024
@davidhewitt davidhewitt added this pull request to the merge queue Feb 20, 2024
Merged via the queue into PyO3:main with commit 61bc02d Feb 20, 2024
37 of 39 checks passed
@Icxolu Icxolu deleted the pycell-new branch February 20, 2024 17:22
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.

None yet

2 participants