From 127b0c3e523e4f9bfb1c82bda34ba950f15c0821 Mon Sep 17 00:00:00 2001
From: Simon Barner <barner@fortiss.org>
Date: Wed, 17 Feb 2021 17:44:40 +0100
Subject: [PATCH] Add cache for (expensive) method
 getComposablePrototypes(Class)

Issue-Ref: 4106
Issue-Url: https://git.fortiss.org/af3/af3/issues/4106

Signed-off-by: Simon Barner <barner@fortiss.org>
---
 .../fortiss/tooling/kernel/internal/.ratings  |  2 +-
 .../kernel/internal/PrototypeService.java     | 19 ++++++++++++++++---
 2 files changed, 17 insertions(+), 4 deletions(-)

diff --git a/org.fortiss.tooling.kernel/src/org/fortiss/tooling/kernel/internal/.ratings b/org.fortiss.tooling.kernel/src/org/fortiss/tooling/kernel/internal/.ratings
index 64dad4fdd..942c9a4d7 100644
--- a/org.fortiss.tooling.kernel/src/org/fortiss/tooling/kernel/internal/.ratings
+++ b/org.fortiss.tooling.kernel/src/org/fortiss/tooling/kernel/internal/.ratings
@@ -9,7 +9,7 @@ LibraryService.java a56e71191cd9bab5a6be461000e7e9b605c7b0db GREEN
 LoggingService.java da784259f7b456b54bf75c41ec268f64919ce78d GREEN
 MigrationService.java 017c8438262065f663427a998536035bc7732fe1 GREEN
 PersistencyService.java 621898863371959d822edcde0f83c8a4d2757972 GREEN
-PrototypeService.java 04093ccaea091776e8ed6f8fc723a62e4619ddb6 GREEN
+PrototypeService.java 34fef95b51bb14f6c31144a9a92769141208c23a YELLOW
 ToolingKernelInternal.java f6e7114825748683c7f1d040b41ab854a6c4d79b GREEN
 TransformationService.java 64ee4fb5ccc623f8acfba20866fc2b0944c4adab GREEN
 TutorialService.java 675d3f365ce062869f86baa3779d50687674bda0 GREEN
diff --git a/org.fortiss.tooling.kernel/src/org/fortiss/tooling/kernel/internal/PrototypeService.java b/org.fortiss.tooling.kernel/src/org/fortiss/tooling/kernel/internal/PrototypeService.java
index 04093ccae..34fef95b5 100644
--- a/org.fortiss.tooling.kernel/src/org/fortiss/tooling/kernel/internal/PrototypeService.java
+++ b/org.fortiss.tooling.kernel/src/org/fortiss/tooling/kernel/internal/PrototypeService.java
@@ -16,6 +16,7 @@
 package org.fortiss.tooling.kernel.internal;
 
 import static java.util.Collections.emptyList;
+import static java.util.Collections.unmodifiableList;
 import static org.conqat.lib.commons.collections.CollectionUtils.asUnmodifiable;
 import static org.fortiss.tooling.kernel.utils.ExtensionPointUtils.getBundle;
 import static org.fortiss.tooling.kernel.utils.ExtensionPointUtils.getConfigurationElements;
@@ -23,8 +24,10 @@ import static org.fortiss.tooling.kernel.utils.ExtensionPointUtils.loadClass;
 import static org.fortiss.tooling.kernel.utils.LoggingUtils.error;
 
 import java.util.ArrayList;
+import java.util.HashMap;
 import java.util.LinkedList;
 import java.util.List;
+import java.util.Map;
 import java.util.stream.Collectors;
 
 import org.conqat.lib.commons.collections.UnmodifiableList;
@@ -71,6 +74,10 @@ public class PrototypeService implements IPrototypeService, IIntrospectiveKernel
 	/** The tutorial service instance. */
 	private ITutorialService tutorialService;
 
+	/** Cache for (expensive) {@link #getComposablePrototypes(Class)}. */
+	private Map<Class<? extends EObject>, List<Prototype>> composablePrototypesCache =
+			new HashMap<>();
+
 	/** Initializes the service. */
 	public void initializeService() {
 		setupPrototypes();
@@ -189,14 +196,20 @@ public class PrototypeService implements IPrototypeService, IIntrospectiveKernel
 	/** {@inheritDoc} */
 	@Override
 	public List<Prototype> getComposablePrototypes(Class<? extends EObject> modelElementType) {
+		List<Prototype> composablePrototypes = composablePrototypesCache.get(modelElementType);
+		if(composablePrototypes != null) {
+			return composablePrototypes;
+		}
 		IElementCompositorService ecs = IElementCompositorService.getInstance();
-		List<Prototype> result = new LinkedList<Prototype>();
+		composablePrototypes = new LinkedList<Prototype>();
 		for(Prototype prototype : getAllPrototypes()) {
 			if(ecs.canComposePrototype(modelElementType, prototype)) {
-				result.add(prototype);
+				composablePrototypes.add(prototype);
 			}
 		}
-		return result;
+		composablePrototypes = unmodifiableList(composablePrototypes);
+		composablePrototypesCache.put(modelElementType, composablePrototypes);
+		return composablePrototypes;
 	}
 
 	/** {@inheritDoc} */
-- 
GitLab