From 87b96c23de4428eea5c327b18b589bd6a0e62e9d Mon Sep 17 00:00:00 2001 From: Barry Nouwt Date: Fri, 7 Feb 2025 16:53:39 +0100 Subject: [PATCH 1/2] Small change that might improve performance alot. --- .../main/java/eu/knowledge/engine/reasoner/Match.java | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/reasoner/src/main/java/eu/knowledge/engine/reasoner/Match.java b/reasoner/src/main/java/eu/knowledge/engine/reasoner/Match.java index 68a0a3fb7..e4fd415f4 100644 --- a/reasoner/src/main/java/eu/knowledge/engine/reasoner/Match.java +++ b/reasoner/src/main/java/eu/knowledge/engine/reasoner/Match.java @@ -88,9 +88,7 @@ public Match merge(Match otherMatch) { Map newMatchingPatterns = new HashMap<>(this.getMatchingPatterns()); newMatchingPatterns.putAll(otherMatch.getMatchingPatterns()); - Map newMapping = new HashMap<>(this.getMappings()); - newMapping.putAll(otherMatch.getMappings()); - m = new Match(newMatchingPatterns, newMapping); + m = new Match(newMatchingPatterns, mergedMapping); } } return m; @@ -135,7 +133,10 @@ private Map mergeContexts(Map ex Map newContext) { Collection existingContextValues = existingContext.values(); - Map mergedContext = new HashMap(existingContext); + Map mergedContext = new HashMap( + existingContext.size() + newContext.size()); + mergedContext.putAll(existingContext); + for (Map.Entry newEntry : newContext.entrySet()) { Node node; if ((node = getOtherNode(existingContext, newEntry.getKey().node)) != null) { From 000cb8ae96836f1cf1800199428e5515f2a892dc Mon Sep 17 00:00:00 2001 From: Barry Nouwt Date: Fri, 7 Feb 2025 17:37:43 +0100 Subject: [PATCH 2/2] Another less important performance update. --- .../eu/knowledge/engine/reasoner/Match.java | 31 ++++++++++++------- 1 file changed, 19 insertions(+), 12 deletions(-) diff --git a/reasoner/src/main/java/eu/knowledge/engine/reasoner/Match.java b/reasoner/src/main/java/eu/knowledge/engine/reasoner/Match.java index e4fd415f4..d7f083b47 100644 --- a/reasoner/src/main/java/eu/knowledge/engine/reasoner/Match.java +++ b/reasoner/src/main/java/eu/knowledge/engine/reasoner/Match.java @@ -76,19 +76,16 @@ public Match merge(Match otherMatch) { Match m = null; - // if both sides of the matching patterns do not overlap - boolean doMapsIntersect = doIntersect(this.getMatchingPatterns(), otherMatch.getMatchingPatterns()); - - if (!doMapsIntersect) { + var mergedMatchingPatterns = mergeMatchingPatterns(this.getMatchingPatterns(), + otherMatch.getMatchingPatterns()); + // if both sides of the matching patterns do not overlap + if (mergedMatchingPatterns != null) { // and if the mappings do not conflict Map mergedMapping = mergeContexts(this.getMappings(), otherMatch.getMappings()); if (mergedMapping != null) { // if both patterns and mappings do not conflict. - Map newMatchingPatterns = new HashMap<>(this.getMatchingPatterns()); - newMatchingPatterns.putAll(otherMatch.getMatchingPatterns()); - - m = new Match(newMatchingPatterns, mergedMapping); + m = new Match(mergedMatchingPatterns, mergedMapping); } } return m; @@ -99,19 +96,29 @@ public Match merge(Match otherMatch) { * * @param aFirstMap * @param aSecondMap - * @return + * @return {@code null} if the matching patterns overlap on either keys or + * values, otherwise the merged mapping. */ - private boolean doIntersect(Map aFirstMap, + private HashMap mergeMatchingPatterns(Map aFirstMap, Map aSecondMap) { + var mergedMatchingPatterns = new HashMap(aFirstMap.size() + aSecondMap.size()); + + boolean firstTime = true; + for (Entry entry1 : aFirstMap.entrySet()) { for (Entry entry2 : aSecondMap.entrySet()) { if (entry1.getKey().equals(entry2.getKey()) || entry1.getValue().equals(entry2.getValue())) - return true; + return null; + + if (firstTime) + mergedMatchingPatterns.put(entry2.getKey(), entry2.getValue()); } + mergedMatchingPatterns.put(entry1.getKey(), entry1.getValue()); + firstTime = false; } - return false; + return mergedMatchingPatterns; } public Map getMappings() {