mercredi 10 janvier 2024

lambdametafactory creation is quite slow compared to just using lambda

@Benchmark
public void testLambdaGeneration(Blackhole bh) {
    Function<Object, Object> function = a -> {
        if(a instanceof Alpha alpha) {
            return alpha.primitiveInt();
        }else {
            throw new RuntimeException();
        }
    };
    bh.consume(function);
}

@Benchmark
public void testManualGeneration(Blackhole bh) {
    try{
        MethodHandle mh = MethodHandles.lookup().findVirtual(Alpha.class, "primitiveInt", MethodType.methodType(int.class));
        MethodType type = mh.type();
        type = type.changeReturnType(Integer.class);
        CallSite callSite = LambdaMetafactory.metafactory(MethodHandles.lookup(),
                "apply",
                MethodType.methodType(Function.class),
                type.erase(), mh, type);
        Function<Object, Object> f = (Function<Object, Object>) callSite.getTarget().invokeExact();
        bh.consume(f);
    }catch (Throwable e) {
        throw new RuntimeException("Unreached");
    }
}

I want to use lambdametafactory to create constructor, getter and setter methods for reflection usage, and generated Function<> is just as fast as the direct call, which greatly satisfies my needs. However, when I use the above code to test the generation performance between lambdafactory and pure lambda, the result is quite unpromising:

LambdaTest.testLambdaGeneration  avgt   50      0.574 ±     0.015  ns/op
LambdaTest.testManualGeneration  avgt   50  29424.815 ± 20402.801  ns/op

I think the lambda function would use LambdaMetafactory internally, so the result should be consistent, but the actual result really shock me.

Is there any thing wrong with my code? How could I improve the LambdaMetafactory.metafactory()'s performance to match the pure lambda generation?





Aucun commentaire:

Enregistrer un commentaire