From: Robert Varga Date: Tue, 2 Dec 2014 17:47:36 +0000 (+0100) Subject: BUG-2459: maintain a cache of grammar caches X-Git-Tag: release/lithium~751^2 X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?p=controller.git;a=commitdiff_plain;h=dcf48864ca54fe1b27d44de10e0bcb6e241cc5c4;ds=inline BUG-2459: maintain a cache of grammar caches This just reuses instances across multiple sessions, since the cardinality is usually low. Change-Id: Icd18348eee51295f50e224572c103e3b5572963b Signed-off-by: Robert Varga --- diff --git a/opendaylight/netconf/netconf-netty-util/src/main/java/org/opendaylight/controller/netconf/nettyutil/handler/NetconfEXICodec.java b/opendaylight/netconf/netconf-netty-util/src/main/java/org/opendaylight/controller/netconf/nettyutil/handler/NetconfEXICodec.java index 8f67da670a..16da7a7f9d 100644 --- a/opendaylight/netconf/netconf-netty-util/src/main/java/org/opendaylight/controller/netconf/nettyutil/handler/NetconfEXICodec.java +++ b/opendaylight/netconf/netconf-netty-util/src/main/java/org/opendaylight/controller/netconf/nettyutil/handler/NetconfEXICodec.java @@ -1,6 +1,9 @@ package org.opendaylight.controller.netconf.nettyutil.handler; import com.google.common.base.Preconditions; +import com.google.common.cache.CacheBuilder; +import com.google.common.cache.CacheLoader; +import com.google.common.cache.LoadingCache; import org.openexi.proc.HeaderOptionsOutputType; import org.openexi.proc.common.EXIOptions; import org.openexi.proc.common.EXIOptionsException; @@ -30,6 +33,17 @@ public final class NetconfEXICodec { } }; + /** + * Since we have a limited number of options we can have, instantiating a weak cache + * will allow us to reuse instances where possible. + */ + private static final LoadingCache GRAMMAR_CACHES = CacheBuilder.newBuilder().weakValues().build(new CacheLoader() { + @Override + public GrammarCache load(final Short key) { + return new GrammarCache(key); + } + }); + /** * Grammar cache acts as a template and is duplicated by the Transmogrifier and the Reader * before use. It is safe to reuse a single instance. @@ -57,7 +71,7 @@ public final class NetconfEXICodec { go = GrammarOptions.addPI(go); } - return new GrammarCache(go); + return GRAMMAR_CACHES.getUnchecked(go); } EXIReader getReader() throws EXIOptionsException {