From 282a8b779b9974ff8f449dee8ebb8165d0555dd9 Mon Sep 17 00:00:00 2001 From: Remi Chateauneu Date: Tue, 18 May 2021 12:05:05 +0100 Subject: [PATCH 1/2] Speedup Literal.__hash__ and Literal.__eq__ by accessing directly _datatype and _language. --- rdflib/term.py | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/rdflib/term.py b/rdflib/term.py index 0f627e69f..4e7c3c82f 100644 --- a/rdflib/term.py +++ b/rdflib/term.py @@ -969,10 +969,10 @@ 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) + if self._language: + res ^= hash(self._language.lower()) + if self._datatype: + res ^= hash(self._datatype) return res def __eq__(self, other): @@ -1017,9 +1017,9 @@ def __eq__(self, other): return False 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) ) From 42c90d97f8564e7676023a59b860c05997870eaf Mon Sep 17 00:00:00 2001 From: Remi Chateauneu Date: Thu, 20 May 2021 07:31:00 +0100 Subject: [PATCH 2/2] Added comments. --- rdflib/term.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/rdflib/term.py b/rdflib/term.py index 4e7c3c82f..36f84aaa2 100644 --- a/rdflib/term.py +++ b/rdflib/term.py @@ -969,6 +969,7 @@ def __hash__(self): """ # don't use super()... for efficiency reasons, see Identifier.__hash__ res = str.__hash__(self) + # Directly accessing the member is faster than the property. if self._language: res ^= hash(self._language.lower()) if self._datatype: @@ -1015,6 +1016,7 @@ 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