From efb454b9e1e6869240aebd6489e6219abefec227 Mon Sep 17 00:00:00 2001 From: Robert Varga Date: Wed, 24 Apr 2019 11:05:38 +0200 Subject: [PATCH] Speed up BindingReflections.getAugmentations() One of the ways in which we can acquire augmentations is through AugmentationHolder interface. While this is handled through AugmentationFieldGetter.getGetter(), that is not really efficient as it requires indirection through multiple implementations. Since AugmentationFieldGetter is a package-internal implementation detail, factor the check out into BindingReflections, where it can execute directly. This not only improves locality, but also allows us to remove one AugmentationFieldGetter implementation, resulting in bimorphic invocation. Change-Id: I15584dbe6151a70d13a766f27b927f598a5d21ba Signed-off-by: Robert Varga --- .../spec/reflect/AugmentationFieldGetter.java | 20 ++++--------------- .../spec/reflect/BindingReflections.java | 4 ++++ .../reflect/AugmentationFieldGetterTest.java | 4 ---- 3 files changed, 8 insertions(+), 20 deletions(-) diff --git a/binding/mdsal-binding-spec-util/src/main/java/org/opendaylight/mdsal/binding/spec/reflect/AugmentationFieldGetter.java b/binding/mdsal-binding-spec-util/src/main/java/org/opendaylight/mdsal/binding/spec/reflect/AugmentationFieldGetter.java index 33009197f1..ad0c430d2e 100644 --- a/binding/mdsal-binding-spec-util/src/main/java/org/opendaylight/mdsal/binding/spec/reflect/AugmentationFieldGetter.java +++ b/binding/mdsal-binding-spec-util/src/main/java/org/opendaylight/mdsal/binding/spec/reflect/AugmentationFieldGetter.java @@ -24,7 +24,6 @@ import java.util.Collections; import java.util.Map; import org.opendaylight.mdsal.binding.spec.naming.BindingMapping; import org.opendaylight.yangtools.yang.binding.Augmentation; -import org.opendaylight.yangtools.yang.binding.AugmentationHolder; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -34,19 +33,11 @@ abstract class AugmentationFieldGetter { private static final AugmentationFieldGetter DUMMY = new AugmentationFieldGetter() { @Override - protected Map>, Augmentation> getAugmentations(final Object input) { + Map>, Augmentation> getAugmentations(final Object input) { return Collections.emptyMap(); } }; - private static final AugmentationFieldGetter AUGMENTATION_HOLDER_GETTER = new AugmentationFieldGetter() { - @Override - @SuppressWarnings({"unchecked", "rawtypes"}) - protected Map>, Augmentation> getAugmentations(final Object input) { - return (Map) ((AugmentationHolder) input).augmentations(); - } - }; - private static final LoadingCache, AugmentationFieldGetter> AUGMENTATION_GETTERS = CacheBuilder.newBuilder().weakKeys().build(new AugmentationGetterLoader()); @@ -56,12 +47,9 @@ abstract class AugmentationFieldGetter { * @param input Input Data object, from which augmentations should be extracted * @return Map of Augmentation class to augmentation */ - protected abstract Map>, Augmentation> getAugmentations(Object input); + abstract Map>, Augmentation> getAugmentations(Object input); - public static AugmentationFieldGetter getGetter(final Class clz) { - if (AugmentationHolder.class.isAssignableFrom(clz)) { - return AUGMENTATION_HOLDER_GETTER; - } + static AugmentationFieldGetter getGetter(final Class clz) { return AUGMENTATION_GETTERS.getUnchecked(clz); } @@ -102,7 +90,7 @@ abstract class AugmentationFieldGetter { @Override @SuppressWarnings("checkstyle:illegalCatch") - protected Map>, Augmentation> getAugmentations(final Object input) { + Map>, Augmentation> getAugmentations(final Object input) { try { return (Map>, Augmentation>) fieldGetter.invokeExact(input); } catch (Throwable e) { diff --git a/binding/mdsal-binding-spec-util/src/main/java/org/opendaylight/mdsal/binding/spec/reflect/BindingReflections.java b/binding/mdsal-binding-spec-util/src/main/java/org/opendaylight/mdsal/binding/spec/reflect/BindingReflections.java index af800bee26..5875f17b91 100644 --- a/binding/mdsal-binding-spec-util/src/main/java/org/opendaylight/mdsal/binding/spec/reflect/BindingReflections.java +++ b/binding/mdsal-binding-spec-util/src/main/java/org/opendaylight/mdsal/binding/spec/reflect/BindingReflections.java @@ -38,6 +38,7 @@ import org.opendaylight.yangtools.util.ClassLoaderUtils; import org.opendaylight.yangtools.yang.binding.Action; import org.opendaylight.yangtools.yang.binding.Augmentable; import org.opendaylight.yangtools.yang.binding.Augmentation; +import org.opendaylight.yangtools.yang.binding.AugmentationHolder; import org.opendaylight.yangtools.yang.binding.BaseIdentity; import org.opendaylight.yangtools.yang.binding.ChildOf; import org.opendaylight.yangtools.yang.binding.DataContainer; @@ -548,6 +549,9 @@ public final class BindingReflections { * @return Map of augmentations if read was successful, otherwise empty map. */ public static Map>, Augmentation> getAugmentations(final Augmentable input) { + if (input instanceof AugmentationHolder) { + return ((AugmentationHolder) input).augmentations(); + } return AugmentationFieldGetter.getGetter(input.getClass()).getAugmentations(input); } diff --git a/binding/mdsal-binding-spec-util/src/test/java/org/opendaylight/mdsal/binding/spec/reflect/AugmentationFieldGetterTest.java b/binding/mdsal-binding-spec-util/src/test/java/org/opendaylight/mdsal/binding/spec/reflect/AugmentationFieldGetterTest.java index e2de73d8d4..b37d713ad8 100644 --- a/binding/mdsal-binding-spec-util/src/test/java/org/opendaylight/mdsal/binding/spec/reflect/AugmentationFieldGetterTest.java +++ b/binding/mdsal-binding-spec-util/src/test/java/org/opendaylight/mdsal/binding/spec/reflect/AugmentationFieldGetterTest.java @@ -18,15 +18,11 @@ import java.util.HashMap; import java.util.Map; import org.junit.Test; import org.opendaylight.yangtools.yang.binding.Augmentation; -import org.opendaylight.yangtools.yang.binding.AugmentationHolder; public class AugmentationFieldGetterTest { @Test public void getGetterTest() throws Exception { - assertNotNull(getGetter(AugmentationHolder.class)); - assertTrue(getGetter(AugmentationHolder.class) - .getAugmentations(mock(AugmentationHolder.class)).isEmpty()); assertTrue(getGetter(Object.class).getAugmentations(null).isEmpty()); assertTrue(getGetter(TestAugmentationWrongTypeClass.class).getAugmentations(null).isEmpty()); -- 2.36.6