Commit f36b7d65 authored by Tuukka Lehtonen's avatar Tuukka Lehtonen
Browse files

Add ComponentBase.componentId -> ComponentBase cache map in MappingBase

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

(cherry picked from commit 7654cd8a)
(cherry picked from commit 6b5ee0d3)
parent be666b60
......@@ -8,7 +8,9 @@ import java.util.function.Consumer;
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;
......@@ -35,7 +37,12 @@ abstract public class MappingBase<T extends ComponentBase<T>> {
* Set of all components indexed by their solver name.
*/
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 +109,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) {
Object[] t = createConfigurationCacheMaps(configuration);
return (Map<String, T>) t[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) {
Object[] t = createConfigurationCacheMaps(configuration);
return (TIntObjectMap<T>) t[1];
} else {
result = new TIntObjectHashMap<>(1);
}
}
return result;
}
protected Map<String, T> createConfigurationBySolverName(T configuration) {
protected Object[] 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 Object[] { 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);
}
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment