Implementing code using methods in java.util.function is ten times slower than normal java code?

problem description

java.util.function,,

A code that determines whether a field is empty has been rewritten (some business definitions assume that a field is empty by default if it is equal to-1).

,,,:

related codes

/ / Old Code

public static <T> boolean isBlank(T t) {

    if (t == null) {
        return true;
    }

    if (t instanceof List) {
        if (((List) t).size() == 0) {
            return true;
        }
    } else if (t instanceof Map) {
        if (((Map) t).size() == 0) {
            return true;
        }
        if (((Map) t).get("-1") != null && ((Map) t).get("-1").equals("-1")) {
            return true;
        }
    } else if (t instanceof Set) {
        if (((Set) t).size() == 0) {
            return true;
        }
    } else if (t instanceof Object[]) {
        if (((Object[]) t).length == 0) {
            return true;
        }
    } else if (t instanceof String) {
        String str = (String) t;
        if (str.length() == 0)
            return true;

        str = str.trim();
        if (str.length() == 0)
            return true;
    }
    return false;
}

public static <T> boolean isinFallback(T t) {
    Class<?> c = t.getClass();
    Method[] methods = c.getMethods();
    try {
        for (Method method : methods) {
            if (method.getName().equals("getId")) {
                Object o = method.invoke(t);
                if (o == null) {
                    return true;
                }
                if (o.equals(-1L)) {
                    return true;
                }
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
        return true;
    }
    return false;
}

/ / New Code

private static final Function<Method[], Method> filter = (s) -> {
    for (Method m : s) {
        if (m.getName().equals("getId")) {
            return m;
        } else if (m.getName().equals("getUid")) {
            return m;
        }
    }
    return null;
};

private static final Predicate<Object> object = (s) -> s.equals("-1");

private static final Function<Object, Predicate> getPre = (s) -> {
    if (s instanceof List) {
        return (Predicate<List>) l -> l.size() == 0;
    } else if (s instanceof Map) {
        return (Predicate<Map>) l -> l.size() == 0 || l.get("-1") != null;
    } else if (s instanceof Set) {
        return (Predicate<Set>) l -> l.size() == 0;
    } else if (s instanceof Object[]) {
        return (Predicate<Object[]>) l -> Objects.nonNull(l) && l.length == 0;
    } else if (s instanceof String) {
        return (Predicate<String>) String::isEmpty;
    }
    return null;
};


public static <T> boolean isBlank(T t) {
    if (t == null) {
        return true;
    }
    if (t instanceof String) {
        String str = (String) t;
        str = str.trim();
        return getPre.apply(str).test(str);
    }
    Predicate apply = getPre.apply(t);
    return apply != null && apply.test(t);
}

public static <T> boolean isinFallback(T t) {
    Class<?> c = t.getClass();
    Method[] method = c.getMethods();
    try {
        Method apply = filter.apply(method);
        return apply != null && object.or(oj -> oj.equals(-1L)).test(apply.invoke(t));
    } catch (IllegalAccessException | InvocationTargetException e) {
        e.printStackTrace();
    }
    return false;
}

Test code:

clipboard.png

:

clipboard.png

question:

the results of the three methods are inconsistent because I have added some new judgments, so don"t worry about it. The confusion is that the code logic has not changed at all, only the implementation method has been replaced with the new implementation of java8, and the performance is so much worse. So the question is:

1. If the problem is caused by the wrong logic written in my code, where am I wrong?

2. Looking at the method introduction, the class Predicate seems to exist because it can do some judgmental actions, but what is the need to use it when its performance is so slow?

3. If I kill a chicken with an awesome knife, then where is the correct use of several utility classes under the java.util.function package (Predicate, Consumer, Function, Supplier, UnaryOperator, BinaryOperator)?


the code doesn't seem to be fully posted. I don't know if it's such a problem:

  1. directly judge that vs has a layer of judgment, and the latter must be slow
  2. but if there is jit and the latter is inlined, the performance gap should be small
  3. The function interface of
  4. java8 is more used for writing lambda, not for direct use
  5. .

I've noticed this problem, too, but it's not what the landlord said. implementation code using methods in java.util.function is ten times slower than normal java code? . He is slow in initialization. I can see the test case used by the main building after a little modification. (the LegalPredicate.isBlank () methods here are all implemented with Function )

  

I don't know if you know this belongs to Java reflection?
reflection does not need virtual machine optimization because it involves dynamic type parsing, and the performance of reflection operation is lower than that of non-reflection operation

.
Menu