Skip to content

Commit

Permalink
Merge pull request #1321 from rchateauneu/speedup_term_hash
Browse files Browse the repository at this point in the history
Speedup Literal.__hash__ and Literal.__eq__ by accessing directly _da…
  • Loading branch information
nicholascar authored Jun 29, 2021
2 parents 871f797 + 42c90d9 commit e4d975a
Showing 1 changed file with 9 additions and 7 deletions.
16 changes: 9 additions & 7 deletions rdflib/term.py
Original file line number Diff line number Diff line change
Expand Up @@ -978,10 +978,11 @@ def __hash__(self):
"""
# don't use super()... for efficiency reasons, see Identifier.__hash__
res = str.__hash__(self)
if self.language:
res ^= hash(self.language.lower())
if self.datatype:
res ^= hash(self.datatype)
# Directly accessing the member is faster than the property.
if self._language:
res ^= hash(self._language.lower())
if self._datatype:
res ^= hash(self._datatype)
return res

def __eq__(self, other):
Expand Down Expand Up @@ -1024,11 +1025,12 @@ def __eq__(self, other):
return True
if other is None:
return False
# Directly accessing the member is faster than the property.
if isinstance(other, Literal):
return (
self.datatype == other.datatype
and (self.language.lower() if self.language else None)
== (other.language.lower() if other.language else None)
self._datatype == other._datatype
and (self._language.lower() if self._language else None)
== (other._language.lower() if other._language else None)
and str.__eq__(self, other)
)

Expand Down

0 comments on commit e4d975a

Please sign in to comment.