-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
Cache Qid instances for common types #6371
Conversation
5b9b5f8
to
ae62991
Compare
Codecov ReportAll modified and coverable lines are covered by tests ✅
Additional details and impacted files@@ Coverage Diff @@
## main #6371 +/- ##
=======================================
Coverage 97.80% 97.81%
=======================================
Files 1111 1111
Lines 96877 96948 +71
=======================================
+ Hits 94754 94825 +71
Misses 2123 2123 ☔ View full report in Codecov by Sentry. |
|
I don't know of anything to do this automatically. Which is unfortunate because it seems like a rather natural thing to do, and in fact I think we could improve performance by doing this for other heavily-used cirq classes as well.
We could do that, though I suspect it's not common to create so many different qubits that this would matter. |
Changed to use a WeakValueDictionary for caching. |
9e26a02
to
b8a4a62
Compare
b8a4a62
to
74ff0df
Compare
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.
Seems fine to me modulo some extra comments to help future devs try to modify it.
@@ -178,22 +171,33 @@ class GridQid(_BaseGridQid): | |||
cirq.GridQid(5, 4, dimension=2) | |||
""" | |||
|
|||
def __init__(self, row: int, col: int, *, dimension: int) -> None: | |||
"""Initializes a grid qid at the given row, col coordinate | |||
_cache = weakref.WeakValueDictionary[Tuple[int, int, int], 'cirq.GridQid']() |
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.
A line comment explaining this would be appreciated.
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.
Added.
cls._cache[key] = inst | ||
return inst | ||
|
||
def __getnewargs_ex__(self): |
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 seems like a pretty obscure dunder function. Maybe a docstring to explain what this is would be helpful.
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.
Added.
Here we add caches of common qubit types
GridQubit
,LineQubit
, andNamedQubit
(and their associatedQid
types) so that we can reuse instances of these objects. Operations on these objects are extremely common, so reusing instances can have significant performance benefits, in particular because hashes do not need to be recomputed and equality comparisons can be short-circuited with instance equality in common cases.