]> gerrit.simantics Code Review - simantics/platform.git/commitdiff
Add ComponentBase.componentId -> ComponentBase cache map in MappingBase
authorTuukka Lehtonen <tuukka.lehtonen@semantum.fi>
Tue, 4 May 2021 07:45:32 +0000 (10:45 +0300)
committerTuukka Lehtonen <tuukka.lehtonen@semantum.fi>
Tue, 4 May 2021 07:45:32 +0000 (10:45 +0300)
This map is built at the same time as the previously existing
`Map<String, T> configurationBySolverName` with the purpose of allowing
implementations to avoid performing solver-side int -> String lookups in
cases where pure model mapping structure lookup zero GC behavior and
speed is essential.

gitlab #713

bundles/org.simantics.structural.synchronization.client/src/org/simantics/structural/synchronization/base/MappingBase.java

index 0714a1b11373fcffa49186690f2c6324fb14cbae..9f557eccf9943a9b8a6aaa02736ef799a04a6918 100644 (file)
@@ -5,10 +5,13 @@ import java.util.Collections;
 import java.util.Map;
 import java.util.function.Consumer;
 
+import org.simantics.scl.runtime.tuple.Tuple2;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
+import gnu.trove.map.TIntObjectMap;
 import gnu.trove.map.hash.THashMap;
+import gnu.trove.map.hash.TIntObjectHashMap;
 import gnu.trove.procedure.TObjectObjectProcedure;
 import gnu.trove.procedure.TObjectProcedure;
 import gnu.trove.set.hash.THashSet;
@@ -36,6 +39,11 @@ abstract public class MappingBase<T extends ComponentBase<T>> {
      */
     transient protected Map<String, T> configurationBySolverName;
     
+    /**
+     * Set of all mapped components indexed by their solver component id.
+     */
+    transient protected TIntObjectMap<T> configurationByComponentId;
+    
     /** 
      * Set of components whose removal is delayed because they might
      * have been moved somewhere else.
@@ -102,35 +110,58 @@ abstract public class MappingBase<T extends ComponentBase<T>> {
         }
     }
 
+    @SuppressWarnings("unchecked")
     public Map<String, T> getConfigurationBySolverName() {
         Map<String, T> result = configurationBySolverName;
         if (result == null) {
             T configuration = getConfiguration();
-            if (configuration != null)
-                result = configurationBySolverName = createConfigurationBySolverName(configuration);
-            else
+            if (configuration != null) {
+                Tuple2 t = createConfigurationCacheMaps(configuration);
+                return (Map<String, T>) t.get(0);
+            } else {
                 result = Collections.emptyMap();
+            }
+        }
+        return result;
+    }
+
+    @SuppressWarnings("unchecked")
+    public TIntObjectMap<T> getConfigurationByComponentId() {
+        TIntObjectMap<T> result = configurationByComponentId;
+        if (result == null) {
+            T configuration = getConfiguration();
+            if (configuration != null) {
+                Tuple2 t = createConfigurationCacheMaps(configuration);
+                return (TIntObjectMap<T>) t.get(1);
+            } else {
+                result = new TIntObjectHashMap<>(1);
+            }
         }
         return result;
     }
 
-    protected Map<String, T> createConfigurationBySolverName(T configuration) {
+    protected Tuple2 createConfigurationCacheMaps(T configuration) {
         THashMap<String, T> configurationBySolverName = new THashMap<>();
-        browseConfigurationBySolverName(configurationBySolverName, configuration);
-        return configurationBySolverName;
+        TIntObjectMap<T> configurationByComponentId = new TIntObjectHashMap<>();
+        browseConfigurationCacheMaps(configurationBySolverName, configurationByComponentId, configuration);
+        this.configurationBySolverName = configurationBySolverName;
+        this.configurationByComponentId = configurationByComponentId;
+        return new Tuple2(configurationBySolverName, configurationByComponentId);
     }
 
-    private void browseConfigurationBySolverName(
+    private void browseConfigurationCacheMaps(
             THashMap<String, T> configurationBySolverName,
-            T configuration) {
+            TIntObjectMap<T> configurationByComponentId,
+            T configuration)
+    {
         if (configuration.solverComponentName != null) {
             configurationBySolverName.put(configuration.solverComponentName, configuration);
+            configurationByComponentId.put(configuration.componentId, configuration);
         } else if (configuration.componentId != 0) {
             LOGGER.warn("configuration.solverComponentName is null! configuration uid is {} and component id {}", configuration.getUid(), configuration.componentId);
         }
         for(T child : configuration.getChildren()) {
-            browseConfigurationBySolverName(configurationBySolverName, child);
-            child.parent = configuration;
+            browseConfigurationCacheMaps(configurationBySolverName, configurationByComponentId, child);
         }
     }