Skip to content

Commit

Permalink
Short-circuit value equality with an identity check (#6372)
Browse files Browse the repository at this point in the history
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.

Review: @95-martin-orion
  • Loading branch information
maffoo authored Dec 5, 2023
1 parent 7578110 commit be7b059
Showing 1 changed file with 4 additions and 0 deletions.
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

0 comments on commit be7b059

Please sign in to comment.