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

port Python::import to Bound API #3832

Merged
merged 2 commits into from
Feb 14, 2024
Merged

Conversation

Icxolu
Copy link
Contributor

@Icxolu Icxolu commented Feb 13, 2024

Part of #3684

This ports the Python::import over to the Bound API. This does not port PyModule::import, which can be done separately and is currently part of #3775. I left a FIXME there, indicating that it needs to change in the future, but it should also popup once PyModule::import gets deprecated.

Copy link

codspeed-hq bot commented Feb 13, 2024

CodSpeed Performance Report

Merging #3832 will degrade performances by 28.99%

Comparing Icxolu:python-import (dc36403) with main (e308c8d)

Summary

⚡ 3 improvements
❌ 3 regressions
✅ 73 untouched benchmarks

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

Benchmarks breakdown

Benchmark main Icxolu:python-import Change
mapping_from_dict 272.2 ns 355.6 ns -23.44%
list_via_downcast 185 ns 157.2 ns +17.67%
list_via_extract 392.2 ns 336.7 ns +16.5%
not_a_list_via_downcast 244.4 ns 216.7 ns +12.82%
sequence_from_list 272.2 ns 383.3 ns -28.99%
sequence_from_tuple 232.8 ns 260.6 ns -10.66%

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 yet again! Looks great, it's nice to see a whole bunch of .as_borrowed() and .as_gil_ref() calls getting removed.

Only one tiny adjustment which I thought could be worth doing, plus a couple of thoughts which aren't really for this PR and more follow-ups...

@@ -1196,15 +1196,15 @@ impl<T> Py<T> {
/// # Example: `intern!`ing the attribute name
///
/// ```
/// # use pyo3::{intern, pyfunction, types::PyModule, IntoPy, Py, Python, PyObject, PyResult};
/// # use pyo3::{prelude::*, intern};
Copy link
Member

Choose a reason for hiding this comment

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

Nice tidy ups 👍

Comment on lines +127 to +128
.to_cow()?
.into_owned();
Copy link
Member

Choose a reason for hiding this comment

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

👍 nicely done figuring out the to_str / to_cow tweaks on PyString. I guess we should probably add a note about PyString::to_str to the migration guide; I don't think we have one at the moment...

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yeah, thats a good idea. I think I stumbled over this twice already...

@@ -1,6 +1,6 @@
#![cfg(not(Py_LIMITED_API))]

use pyo3::{types::PyDate, Python};
use pyo3::{prelude::PyAnyMethods, types::PyDate, Python};
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
use pyo3::{prelude::PyAnyMethods, types::PyDate, Python};
use pyo3::{prelude::*, types::PyDate};

Slightly related thought: I've been thinking we might want to make these traits public at some path other than the prelude, just for the few occasions when users might want to directly name them (?).

One option would be to make their existing paths public, e.g. pyo3::types::any::PyAnyMethods. We'd just need to be careful we didn't accidentally make other APIs public at the same time.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

We could also reexport them from pyo3::types, like we already do for the types themselfs

Copy link
Member

Choose a reason for hiding this comment

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

pyo3::types I think makes a lot of sense. That module is already public, and we can control easily what gets exposed 👍

@davidhewitt davidhewitt added the CI-skip-changelog Skip checking changelog entry label Feb 13, 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.

I decided to commit the tiny fixup & merge now, and we can continue to discuss the follow-ups while potentially I rebase and get #3775 ready for review.

@davidhewitt davidhewitt added this pull request to the merge queue Feb 14, 2024
Merged via the queue into PyO3:main with commit 0c12d91 Feb 14, 2024
37 of 39 checks passed
@Icxolu Icxolu deleted the python-import branch February 14, 2024 11:08
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