From 7654cd8a5a5676eccb5cc9e0e3daadd5beea8537 Mon Sep 17 00:00:00 2001 From: Tuukka Lehtonen Date: Tue, 4 May 2021 10:45:32 +0300 Subject: [PATCH] Add ComponentBase.componentId -> ComponentBase cache map in MappingBase This map is built at the same time as the previously existing `Map 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 --- .../synchronization/base/MappingBase.java | 51 +++++++++++++++---- 1 file changed, 41 insertions(+), 10 deletions(-) diff --git a/bundles/org.simantics.structural.synchronization.client/src/org/simantics/structural/synchronization/base/MappingBase.java b/bundles/org.simantics.structural.synchronization.client/src/org/simantics/structural/synchronization/base/MappingBase.java index 0714a1b11..9f557eccf 100644 --- a/bundles/org.simantics.structural.synchronization.client/src/org/simantics/structural/synchronization/base/MappingBase.java +++ b/bundles/org.simantics.structural.synchronization.client/src/org/simantics/structural/synchronization/base/MappingBase.java @@ -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> { */ transient protected Map configurationBySolverName; + /** + * Set of all mapped components indexed by their solver component id. + */ + transient protected TIntObjectMap configurationByComponentId; + /** * Set of components whose removal is delayed because they might * have been moved somewhere else. @@ -102,35 +110,58 @@ abstract public class MappingBase> { } } + @SuppressWarnings("unchecked") public Map getConfigurationBySolverName() { Map 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) t.get(0); + } else { result = Collections.emptyMap(); + } + } + return result; + } + + @SuppressWarnings("unchecked") + public TIntObjectMap getConfigurationByComponentId() { + TIntObjectMap result = configurationByComponentId; + if (result == null) { + T configuration = getConfiguration(); + if (configuration != null) { + Tuple2 t = createConfigurationCacheMaps(configuration); + return (TIntObjectMap) t.get(1); + } else { + result = new TIntObjectHashMap<>(1); + } } return result; } - protected Map createConfigurationBySolverName(T configuration) { + protected Tuple2 createConfigurationCacheMaps(T configuration) { THashMap configurationBySolverName = new THashMap<>(); - browseConfigurationBySolverName(configurationBySolverName, configuration); - return configurationBySolverName; + TIntObjectMap 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 configurationBySolverName, - T configuration) { + TIntObjectMap 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); } } -- 2.47.1