Move Option<Box<_>> into ExtraInstructionAttributes.#13127
Merged
Conversation
Collaborator
|
One or more of the following people are relevant to this code:
|
Pull Request Test Coverage Report for Build 10837025267Details
💛 - Coveralls |
jakelishman
reviewed
Sep 12, 2024
Member
jakelishman
left a comment
There was a problem hiding this comment.
Thanks for doing this, it looks like a very sensible idea.
1 task
Previously, CircuitInstruction and PackedInstruction held the ExtraInstructionAttributes struct within an Option<Box<_>>. By putting the Option<Box<_>> inside ExtraInstructionAttributes, we can use the struct itself to manage its memory and provide access to the attributes behind a nicer interface. The size of ExtraInstructionAttributes should be the same size as the old Option<Box<ExtraInstructionAttributes>>, so this should not have memory implications.
- Use tuple struct. - Use 'Attributes' over 'Attrs'. - Add doc comment for internal 'ExtraAttributes' struct. - Add doc comments for methods. - Add setters for unit and duration.
919a3b9 to
943b142
Compare
jakelishman
approved these changes
Sep 12, 2024
Member
jakelishman
left a comment
There was a problem hiding this comment.
Thanks for this and the changes. I'll leave unmerged because I saw elsewhere that Ray was going to look too.
raynelfss
approved these changes
Sep 12, 2024
Contributor
raynelfss
left a comment
There was a problem hiding this comment.
This is all sensible to me, my only comment was about the usage of a map-like struct instead of a simple tuple struct but it seems it's already been corrected :)
This should be good as is. Thanks for working on it! 🚀
ElePT
pushed a commit
to ElePT/qiskit
that referenced
this pull request
Sep 18, 2024
* Move Option<Box<_>> into ExtraInstructionAttributes. Previously, CircuitInstruction and PackedInstruction held the ExtraInstructionAttributes struct within an Option<Box<_>>. By putting the Option<Box<_>> inside ExtraInstructionAttributes, we can use the struct itself to manage its memory and provide access to the attributes behind a nicer interface. The size of ExtraInstructionAttributes should be the same size as the old Option<Box<ExtraInstructionAttributes>>, so this should not have memory implications. * Address review comments. - Use tuple struct. - Use 'Attributes' over 'Attrs'. - Add doc comment for internal 'ExtraAttributes' struct. - Add doc comments for methods. - Add setters for unit and duration. * Fix performance regression from unnecessary dict creation.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
The primary motivation here is to encapsulate the hairy logic of managing memory for extra attributes. By moving the
Option<Box<_>>into theExtraInstructionAttributesstruct itself, clients are no longer burdened with the responsibility of checking if theOptioncontains a value, unwrapping it etc.; the semantics of that aren't relevant to clients likeDAGCircuitandCircuitData. Instead, theExtraInstructionAttributesacts as a container for optional attributes, and can easily drop theBoxinternally if all attributes are cleared.A give-away that this pattern could be helpful was
ExtraInstructionAttributes::new's return type, which was previouslyOption<Self>.Details and comments
The size of
ExtraInstructionAttributes(now held by value by instructions) should be the same size as the oldOption<Box<ExtraInstructionAttributes>>, so this should not have memory implications regarding the size of instructions.Note that
ExtraAttrsis completely private to thecircuit_instructionmodule, i.e. it's only an implementation detail and in no way part of the interface.