Skip to content

Instantly share code, notes, and snippets.

@games647
Created May 18, 2020 14:43
Show Gist options
  • Save games647/88c4439e1cd7810f21318b1b24a04ee0 to your computer and use it in GitHub Desktop.
Save games647/88c4439e1cd7810f21318b1b24a04ee0 to your computer and use it in GitHub Desktop.
Java Reflection performance
OpenJDK 64-Bit Server VM, 14.0.1
ReflectionTest.lambda avgt 5 2,602 ± 0,038 ns/op
ReflectionTest.mh_invoke avgt 5 5,787 ± 0,445 ns/op
ReflectionTest.mh_invokeExact avgt 5 2,744 ± 0,279 ns/op
ReflectionTest.plain avgt 5 2,601 ± 0,022 ns/op
ReflectionTest.reflect avgt 5 5,615 ± 0,120 ns/op
ReflectionTest.reflectAccess avgt 5 5,058 ± 0,059 ns/op
ReflectionTest.unreflect_invoke avgt 5 5,564 ± 0,180 ns/op
ReflectionTest.unreflect_invoke2 avgt 5 5,587 ± 0,463 ns/op
ReflectionTest.unreflect_invokeExact avgt 5 2,608 ± 0,011 ns/op
ReflectionTest.unreflect_invokeExact2 avgt 5 2,594 ± 0,015 ns/op
JDK 1.8.0_252, OpenJDK 64-Bit Server VM, 25.252-b09
ReflectionTest.lambda avgt 5 2,748 ± 0,209 ns/op
ReflectionTest.mh_invoke avgt 5 5,293 ± 0,039 ns/op
ReflectionTest.mh_invokeExact avgt 5 2,754 ± 0,050 ns/op
ReflectionTest.plain avgt 5 2,755 ± 0,050 ns/op
ReflectionTest.reflect avgt 5 5,628 ± 0,215 ns/op
ReflectionTest.reflectAccess avgt 5 4,883 ± 0,054 ns/op
ReflectionTest.unreflect_invoke avgt 5 5,297 ± 0,063 ns/op
ReflectionTest.unreflect_invoke2 avgt 5 5,270 ± 0,019 ns/op
ReflectionTest.unreflect_invokeExact avgt 5 2,823 ± 0,008 ns/op
ReflectionTest.unreflect_invokeExact2 avgt 5 2,752 ± 0,009 ns/op
VM version: JDK 1.8.0_252, Eclipse OpenJ9 VM, openj9-0.20.0
ReflectionTest.lambda avgt 5 5,338 ± 7,947 ns/op
ReflectionTest.mh_invoke avgt 5 7,607 ± 1,442 ns/op
ReflectionTest.mh_invokeExact avgt 5 2,812 ± 6,027 ns/op
ReflectionTest.plain avgt 5 2,117 ± 0,054 ns/op
ReflectionTest.reflect avgt 5 8,222 ± 1,656 ns/op
ReflectionTest.reflectAccess avgt 5 4,771 ± 0,269 ns/op
ReflectionTest.unreflect_invoke avgt 5 7,770 ± 1,617 ns/op
ReflectionTest.unreflect_invoke2 avgt 5 7,870 ± 1,809 ns/op
ReflectionTest.unreflect_invokeExact avgt 5 2,109 ± 0,004 ns/op
ReflectionTest.unreflect_invokeExact2 avgt 5 2,809 ± 6,028 ns/op
package org.sample;
import java.lang.invoke.LambdaMetafactory;
import java.lang.invoke.MethodHandle;
import java.lang.invoke.MethodHandles;
import java.lang.invoke.MethodHandles.Lookup;
import java.lang.invoke.MethodType;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.UUID;
import java.util.concurrent.TimeUnit;
import java.util.function.Supplier;
import org.openjdk.jmh.annotations.Benchmark;
import org.openjdk.jmh.annotations.BenchmarkMode;
import org.openjdk.jmh.annotations.Fork;
import org.openjdk.jmh.annotations.Measurement;
import org.openjdk.jmh.annotations.Mode;
import org.openjdk.jmh.annotations.OutputTimeUnit;
import org.openjdk.jmh.annotations.Scope;
import org.openjdk.jmh.annotations.State;
import org.openjdk.jmh.annotations.Warmup;
@Warmup(iterations = 5, time = 1, timeUnit = TimeUnit.SECONDS)
@Measurement(iterations = 5, time = 1, timeUnit = TimeUnit.SECONDS)
@Fork(1)
@BenchmarkMode(Mode.AverageTime)
@OutputTimeUnit(TimeUnit.NANOSECONDS)
@State(Scope.Thread)
public class ReflectionTest {
public static UUID value = UUID.randomUUID();
private static final Method reflective;
private static final Method reflectiveAccess;
private static final MethodHandle unreflect;
private static final MethodHandle unreflectAccess;
private static final MethodHandle mh;
private static final Supplier<UUID> sup;
public static UUID getUniqueId() {
return value;
}
static {
try {
reflective = ReflectionTest.class.getDeclaredMethod("getUniqueId");
reflectiveAccess = ReflectionTest.class.getDeclaredMethod("getUniqueId");
reflectiveAccess.setAccessible(true);
final Lookup lookup = MethodHandles.lookup();
unreflect = lookup.unreflect(reflective);
unreflectAccess = lookup.unreflect(reflectiveAccess);
mh = lookup.findStatic(ReflectionTest.class, "getUniqueId", MethodType.methodType(UUID.class));
MethodType methodType = MethodType.methodType(Object.class);
sup = (Supplier<UUID>) LambdaMetafactory.metafactory(lookup,
"get", MethodType.methodType(Supplier.class), methodType,
mh, methodType).getTarget().invoke();
} catch (Throwable throwable) {
throwable.printStackTrace();
throw new IllegalStateException("This shouldn't happen");
}
}
@Benchmark
public UUID plain() {
return value;
}
@Benchmark
public Object reflect() throws InvocationTargetException, IllegalAccessException {
return reflective.invoke(null);
}
@Benchmark
public Object reflectAccess() throws InvocationTargetException, IllegalAccessException {
return reflectiveAccess.invoke(null);
}
@Benchmark
public Object unreflect_invoke() throws Throwable {
return unreflect.invoke();
}
@Benchmark
public Object unreflect_invokeExact() throws Throwable {
return (UUID) unreflect.invokeExact();
}
@Benchmark
public Object unreflect_invoke2() throws Throwable {
return unreflectAccess.invoke();
}
@Benchmark
public Object unreflect_invokeExact2() throws Throwable {
return (UUID) unreflectAccess.invokeExact();
}
@Benchmark
public Object mh_invoke() throws Throwable {
return mh.invoke();
}
@Benchmark
public Object mh_invokeExact() throws Throwable {
return (UUID) mh.invokeExact();
}
@Benchmark
public Object lambda() throws Throwable {
return sup.get();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment