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>
*/
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;
if (map.isEmpty()) {
return Collections.emptyMap();
}
+ if (map.size() < WRAP_THRESHOLD) {
+ return ImmutableMap.copyOf(map);
+ }
return new UnmodifiableChildrenMap(map);
}