Skip to content

Commit be7b059

Browse files
authored
Short-circuit value equality with an identity check (#6372)
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
1 parent 7578110 commit be7b059

File tree

1 file changed

+4
-0
lines changed

1 file changed

+4
-0
lines changed

cirq-core/cirq/value/value_equality_attr.py

+4
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,8 @@ def _value_equality_values_cls_(self) -> Any:
7070

7171

7272
def _value_equality_eq(self: _SupportsValueEquality, other: _SupportsValueEquality) -> bool:
73+
if other is self:
74+
return True
7375
cls_self = self._value_equality_values_cls_()
7476
get_cls_other = getattr(other, '_value_equality_values_cls_', None)
7577
if get_cls_other is None:
@@ -91,6 +93,8 @@ def _value_equality_hash(self: _SupportsValueEquality) -> int:
9193
def _value_equality_approx_eq(
9294
self: _SupportsValueEquality, other: _SupportsValueEquality, atol: float
9395
) -> bool:
96+
if other is self:
97+
return True
9498
cls_self = self._value_equality_values_cls_()
9599
get_cls_other = getattr(other, '_value_equality_values_cls_', None)
96100
if get_cls_other is None:

0 commit comments

Comments
 (0)