summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNatanael Arndt <arndtn@gmail.com>2021-03-09 19:34:48 +0100
committerGitHub <noreply@github.com>2021-03-09 19:34:48 +0100
commit83a6fda9e207dc849ed42fecfcf28cbaaa0c2f2c (patch)
tree44483cfe1ff00abe74e333aeb1692f74c5d2843f
parent16b218ba20fca55ebd2d1e1ba923a0c3f50c6c36 (diff)
parent53164b2168e950364ee704c1ace9bb6cfd7817f5 (diff)
downloadrdflib-83a6fda9e207dc849ed42fecfcf28cbaaa0c2f2c.tar.gz
Merge pull request #1271 from rchateauneu/memory_only
Speedup __add_triple_context.
-rw-r--r--rdflib/plugins/stores/memory.py21
1 files changed, 12 insertions, 9 deletions
diff --git a/rdflib/plugins/stores/memory.py b/rdflib/plugins/stores/memory.py
index 8f783fc8..8bf40a21 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 KeyError:
# 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,10 +474,9 @@ 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:
+ if triple_context == self.__defaultContexts:
del self.__tripleContexts[triple]
def __get_context_for_triple(self, triple, skipQuoted=False):