-
Notifications
You must be signed in to change notification settings - Fork 2.4k
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
Localise py_op
caching data in RefCell
#12594
Conversation
This localises the caching concerns of the `PackedInstruction::py_op` field into a method `unpack_py_op`, which can now update the cache through an immutable reference (if no other immutable references are taken out). Having the new method to encapsulate the `cache_pyops` feature simplifies the large `#[cfg(feature = "cache_pyop")]` gates.
One or more of the following people are relevant to this code:
|
Pull Request Test Coverage Report for Build 9552158178Details
💛 - Coveralls |
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 LGTM.
I like the idea of using the RefCell
for interior mutability to avoid requiring &mut
refs when we'll be doing away with the cache stuff later on.
I'm not hitting "merge" in case @mtreinish wants to take a look first, since this is an extension of his operation oxidation work.
This looks like it does: 1) refactor a lot of code into I'm curious if it is possible to get the same refactoring benefit without using Since caching the Python op is supposed to be a bridge anyway, and there are not dramatic performance differences, maybe it's not a big deal whether |
John: in theory this can be done without the About the refactoring: reducing the duplicated code in the |
With the same principles of moving fast and undoing if necessary, I'll merge this now to avoid further merge conflicts, since it may well get touched up as part of #12622 anyway. |
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.
* Accept Option<&str> instead of &Option<String>, etc 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 #12594. The original commit has been cherry picked on top of main. * Remove unnecessary as_ref()
This localises the caching concerns of the `PackedInstruction::py_op` field into a method `unpack_py_op`, which can now update the cache through an immutable reference (if no other immutable references are taken out). Having the new method to encapsulate the `cache_pyops` feature simplifies the large `#[cfg(feature = "cache_pyop")]` gates.
* Accept Option<&str> instead of &Option<String>, etc 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. * Remove unnecessary as_ref()
Summary
This localises the caching concerns of the
PackedInstruction::py_op
field into a methodunpack_py_op
, which can now update the cache through an immutable reference (if no other immutable references are taken out). Having the new method to encapsulate thecache_pyops
feature simplifies the large#[cfg(feature = "cache_pyop")]
gates.Details and comments
Locally, this had no impact for me on runtime performance for the benchmarks of #12459 if (on macOS) I set the
MallocMediumZone=0
environment variable, but I did see a reduction in performance without that set. That said, setting it also caused a sizeable improvement in the benchmarks without this PR as well.This will make memory usage a bit worse while the pygate caching is in place (the
RefCell
makes the cost 2 pointers rather than one), but since we're roughly hoping to remove that before 1.2, it's probably worth doing. This commit makes it much harder to forget to set the cache (which a few places in the existing code were doing), and means that the APIs (&self
vs&mut self
) don't need to change within the feature flag, which will make ripping the cache out easier in the future. edit: if necessary, we can trade off the memory usage against a shade more runtime cost by boxing theRefCell
, which should be ok since performance-critical Qiskit code largely shouldn't be using the caching mechanisms by the time of the 1.2 release, but if the caching is still enabled in release builds, then the memory costs would be paid by everyone.Will clash with #12593 (most of its changes are already made locally here too) but I can sort that out easily.