From: Robert Varga Date: Tue, 3 Jun 2014 10:50:26 +0000 (+0200) Subject: Optimize conversionMapping table X-Git-Tag: release/helium~164 X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?a=commitdiff_plain;ds=sidebyside;h=e82ecc6bb4235e792b21964019ea07fc62e9ad3c;p=openflowplugin.git Optimize conversionMapping table Once the table has been initialized, we can switch it to immutable, as the keys (Short) provide fast hashCode/equals. Saves memory and increases lookup speed. Change-Id: I4698f44ce5a8e83c7c54e544e5ab72487f22a92e Signed-off-by: Robert Varga --- diff --git a/openflowplugin/src/main/java/org/opendaylight/openflowplugin/openflow/md/core/sal/convertor/common/ConvertReactor.java b/openflowplugin/src/main/java/org/opendaylight/openflowplugin/openflow/md/core/sal/convertor/common/ConvertReactor.java index b0306d1901..df33256e4b 100644 --- a/openflowplugin/src/main/java/org/opendaylight/openflowplugin/openflow/md/core/sal/convertor/common/ConvertReactor.java +++ b/openflowplugin/src/main/java/org/opendaylight/openflowplugin/openflow/md/core/sal/convertor/common/ConvertReactor.java @@ -12,45 +12,49 @@ import java.math.BigInteger; import java.util.HashMap; import java.util.Map; +import com.google.common.collect.ImmutableMap; + /** * @param source type for conversion - * + * */ public abstract class ConvertReactor { - - private Map> conversionMapping; - private Map> injectionMapping; - + private final Map> injectionMapping; + private final Map> conversionMapping; + protected ConvertReactor() { - conversionMapping = new HashMap<>(); + final Map> conversions = new HashMap<>(); injectionMapping = new HashMap<>(); - initMappings(conversionMapping, injectionMapping); + initMappings(conversions, injectionMapping); + + // Create optimized view of conversion mapping + this.conversionMapping = ImmutableMap.copyOf(conversions); } - + /** * fill conversion and injection mappings - * @param conversions - * @param injections + * @param conversions + * @param injections */ - protected abstract void initMappings(Map> conversions, + protected abstract void initMappings(Map> conversions, Map> injections); - + /** * @param source * @param version - * @param target - * @param datapathid + * @param target + * @param datapathid */ @SuppressWarnings("unchecked") - public void convert(FROM source, short version, TARGET target, BigInteger datapathid) { - + public void convert(final FROM source, final short version, final TARGET target, final BigInteger datapathid) { + //lookup converter Convertor convertor = (Convertor) conversionMapping.get(version); if (convertor == null) { throw new IllegalArgumentException("convertor for given version ["+version+"] not found"); } RESULT convertedItem = convertor.convert(source,datapathid); - + //lookup injection InjectionKey key = buildInjectionKey(version, convertedItem, target); ResultInjector injection = (ResultInjector) injectionMapping.get(key); @@ -62,11 +66,11 @@ public abstract class ConvertReactor { /** * @param version - * @param convertedItem to be injected + * @param convertedItem to be injected * @param target object * @return */ - protected InjectionKey buildInjectionKey(short version, Object convertedItem, Object target) { + protected InjectionKey buildInjectionKey(final short version, final Object convertedItem, final Object target) { return new InjectionKey(version, target.getClass().getName()); }