Skip to content
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

Short-circuit value equality with an identity check #6372

Merged
merged 3 commits into from
Dec 5, 2023
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Short-circuit value equality with an identity check
We often reuse instances of objects with value semantics, and in such
cases equality checking can be short-circuited by first checking for
object identity before falling back to actually comparing the values of
two objects.
  • Loading branch information
maffoo committed Dec 5, 2023
commit ceb3e48ad9f1c5209ad34ef0153311e06d677153
4 changes: 4 additions & 0 deletions cirq-core/cirq/value/value_equality_attr.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,8 @@ def _value_equality_values_cls_(self) -> Any:


def _value_equality_eq(self: _SupportsValueEquality, other: _SupportsValueEquality) -> bool:
if other is self:
return True
cls_self = self._value_equality_values_cls_()
get_cls_other = getattr(other, '_value_equality_values_cls_', None)
if get_cls_other is None:
Expand All @@ -91,6 +93,8 @@ def _value_equality_hash(self: _SupportsValueEquality) -> int:
def _value_equality_approx_eq(
self: _SupportsValueEquality, other: _SupportsValueEquality, atol: float
) -> bool:
if other is self:
return True
cls_self = self._value_equality_values_cls_()
get_cls_other = getattr(other, '_value_equality_values_cls_', None)
if get_cls_other is None:
Expand Down
Loading