BUG-2459: maintain a cache of grammar caches 18/13318/17
authorRobert Varga <rovarga@cisco.com>
Tue, 2 Dec 2014 17:47:36 +0000 (18:47 +0100)
committerRobert Varga <rovarga@cisco.com>
Tue, 16 Dec 2014 10:58:58 +0000 (11:58 +0100)
This just reuses instances across multiple sessions, since the
cardinality is usually low.

Change-Id: Icd18348eee51295f50e224572c103e3b5572963b
Signed-off-by: Robert Varga <rovarga@cisco.com>
opendaylight/netconf/netconf-netty-util/src/main/java/org/opendaylight/controller/netconf/nettyutil/handler/NetconfEXICodec.java

index 8f67da670a9231369cc8914dfb490b02428d88a5..16da7a7f9dcf3eabb7fee06e8c3201fce1c41155 100644 (file)
@@ -1,6 +1,9 @@
 package org.opendaylight.controller.netconf.nettyutil.handler;
 
 import com.google.common.base.Preconditions;
 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;
 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<Short, GrammarCache> GRAMMAR_CACHES = CacheBuilder.newBuilder().weakValues().build(new CacheLoader<Short, GrammarCache>() {
+        @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.
     /**
      * 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);
         }
 
             go = GrammarOptions.addPI(go);
         }
 
-        return new GrammarCache(go);
+        return GRAMMAR_CACHES.getUnchecked(go);
     }
 
     EXIReader getReader() throws EXIOptionsException {
     }
 
     EXIReader getReader() throws EXIOptionsException {