-
-
Notifications
You must be signed in to change notification settings - Fork 1.9k
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
chore(python): Continue converting to PyO3 0.21 Bound<> APIs #16081
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good to me!
PS: Even though you're only touching Rust code, this should get a 'python' tag since it affects our Python releases, not our Rust ones.
Will use correct tag for future PRs, thanks. Also note that I can't merge this, so someone else will have to :) |
@@ -46,7 +48,7 @@ impl PyExpr { | |||
let name_mapper = Arc::new(move |name: &str| { | |||
Python::with_gil(|py| { | |||
let out = name_mapper.call1(py, (name,)).unwrap(); | |||
let out: SmartString = out.extract::<&str>(py).unwrap().into(); | |||
let out: SmartString = out.extract::<Cow<str>>(py).unwrap().into(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why do we use Cow
in favor of pybackedstr
here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Less typing to convert to SmartString. Could do it the other way too, though.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok, looking at the FromPyObject
implementations in Pyo3
, I now see what the "fastest" way is to get a rust &str
.
And that is indeed going via Cow
. As they write obj.downcast::<PyString>()?.to_cow().map(Cow::into_owned)
for String
, where to_cow
says:
"""
Converts the PyString into a Rust string, avoiding copying when possible.
"""
So I do think this is the fastest way to get a borrowing string out of python. I think we should favor this than over pybytes
in other places.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
- Is that specific to this PR too? If not, can it be merged?
- I am not sure that
Cow<str>
is always the best option. In particular, it has a lifetime that is tied to the original object.PyBackedStr
in contrast is an owned object, so it's a lot easier to work with some contexts. And it has some overhead but I don't think its copying overhead, it's more "having aPy<>
" overhead, so like... a Python incref basically?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, I mean in the borrowing &str
case. Not related to this PR indeed. Thanks!
…#16081) Co-authored-by: Itamar Turner-Trauring <[email protected]>
Fixes #16078