Improve stored map memory efficiency 62/22262/3
authorRobert Varga <rovarga@cisco.com>
Wed, 10 Jun 2015 09:25:01 +0000 (11:25 +0200)
committerGerrit Code Review <gerrit@opendaylight.org>
Thu, 11 Jun 2015 08:06:57 +0000 (08:06 +0000)
When we deal with small maps, we can improve our memory efficiency by
turning it into an ImmutableMap instead of wrapping it in another
instance.

For 1M fake BGP routes, this lowers the memory footprint by ~300MB,
which is about 10%.

Change-Id: I7fbac729f635ea089ac144e8a264df884d94c512
Signed-off-by: Robert Varga <rovarga@cisco.com>
yang/yang-data-impl/src/main/java/org/opendaylight/yangtools/yang/data/impl/schema/nodes/UnmodifiableChildrenMap.java

index 98598aa31e8d852b1dc284bd889dcc73375aaac6..5037dcbfe8df53d60bcc6cc97da6faf6ff322eb8 100644 (file)
@@ -23,6 +23,11 @@ import org.opendaylight.yangtools.yang.data.api.schema.DataContainerChild;
  */
 final class UnmodifiableChildrenMap implements Map<PathArgument, DataContainerChild<? extends PathArgument, ?>>, Serializable {
     private static final long serialVersionUID = 1L;
+    /*
+     * Do not wrap maps which are smaller than this and instead copy them into
+     * an ImmutableMap.
+     */
+    private static final int WRAP_THRESHOLD = 9;
     private final Map<PathArgument, DataContainerChild<? extends PathArgument, ?>> delegate;
     private transient Collection<DataContainerChild<? extends PathArgument, ?>> values;
 
@@ -47,6 +52,9 @@ final class UnmodifiableChildrenMap implements Map<PathArgument, DataContainerCh
         if (map.isEmpty()) {
             return Collections.emptyMap();
         }
+        if (map.size() < WRAP_THRESHOLD) {
+            return ImmutableMap.copyOf(map);
+        }
 
         return new UnmodifiableChildrenMap(map);
     }