From 8086068eb55ee5cf0504c7397ccafcc4779f4bc4 Mon Sep 17 00:00:00 2001 From: Remi Chateauneu Date: Mon, 1 Mar 2021 17:50:42 +0000 Subject: [PATCH 1/3] Speedup __add_triple_context. (cherry picked from commit 85e1cfee04e8f9c590171379f57c751e0d5b9828) --- rdflib/plugins/stores/memory.py | 23 +++++++++++++---------- 1 file changed, 13 insertions(+), 10 deletions(-) diff --git a/rdflib/plugins/stores/memory.py b/rdflib/plugins/stores/memory.py index 8f783fc80..df9e64e4e 100644 --- a/rdflib/plugins/stores/memory.py +++ b/rdflib/plugins/stores/memory.py @@ -444,20 +444,24 @@ def __add_triple_context(self, triple, context, quoted): 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 @@ def __add_triple_context(self, triple, context, quoted): # 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 From eb5e025177ad36bd0cd7ac6a794cbc42943826f1 Mon Sep 17 00:00:00 2001 From: Natanael Arndt Date: Fri, 5 Mar 2021 17:22:55 +0100 Subject: [PATCH 2/3] correctly delete default triple context info --- rdflib/plugins/stores/memory.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/rdflib/plugins/stores/memory.py b/rdflib/plugins/stores/memory.py index df9e64e4e..404edfeba 100644 --- a/rdflib/plugins/stores/memory.py +++ b/rdflib/plugins/stores/memory.py @@ -477,7 +477,7 @@ def __add_triple_context(self, triple, context, quoted): self.__defaultContexts = triple_context # if the context info is the same as default, no need to store it if triple_context == self.__defaultContexts: - del triple_context + del self.__tripleContexts[triple] def __get_context_for_triple(self, triple, skipQuoted=False): """return a list of contexts (str) for the triple, skipping From 53164b2168e950364ee704c1ace9bb6cfd7817f5 Mon Sep 17 00:00:00 2001 From: Primhill Computers Date: Sat, 6 Mar 2021 16:49:41 +0000 Subject: [PATCH 3/3] KeyError instead of IndexError. --- rdflib/plugins/stores/memory.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/rdflib/plugins/stores/memory.py b/rdflib/plugins/stores/memory.py index 404edfeba..8bf40a213 100644 --- a/rdflib/plugins/stores/memory.py +++ b/rdflib/plugins/stores/memory.py @@ -446,7 +446,7 @@ def __add_triple_context(self, triple, context, quoted): # we know the triple exists somewhere in the store try: triple_context = self.__tripleContexts[triple] - except IndexError: + except KeyError: # triple exists with default ctx info # start with a copy of the default ctx info triple_context = self.__tripleContexts[triple] = self.__defaultContexts.copy()