Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit cd92f92f authored by Will Brazil's avatar Will Brazil Committed by Eino-Ville Talvala
Browse files

Camera2: Cache method list in MethodNameInvoker.

Retrieving list of methods in every invoke() call is very expensive.
Caching the list inside the constructor prevents several unnecessary
calls to Class.getMethods().

Test: Run camera2 CTS
Bug: 62490715

Change-Id: Ib2a93af0f364b055df2eab9bd7870730428429ad
parent 7c41ddb9
Loading
Loading
Loading
Loading
+5 −3
Original line number Original line Diff line number Diff line
@@ -15,13 +15,13 @@
 */
 */
package android.hardware.camera2.dispatch;
package android.hardware.camera2.dispatch;


import static com.android.internal.util.Preconditions.checkNotNull;

import android.hardware.camera2.utils.UncheckedThrow;
import android.hardware.camera2.utils.UncheckedThrow;


import java.lang.reflect.Method;
import java.lang.reflect.Method;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentHashMap;


import static com.android.internal.util.Preconditions.*;

/**
/**
 * Invoke a method on a dispatchable by its name (without knowing the {@code Method} ahead of time).
 * Invoke a method on a dispatchable by its name (without knowing the {@code Method} ahead of time).
 *
 *
@@ -31,6 +31,7 @@ public class MethodNameInvoker<T> {


    private final Dispatchable<T> mTarget;
    private final Dispatchable<T> mTarget;
    private final Class<T> mTargetClass;
    private final Class<T> mTargetClass;
    private final Method[] mTargetClassMethods;
    private final ConcurrentHashMap<String, Method> mMethods =
    private final ConcurrentHashMap<String, Method> mMethods =
            new ConcurrentHashMap<>();
            new ConcurrentHashMap<>();


@@ -42,6 +43,7 @@ public class MethodNameInvoker<T> {
     */
     */
    public MethodNameInvoker(Dispatchable<T> target, Class<T> targetClass) {
    public MethodNameInvoker(Dispatchable<T> target, Class<T> targetClass) {
        mTargetClass = targetClass;
        mTargetClass = targetClass;
        mTargetClassMethods = targetClass.getMethods();
        mTarget = target;
        mTarget = target;
    }
    }


@@ -68,7 +70,7 @@ public class MethodNameInvoker<T> {


        Method targetMethod = mMethods.get(methodName);
        Method targetMethod = mMethods.get(methodName);
        if (targetMethod == null) {
        if (targetMethod == null) {
            for (Method method : mTargetClass.getMethods()) {
            for (Method method : mTargetClassMethods) {
                // TODO future: match types of params if possible
                // TODO future: match types of params if possible
                if (method.getName().equals(methodName) &&
                if (method.getName().equals(methodName) &&
                        (params.length == method.getParameterTypes().length) ) {
                        (params.length == method.getParameterTypes().length) ) {