-
Notifications
You must be signed in to change notification settings - Fork 2.3k
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
Accept Option<&str>
instead of &Option<String>
, etc
#12593
base: main
Are you sure you want to change the base?
Conversation
One or more of the following people are relevant to this code:
|
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.
This is a good idea in general, thanks. I have a branch locally that does much of this as part of a different performance change, but I can update that.
label: Option<String>, | ||
label: Option<&str>, |
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.
I think this particular one is mistaken; it's correct that the label
is owned in this constructor.
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.
My intention was to move the creation of the String
into the constructor rather than the caller. Is the problem that this method py_new
is called from Python and has to build a String
rather than &str
? Then a ref is passed and another String
is created with label.map(|x| x.to_string())
which is of course wasteful ?
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.
What I'm thinking of is that if the label
is Some
, then we're always going to need to get an owned object. There's no situation in which we pass Some
to the label
and we won't need to clone it to get an owned copy, so there's nothing gained by allowing it to accept a ref, and there's something potentially lost if the caller had to create the label specifically (say from a format!
) but can't give over the ownership.
I don't know 100% what PyO3 does when it's got an incoming Bound<'py, PyString>
(i.e. if it makes a String
or directly a &str
- I suspect the latter), but accepting an owned String
allows all possible callers to use the most efficient form.
label: Option<String>, | ||
label: Option<&str>, |
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.
This is similarly correct that it's actually able to transfer the ownership.
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.
Hmmm. I guess if we want to transfer ownership, then there is no advantage to building a String
in the constructor. Instead require that the caller constructs a String
. And move it. Otherwise you construct the String
twice, once in the caller and once in the callee.
#12594 is my PR that'll clash, btw - I can update that, though. |
That was faster than I expected. I fixed some clippy complaints and rebased in order to force push because surely no one has looked at this PR yet. I'll push it anyway. I'll compare to your PR; maybe this one is not necessary. |
5463b5a
to
e45f3f5
Compare
Pull Request Test Coverage Report for Build 9551983033Details
💛 - Coveralls |
I'm fine to merge this (or some form of it) before mine - the similar changes in mine are coincidental. |
Ok. Then I should 1) correct the observations you made above and 2) make the same changes where possible in the signatures. |
This comment was marked as off-topic.
This comment was marked as off-topic.
f0386d1
to
3562337
Compare
This comment was marked as off-topic.
This comment was marked as off-topic.
In a few places, this removes unnecessary object copies. Accept a wider range of input types, and more idiomatic input types in a few functions. This affects code added in the gates-in-rust PR. The great majority of the changes here were obsoleted by Qiskit#12594. The original commit has been cherry picked on top of main.
3562337
to
e75c9c3
Compare
Pull Request Test Coverage Report for Build 9670588700Details
💛 - Coveralls |
Accept a wider range of input types, and more idiomatic input types in a few functions. This affects code added in the gates-in-rust PR.
In a few places, this removes unnecessary object copies.
DONE:
The same change could be made in several other places. I can add these in another commit to this PR.