From 6f3efadbc6e76c636a2b199f814ef269df6cac5d Mon Sep 17 00:00:00 2001 From: Robert Varga Date: Sun, 21 Dec 2014 00:47:12 +0100 Subject: [PATCH] BUG-2350: optimize BindingRuntimeContext.getIdentityClass() BGP traces show this method can take upto 35% of CPU time in some use cases. Instantiate a LoadingCache to bypass the cost of loading a class. Change-Id: Iaa49dae655f3987add427e2d9da303b2278f00cb Signed-off-by: Robert Varga --- .../generator/util/BindingRuntimeContext.java | 26 +++++++++++++------ 1 file changed, 18 insertions(+), 8 deletions(-) diff --git a/code-generator/binding-generator-impl/src/main/java/org/opendaylight/yangtools/sal/binding/generator/util/BindingRuntimeContext.java b/code-generator/binding-generator-impl/src/main/java/org/opendaylight/yangtools/sal/binding/generator/util/BindingRuntimeContext.java index 4a3d760172..fe2510857f 100644 --- a/code-generator/binding-generator-impl/src/main/java/org/opendaylight/yangtools/sal/binding/generator/util/BindingRuntimeContext.java +++ b/code-generator/binding-generator-impl/src/main/java/org/opendaylight/yangtools/sal/binding/generator/util/BindingRuntimeContext.java @@ -9,6 +9,9 @@ package org.opendaylight.yangtools.sal.binding.generator.util; import com.google.common.base.Optional; 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 com.google.common.collect.BiMap; import com.google.common.collect.FluentIterable; import com.google.common.collect.HashBiMap; @@ -79,6 +82,20 @@ public class BindingRuntimeContext implements Immutable { private final Multimap choiceToCases = HashMultimap.create(); private final Map identities = new HashMap<>(); + private final LoadingCache> identityClasses = CacheBuilder.newBuilder().weakValues().build( + new CacheLoader>() { + @Override + public Class load(final QName key) { + final Type identityType = identities.get(key); + Preconditions.checkArgument(identityType != null, "Supplied QName %s is not a valid identity", key); + try { + return strategy.loadClass(identityType); + } catch (ClassNotFoundException e) { + throw new IllegalArgumentException("Required class " + identityType + "was not found.", e); + } + } + }); + private BindingRuntimeContext(final ClassLoadingStrategy strategy, final SchemaContext schema) { this.strategy = strategy; this.schemaContext = schema; @@ -367,13 +384,6 @@ public class BindingRuntimeContext implements Immutable { } public Class getIdentityClass(final QName input) { - Type identityType = identities.get(input); - Preconditions.checkArgument(identityType != null, "Supplied QName %s is not a valid identity", input); - try { - return strategy.loadClass(identityType); - } catch (ClassNotFoundException e) { - throw new IllegalArgumentException("Required class " + identityType + "was not found.",e); - } + return identityClasses.getUnchecked(input); } - } -- 2.36.6