From: Robert Varga Date: Thu, 10 Apr 2014 16:59:45 +0000 (+0200) Subject: BUG-509: improve memory efficiency of ListenerTree X-Git-Tag: autorelease-tag-v20140601202136_82eb3f9~244^2 X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?p=controller.git;a=commitdiff_plain;h=8863de0dcb11c160ea067fb157bb95502f49e400;hp=-c BUG-509: improve memory efficiency of ListenerTree As it turns out, we do not need a Set<>, but rather a collection. We do not expect major modifications of listeners, but memory efficiency and iteration speed are required. Thus turn the HashSet into an ArrayList. Also rename DataChangeListenerRegistration to DataChangeListenerRegistrationImpl, so that the interface itself can be imported and used as a shortname. This has the nice effect of rendering a nasty SuppressWarnings completely unneeded. Change-Id: I100337651c3b42c715978708bc98cf24ce5bf703 Signed-off-by: Robert Varga --- 8863de0dcb11c160ea067fb157bb95502f49e400 diff --git a/opendaylight/md-sal/sal-dom-broker/src/main/java/org/opendaylight/controller/md/sal/dom/store/impl/tree/ListenerTree.java b/opendaylight/md-sal/sal-dom-broker/src/main/java/org/opendaylight/controller/md/sal/dom/store/impl/tree/ListenerTree.java index 58a0c69bb0..83cfcaca18 100644 --- a/opendaylight/md-sal/sal-dom-broker/src/main/java/org/opendaylight/controller/md/sal/dom/store/impl/tree/ListenerTree.java +++ b/opendaylight/md-sal/sal-dom-broker/src/main/java/org/opendaylight/controller/md/sal/dom/store/impl/tree/ListenerTree.java @@ -9,9 +9,9 @@ package org.opendaylight.controller.md.sal.dom.store.impl.tree; import java.lang.ref.Reference; import java.lang.ref.WeakReference; +import java.util.ArrayList; import java.util.Collection; import java.util.HashMap; -import java.util.HashSet; import java.util.Map; import java.util.concurrent.locks.Lock; import java.util.concurrent.locks.ReadWriteLock; @@ -21,6 +21,7 @@ import javax.annotation.concurrent.GuardedBy; import org.opendaylight.controller.md.sal.common.api.data.AsyncDataBroker.DataChangeScope; import org.opendaylight.controller.md.sal.common.api.data.AsyncDataChangeListener; +import org.opendaylight.controller.md.sal.dom.store.impl.DataChangeListenerRegistration; import org.opendaylight.yangtools.concepts.AbstractListenerRegistration; import org.opendaylight.yangtools.concepts.Identifiable; import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier; @@ -61,12 +62,12 @@ public final class ListenerTree { try { Node walkNode = rootNode; - for(PathArgument arg : path.getPath()) { + for (final PathArgument arg : path.getPath()) { walkNode = walkNode.ensureChild(arg); } final Node node = walkNode; - DataChangeListenerRegistration listenerReg = new DataChangeListenerRegistration(listener) { + DataChangeListenerRegistration reg = new DataChangeListenerRegistrationImpl(listener) { @Override public DataChangeScope getScope() { return scope; @@ -101,8 +102,8 @@ public final class ListenerTree { } }; - node.addListener(listenerReg); - return listenerReg; + node.addListener(reg); + return reg; } finally { // Always release the lock rwLock.writeLock().unlock(); @@ -155,7 +156,7 @@ public final class ListenerTree { * unclosed. */ public static final class Node implements StoreTreeNode, Identifiable { - private final HashSet> listeners = new HashSet<>(); + private final Collection> listeners = new ArrayList<>(); private final Map children = new HashMap<>(); private final PathArgument identifier; private final Reference parent; @@ -182,13 +183,12 @@ public final class ListenerTree { * * @return the list of current listeners */ - @SuppressWarnings({ "rawtypes", "unchecked" }) - public Collection> getListeners() { - return (Collection) listeners; + public Collection> getListeners() { + return listeners; } private Node ensureChild(final PathArgument child) { - Node potential = (children.get(child)); + Node potential = children.get(child); if (potential == null) { potential = new Node(this, child); children.put(child, potential); @@ -201,7 +201,7 @@ public final class ListenerTree { LOG.debug("Listener {} registered", listener); } - private void removeListener(final DataChangeListenerRegistration listener) { + private void removeListener(final DataChangeListenerRegistrationImpl listener) { listeners.remove(listener); LOG.debug("Listener {} unregistered", listener); @@ -222,12 +222,10 @@ public final class ListenerTree { } } - private abstract static class DataChangeListenerRegistration>> extends AbstractListenerRegistration implements - org.opendaylight.controller.md.sal.dom.store.impl.DataChangeListenerRegistration { - - public DataChangeListenerRegistration(final T listener) { + private abstract static class DataChangeListenerRegistrationImpl>> extends AbstractListenerRegistration // + implements DataChangeListenerRegistration { + public DataChangeListenerRegistrationImpl(final T listener) { super(listener); } } - }