Use ImmutableOffsetMap for NodeWithKey entries 20/26120/1
authorRobert Varga <rovarga@cisco.com>
Thu, 27 Aug 2015 17:51:50 +0000 (19:51 +0200)
committerRobert Varga <rovarga@cisco.com>
Thu, 27 Aug 2015 19:59:47 +0000 (21:59 +0200)
Number of elements allowed in NodeWithKey equals to the number of leaf
nodes specified in the 'key' statement. It is usually a small number of
elements, but these tend to have many instances.

This patch optimizes memory usage with more than one key leaf. Instead
of an ImmutableMap, in which each instance has its own unique key set,
we use an ImmutableOffsetMap, which shares the key set across all
instances. The cost here is a LoadingCache lookup which should prove
neglible.

Change-Id: I68afe667415ba1eaddbb081b419fedcae4e4b8f7
Signed-off-by: Robert Varga <rovarga@cisco.com>
yang/yang-data-api/src/main/java/org/opendaylight/yangtools/yang/data/api/YangInstanceIdentifier.java

index 9c7d5c3569974f0bfd903ad93eb3573351d05557..caee3c30d6450c1b44349c7a1bb99f728c35b31b 100644 (file)
@@ -30,6 +30,7 @@ import org.opendaylight.yangtools.concepts.Builder;
 import org.opendaylight.yangtools.concepts.Immutable;
 import org.opendaylight.yangtools.concepts.Path;
 import org.opendaylight.yangtools.util.HashCodeBuilder;
+import org.opendaylight.yangtools.util.ImmutableOffsetMap;
 import org.opendaylight.yangtools.yang.common.QName;
 import org.opendaylight.yangtools.yang.common.QNameModule;
 import org.opendaylight.yangtools.yang.data.api.schema.LeafSetEntryNode;
@@ -530,7 +531,8 @@ public abstract class YangInstanceIdentifier implements Path<YangInstanceIdentif
 
         public NodeIdentifierWithPredicates(final QName node, final Map<QName, Object> keyValues) {
             super(node);
-            this.keyValues = ImmutableMap.copyOf(keyValues);
+            // Retains ImmutableMap for maps with size() <= 1. For larger sizes uses a shared key set.
+            this.keyValues = ImmutableOffsetMap.copyOf(keyValues);
         }
 
         public NodeIdentifierWithPredicates(final QName node, final QName key, final Object value) {