summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRemi Chateauneu <remi.chateauneu@gmail.com>2021-03-01 17:50:42 +0000
committerRemi Chateauneu <remi.chateauneu@gmail.com>2021-03-03 16:28:24 +0000
commit8086068eb55ee5cf0504c7397ccafcc4779f4bc4 (patch)
treeb24d8e81e9007f24d4545743ff7cc1f38880ff1a
parent9f0f296a3b36cbca9771cd0f0c25492a06a33acd (diff)
downloadrdflib-8086068eb55ee5cf0504c7397ccafcc4779f4bc4.tar.gz
Speedup __add_triple_context.
(cherry picked from commit 85e1cfee04e8f9c590171379f57c751e0d5b9828)
-rw-r--r--rdflib/plugins/stores/memory.py23
1 files changed, 13 insertions, 10 deletions
diff --git a/rdflib/plugins/stores/memory.py b/rdflib/plugins/stores/memory.py
index 8f783fc8..df9e64e4 100644
--- a/rdflib/plugins/stores/memory.py
+++ b/rdflib/plugins/stores/memory.py
@@ -444,20 +444,24 @@ class Memory(Store):
subj, pred, obj = triple
_ = self.__spo[subj][pred][obj]
# we know the triple exists somewhere in the store
- if triple not in self.__tripleContexts:
+ try:
+ triple_context = self.__tripleContexts[triple]
+ except IndexError:
# triple exists with default ctx info
# start with a copy of the default ctx info
- self.__tripleContexts[triple] = self.__defaultContexts.copy()
+ triple_context = self.__tripleContexts[triple] = self.__defaultContexts.copy()
+
+ triple_context[ctx] = quoted
- self.__tripleContexts[triple][ctx] = quoted
if not quoted:
- self.__tripleContexts[triple][None] = quoted
+ triple_context[None] = quoted
+
except KeyError:
# the triple didn't exist before in the store
if quoted: # this context only
- self.__tripleContexts[triple] = {ctx: quoted}
+ triple_context = self.__tripleContexts[triple] = {ctx: quoted}
else: # default context as well
- self.__tripleContexts[triple] = {ctx: quoted, None: quoted}
+ triple_context = self.__tripleContexts[triple] = {ctx: quoted, None: quoted}
# if the triple is not quoted add it to the default context
if not quoted:
@@ -470,11 +474,10 @@ class Memory(Store):
# if this is the first ever triple in the store, set default ctx info
if self.__defaultContexts is None:
- self.__defaultContexts = self.__tripleContexts[triple]
-
+ self.__defaultContexts = triple_context
# if the context info is the same as default, no need to store it
- if self.__tripleContexts[triple] == self.__defaultContexts:
- del self.__tripleContexts[triple]
+ if triple_context == self.__defaultContexts:
+ del triple_context
def __get_context_for_triple(self, triple, skipQuoted=False):
"""return a list of contexts (str) for the triple, skipping