Skip to content

Commit ebfbbd6

Browse files
committed
Improve object equality check performance
1 parent dfc171e commit ebfbbd6

File tree

4 files changed

+13
-6
lines changed

4 files changed

+13
-6
lines changed

gemd/__version__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
__version__ = "2.1.10"
1+
__version__ = "2.1.11"

gemd/entity/base_entity.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -207,11 +207,13 @@ def _cached_equals(this: "BaseEntity",
207207
# Note that this could violate transitivity -- Link(scope1) == obj == Link(scope2)
208208
def __eq__(self, other):
209209
from gemd.entity.link_by_uid import LinkByUID
210-
if isinstance(other, LinkByUID):
210+
from gemd.util import cached_isinstance
211+
212+
if cached_isinstance(other, LinkByUID):
211213
return self.uids.get(other.scope) == other.id
212-
elif isinstance(other, tuple):
214+
elif cached_isinstance(other, tuple):
213215
return len(other) == 2 and other[0] in self.uids and self.uids[other[0]] == other[1]
214-
elif isinstance(other, BaseEntity):
216+
elif cached_isinstance(other, BaseEntity):
215217
# We have to be a little clever for efficiency and to avoid infinite recursion
216218
return BaseEntity._cached_equals(self, other)
217219
else:

gemd/entity/link_by_uid.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,12 +67,16 @@ def from_entity(cls, entity: BaseEntityType, *, scope=None):
6767
# Note that this could violate transitivity
6868
def __eq__(self, other):
6969
from gemd.entity.base_entity import BaseEntity
70-
if isinstance(other, BaseEntity):
70+
from gemd.util import cached_isinstance
71+
72+
if cached_isinstance(other, LinkByUID):
73+
return self.scope == other.scope and self.id == other.id
74+
elif cached_isinstance(other, BaseEntity):
7175
if self.scope in other.uids:
7276
return other.uids[self.scope] == self.id
7377
else:
7478
return False
75-
elif isinstance(other, tuple): # Make them interchangeable in a dict
79+
elif cached_isinstance(other, tuple): # Make them interchangeable in a dict
7680
return len(other) == 2 and (self.scope, self.id) == other
7781
else:
7882
return super().__eq__(other)

tests/entity/test_link_by_uid.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,3 +47,4 @@ def test_equality():
4747
assert link == ("foo", "bar")
4848
assert link != ("foo", "bar", "baz")
4949
assert link != ("foo", "rab")
50+
assert link != "foo: rab"

0 commit comments

Comments
 (0)