Introduce SharedSingletonMap 45/26545/8
authorRobert Varga <rovarga@cisco.com>
Fri, 4 Sep 2015 14:49:58 +0000 (16:49 +0200)
committerGerrit Code Review <gerrit@opendaylight.org>
Thu, 10 Sep 2015 07:13:02 +0000 (07:13 +0000)
commit94ce73de754dde1d931d7d19e6eeb79de997bdef
tree452d2b2a09a1c89b94758b189c1a939eb6cb0f4e
parent83fc678fd751a071d0140505e7efd8d9568f3810
Introduce SharedSingletonMap

ImmutableMap can be very expensive in terms of memory overhead when
storing a large number of singleton maps which have the same keySet(),
because it instantiates an ImmutableSet, which remains cached. That ends
up costing 24 bytes in local testing.

This patch implements a replacement class which retains the same
properties, but shares the keySet instances -- thus lowering the overall
overhead. For these instances we introduce an SingletonSet class, which
is memory-efficient and gives us direct access to the container entry.

This leaves entrySet() as the only method which allocates objects. We do
not cache returned objects in the expectation any caller users them as
pure DTOs, hence they end up being cheap.

Change-Id: I64e74d8e661d2689d32bea31e1cdf72e87ed64af
Signed-off-by: Robert Varga <rovarga@cisco.com>
common/util/src/main/java/org/opendaylight/yangtools/util/ImmutableOffsetMap.java
common/util/src/main/java/org/opendaylight/yangtools/util/MutableOffsetMap.java
common/util/src/main/java/org/opendaylight/yangtools/util/SharedSingletonMap.java [new file with mode: 0644]
common/util/src/main/java/org/opendaylight/yangtools/util/SingletonSet.java [new file with mode: 0644]
common/util/src/test/java/org/opendaylight/yangtools/util/OffsetMapTest.java
common/util/src/test/java/org/opendaylight/yangtools/util/SharedSingletonMapTest.java [new file with mode: 0644]
common/util/src/test/java/org/opendaylight/yangtools/util/SingletonSetTest.java [new file with mode: 0644]
yang/yang-data-api/src/main/java/org/opendaylight/yangtools/yang/data/api/YangInstanceIdentifier.java