-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Cache constructor MethodHandles in factories
Update LocalCacheFactory.java
- Loading branch information
1 parent
9bb8228
commit 557aa49
Showing
4 changed files
with
66 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,14 +15,15 @@ | |
*/ | ||
package com.github.benmanes.caffeine; | ||
|
||
import java.lang.invoke.LambdaMetafactory; | ||
import java.lang.invoke.MethodHandle; | ||
import java.lang.invoke.MethodHandles; | ||
import java.lang.invoke.MethodType; | ||
import java.lang.reflect.Constructor; | ||
|
||
import org.openjdk.jmh.annotations.Benchmark; | ||
import org.openjdk.jmh.annotations.Scope; | ||
import org.openjdk.jmh.annotations.State; | ||
import org.openjdk.jmh.infra.Blackhole; | ||
|
||
/** | ||
* @author [email protected] (Ben Manes) | ||
|
@@ -34,35 +35,46 @@ public class FactoryBenchmark { | |
private final MethodHandleFactory methodHandleFactory = new MethodHandleFactory(); | ||
|
||
@Benchmark | ||
public Alpha direct() { | ||
return new Alpha(); | ||
public void direct(Blackhole blackhole) { | ||
blackhole.consume(new Alpha()); | ||
} | ||
|
||
@Benchmark | ||
public Alpha methodHandle_invoke() { | ||
return methodHandleFactory.invoke(); | ||
public void methodHandle_invoke(Blackhole blackhole) { | ||
blackhole.consume(methodHandleFactory.invoke()); | ||
} | ||
|
||
@Benchmark | ||
public Alpha methodHandle_invokeExact() { | ||
return methodHandleFactory.invokeExact(); | ||
public void methodHandle_invokeExact(Blackhole blackhole) { | ||
blackhole.consume(methodHandleFactory.invokeExact()); | ||
} | ||
|
||
@Benchmark | ||
public Alpha reflection() { | ||
return reflectionFactory.newInstance(); | ||
public void methodHandle_lambda(Blackhole blackhole) { | ||
blackhole.consume(methodHandleFactory.lambda()); | ||
} | ||
|
||
@Benchmark | ||
public void reflection(Blackhole blackhole) { | ||
blackhole.consume(reflectionFactory.newInstance()); | ||
} | ||
|
||
static final class MethodHandleFactory { | ||
private static final MethodHandles.Lookup LOOKUP = MethodHandles.lookup(); | ||
private static final MethodType METHOD_TYPE = MethodType.methodType(void.class); | ||
|
||
private final MethodHandle methodHandle; | ||
private final AlphaConstructor lambda; | ||
|
||
MethodHandleFactory() { | ||
try { | ||
methodHandle = LOOKUP.findConstructor(Alpha.class, METHOD_TYPE); | ||
} catch (NoSuchMethodException | IllegalAccessException e) { | ||
lambda = | ||
(AlphaConstructor) LambdaMetafactory | ||
.metafactory(LOOKUP, "construct", MethodType.methodType(AlphaConstructor.class), | ||
methodHandle.type(), methodHandle, methodHandle.type()) | ||
.getTarget().invokeExact(); | ||
} catch (Throwable e) { | ||
throw new RuntimeException(e); | ||
} | ||
} | ||
|
@@ -82,6 +94,10 @@ Alpha invokeExact() { | |
throw new RuntimeException(e); | ||
} | ||
} | ||
|
||
Alpha lambda() { | ||
return lambda.construct(); | ||
} | ||
} | ||
|
||
static final class ReflectionFactory { | ||
|
@@ -107,4 +123,8 @@ Alpha newInstance() { | |
static final class Alpha { | ||
public Alpha() {} | ||
} | ||
|
||
private interface AlphaConstructor { | ||
Alpha construct(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters