From: Moiz Raja Date: Mon, 11 Aug 2014 22:44:56 +0000 (-0700) Subject: Keep a cache of QName X-Git-Tag: release/helium~227^2 X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?p=controller.git;a=commitdiff_plain;h=84b8ac3b52290fe09bd8fba09f53cb45e1bc06b8 Keep a cache of QName QName creation is expensive so keep of cache of 10,000 QNames around so that we can lookup QNames that were previously created Change-Id: I04e19bbe733a9da46b999e517e2eefdbb88d1259 Signed-off-by: Moiz Raja --- diff --git a/opendaylight/md-sal/sal-clustering-commons/src/main/java/org/opendaylight/controller/cluster/datastore/node/utils/QNameFactory.java b/opendaylight/md-sal/sal-clustering-commons/src/main/java/org/opendaylight/controller/cluster/datastore/node/utils/QNameFactory.java index 002b9ff82e..5a8f522861 100644 --- a/opendaylight/md-sal/sal-clustering-commons/src/main/java/org/opendaylight/controller/cluster/datastore/node/utils/QNameFactory.java +++ b/opendaylight/md-sal/sal-clustering-commons/src/main/java/org/opendaylight/controller/cluster/datastore/node/utils/QNameFactory.java @@ -10,25 +10,28 @@ package org.opendaylight.controller.cluster.datastore.node.utils; +import com.google.common.cache.CacheBuilder; +import com.google.common.cache.CacheLoader; +import com.google.common.cache.LoadingCache; import org.opendaylight.yangtools.yang.common.QName; -import java.util.HashMap; -import java.util.Map; - public class QNameFactory { - private static final Map cache = new HashMap<>(); - public static QName create(String name){ - QName value = cache.get(name); - if(value == null){ - synchronized (cache){ - value = cache.get(name); - if(value == null) { - value = QName.create(name); - cache.put(name, value); + private static final int MAX_QNAME_CACHE_SIZE = 10000; + + private static LoadingCache cache = CacheBuilder.newBuilder() + .maximumSize(MAX_QNAME_CACHE_SIZE) + .softValues() + .build( + new CacheLoader() { + public QName load(String key) { + return QName.create(key); } } - } - return value; + ); + + + public static QName create(String name){ + return cache.getUnchecked(name); } } diff --git a/opendaylight/md-sal/sal-clustering-commons/src/main/java/org/opendaylight/controller/cluster/datastore/util/InstanceIdentifierUtils.java b/opendaylight/md-sal/sal-clustering-commons/src/main/java/org/opendaylight/controller/cluster/datastore/util/InstanceIdentifierUtils.java index 55cb341086..0bb0d4fe87 100644 --- a/opendaylight/md-sal/sal-clustering-commons/src/main/java/org/opendaylight/controller/cluster/datastore/util/InstanceIdentifierUtils.java +++ b/opendaylight/md-sal/sal-clustering-commons/src/main/java/org/opendaylight/controller/cluster/datastore/util/InstanceIdentifierUtils.java @@ -11,6 +11,7 @@ package org.opendaylight.controller.cluster.datastore.util; import org.opendaylight.controller.cluster.datastore.node.utils.NodeIdentifierFactory; +import org.opendaylight.controller.cluster.datastore.node.utils.QNameFactory; import org.opendaylight.controller.protobuff.messages.common.NormalizedNodeMessages; import org.opendaylight.yangtools.yang.common.QName; import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier; @@ -222,7 +223,7 @@ public class InstanceIdentifierUtils { YangInstanceIdentifier.NodeWithValue nodeWithValue = new YangInstanceIdentifier.NodeWithValue( - QName.create(pathArgument.getNodeType().getValue()), + QNameFactory.create(pathArgument.getNodeType().getValue()), parseAttribute(pathArgument.getAttributes(0))); return nodeWithValue; @@ -232,7 +233,7 @@ public class InstanceIdentifierUtils { YangInstanceIdentifier.NodeIdentifierWithPredicates nodeIdentifierWithPredicates = new YangInstanceIdentifier.NodeIdentifierWithPredicates( - QName.create(pathArgument.getNodeType().getValue()), toAttributesMap(pathArgument.getAttributesList())); + QNameFactory.create(pathArgument.getNodeType().getValue()), toAttributesMap(pathArgument.getAttributesList())); return nodeIdentifierWithPredicates; @@ -241,7 +242,7 @@ public class InstanceIdentifierUtils { Set qNameSet = new HashSet<>(); for(NormalizedNodeMessages.Attribute attribute : pathArgument.getAttributesList()){ - qNameSet.add(QName.create(attribute.getValue())); + qNameSet.add(QNameFactory.create(attribute.getValue())); } return new YangInstanceIdentifier.AugmentationIdentifier(qNameSet); @@ -259,7 +260,7 @@ public class InstanceIdentifierUtils { String name = attribute.getName(); Object value = parseAttribute(attribute); - map.put(QName.create(name), value); + map.put(QNameFactory.create(name), value); } return map; diff --git a/opendaylight/md-sal/sal-clustering-commons/src/test/java/org/opendaylight/controller/cluster/datastore/node/utils/QNameFactoryTest.java b/opendaylight/md-sal/sal-clustering-commons/src/test/java/org/opendaylight/controller/cluster/datastore/node/utils/QNameFactoryTest.java new file mode 100644 index 0000000000..76d4ceb2f5 --- /dev/null +++ b/opendaylight/md-sal/sal-clustering-commons/src/test/java/org/opendaylight/controller/cluster/datastore/node/utils/QNameFactoryTest.java @@ -0,0 +1,29 @@ +package org.opendaylight.controller.cluster.datastore.node.utils; + +import org.junit.Test; +import org.opendaylight.controller.cluster.datastore.util.TestModel; +import org.opendaylight.yangtools.yang.common.QName; + +import static junit.framework.Assert.assertTrue; +import static junit.framework.TestCase.assertEquals; +import static org.junit.Assert.assertFalse; + +public class QNameFactoryTest { + + @Test + public void testBasic(){ + QName expected = TestModel.AUG_NAME_QNAME; + QName created = QNameFactory.create(expected.toString()); + + assertFalse( expected == created); + + assertEquals(expected, created); + + QName cached = QNameFactory.create(expected.toString()); + + assertEquals(expected, cached); + + assertTrue( cached == created ); + } + +}