Speed up BindingReflections.getAugmentations() 82/81882/1
authorRobert Varga <robert.varga@pantheon.tech>
Wed, 24 Apr 2019 09:05:38 +0000 (11:05 +0200)
committerRobert Varga <robert.varga@pantheon.tech>
Thu, 2 May 2019 18:55:22 +0000 (20:55 +0200)
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 <robert.varga@pantheon.tech>
(cherry picked from commit efb454b9e1e6869240aebd6489e6219abefec227)

binding/mdsal-binding-spec-util/src/main/java/org/opendaylight/mdsal/binding/spec/reflect/AugmentationFieldGetter.java
binding/mdsal-binding-spec-util/src/main/java/org/opendaylight/mdsal/binding/spec/reflect/BindingReflections.java
binding/mdsal-binding-spec-util/src/test/java/org/opendaylight/mdsal/binding/spec/reflect/AugmentationFieldGetterTest.java

index 37e47f0a0fb876bc9d671ed48a40e691ff78bc34..41d73ee7b7a53f972fb174a482989e14daa64004 100644 (file)
@@ -21,7 +21,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;
 
@@ -31,19 +30,11 @@ abstract class AugmentationFieldGetter {
 
     private static final AugmentationFieldGetter DUMMY = new AugmentationFieldGetter() {
         @Override
-        protected Map<Class<? extends Augmentation<?>>, Augmentation<?>> getAugmentations(final Object input) {
+        Map<Class<? extends Augmentation<?>>, Augmentation<?>> getAugmentations(final Object input) {
             return Collections.emptyMap();
         }
     };
 
-    private static final AugmentationFieldGetter AUGMENTATION_HOLDER_GETTER = new AugmentationFieldGetter() {
-        @Override
-        @SuppressWarnings({"unchecked", "rawtypes"})
-        protected Map<Class<? extends Augmentation<?>>, Augmentation<?>> getAugmentations(final Object input) {
-            return (Map) ((AugmentationHolder<?>) input).augmentations();
-        }
-    };
-
     private static final LoadingCache<Class<?>, AugmentationFieldGetter> AUGMENTATION_GETTERS =
             CacheBuilder.newBuilder().weakKeys().build(new AugmentationGetterLoader());
 
@@ -53,12 +44,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<Class<? extends Augmentation<?>>, Augmentation<?>> getAugmentations(Object input);
+    abstract Map<Class<? extends Augmentation<?>>, Augmentation<?>> getAugmentations(Object input);
 
-    public static AugmentationFieldGetter getGetter(final Class<? extends Object> clz) {
-        if (AugmentationHolder.class.isAssignableFrom(clz)) {
-            return AUGMENTATION_HOLDER_GETTER;
-        }
+    static AugmentationFieldGetter getGetter(final Class<? extends Object> clz) {
         return AUGMENTATION_GETTERS.getUnchecked(clz);
     }
 
@@ -96,7 +84,7 @@ abstract class AugmentationFieldGetter {
 
         @Override
         @SuppressWarnings("checkstyle:illegalCatch")
-        protected Map<Class<? extends Augmentation<?>>, Augmentation<?>> getAugmentations(final Object input) {
+        Map<Class<? extends Augmentation<?>>, Augmentation<?>> getAugmentations(final Object input) {
             try {
                 return (Map<Class<? extends Augmentation<?>>, Augmentation<?>>) fieldGetter.invokeExact(input);
             } catch (Throwable e) {
index 57e4089a7a1c17fc8deaf2f50bdfa31876ef298c..89195fa2672c2a32596870ec91d852edd4c63b04 100644 (file)
@@ -36,6 +36,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;
@@ -565,6 +566,9 @@ public final class BindingReflections {
      * @return Map of augmentations if read was successful, otherwise empty map.
      */
     public static Map<Class<? extends Augmentation<?>>, Augmentation<?>> getAugmentations(final Augmentable<?> input) {
+        if (input instanceof AugmentationHolder) {
+            return ((AugmentationHolder) input).augmentations();
+        }
         return AugmentationFieldGetter.getGetter(input.getClass()).getAugmentations(input);
     }
 
index 6ae817067957acc60009901004fcc80a5c6ff145..65af559a0054127be19ead0e0fdc59604c824506 100644 (file)
@@ -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());