Optimize DataObjectCodecContext memory footprint 73/81873/1
authorRobert Varga <robert.varga@pantheon.tech>
Thu, 2 May 2019 10:27:58 +0000 (12:27 +0200)
committerRobert Varga <robert.varga@pantheon.tech>
Thu, 2 May 2019 11:12:42 +0000 (13:12 +0200)
Examining heap dumps while working on MDSAL-442/MDSAL-443 revealed
a slight inefficiency in DataObjectCodecContext: we are forcing
byStreamClass's entrySet to be instantiated for the purposes of
copying it into byBindingArgClassBuilder.

This patch corrects the mistake by using byStreamClassBuilder
as the source of copied entries -- which holds the same content,
but we will be throwing it away.

Furthermore byStreamClass and byBindingArgClass are only distinct
when we have a choice child, hence we also run comparison on the
two builders and reuse byStreamClass if they are equal.

Change-Id: Ie58367f7cc899d49da7f78cd0d4e70fbb064ae99
Signed-off-by: Robert Varga <robert.varga@pantheon.tech>
(cherry picked from commit 09870ca2f1234be7c175667341f783c0b95ef408)

binding/mdsal-binding-dom-codec/src/main/java/org/opendaylight/mdsal/binding/dom/codec/impl/DataObjectCodecContext.java

index 3dcc386bd2d48ba5ca5e49369f53274a3e55c2d2..572257e7c9e749f187b428a873ecd47aeda05733 100644 (file)
@@ -151,8 +151,13 @@ abstract class DataObjectCodecContext<D extends DataObject, T extends DataNodeCo
         this.byMethod = byMethodBuilder.build();
         this.byYang = ImmutableMap.copyOf(byYangBuilder);
         this.byStreamClass = ImmutableMap.copyOf(byStreamClassBuilder);
-        byBindingArgClassBuilder.putAll(byStreamClass);
-        this.byBindingArgClass = ImmutableMap.copyOf(byBindingArgClassBuilder);
+
+        // Slight footprint optimization: we do not want to copy byStreamClass, as that would force its entrySet view
+        // to be instantiated. Furthermore the two maps can easily end up being equal -- hence we can reuse
+        // byStreamClass for the purposes of both.
+        byBindingArgClassBuilder.putAll(byStreamClassBuilder);
+        this.byBindingArgClass = byStreamClassBuilder.equals(byBindingArgClassBuilder) ? this.byStreamClass
+                : ImmutableMap.copyOf(byBindingArgClassBuilder);
 
         final Map<Class<?>, Method> clsToNonnull = BindingReflections.getChildrenClassToNonnullMethod(bindingClass);
         final Map<String, String> nonnullToGetterBuilder = new HashMap<>();