-
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.
Prefer method handles over reflection [skip ci]
This restores an optimization that was removed in #222 due to a memory leak in older JDKs.
- Loading branch information
Showing
13 changed files
with
73 additions
and
657 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 |
---|---|---|
|
@@ -16,7 +16,9 @@ | |
package com.github.benmanes.caffeine.cache; | ||
|
||
import static com.github.benmanes.caffeine.cache.Specifications.BOUNDED_LOCAL_CACHE; | ||
import static com.github.benmanes.caffeine.cache.Specifications.BUILDER; | ||
import static com.github.benmanes.caffeine.cache.Specifications.BUILDER_PARAM; | ||
import static com.github.benmanes.caffeine.cache.Specifications.CACHE_LOADER; | ||
import static com.github.benmanes.caffeine.cache.Specifications.CACHE_LOADER_PARAM; | ||
import static com.github.benmanes.caffeine.cache.Specifications.kTypeVar; | ||
import static com.github.benmanes.caffeine.cache.Specifications.vTypeVar; | ||
|
@@ -25,6 +27,8 @@ | |
import static java.util.stream.Collectors.toList; | ||
|
||
import java.io.IOException; | ||
import java.lang.invoke.MethodHandles; | ||
import java.lang.invoke.MethodType; | ||
import java.nio.file.Files; | ||
import java.nio.file.Path; | ||
import java.nio.file.Paths; | ||
|
@@ -67,6 +71,7 @@ | |
import com.google.googlejavaformat.java.Formatter; | ||
import com.google.googlejavaformat.java.FormatterException; | ||
import com.squareup.javapoet.ClassName; | ||
import com.squareup.javapoet.FieldSpec; | ||
import com.squareup.javapoet.JavaFile; | ||
import com.squareup.javapoet.MethodSpec; | ||
import com.squareup.javapoet.ParameterizedTypeName; | ||
|
@@ -79,6 +84,16 @@ | |
* @author [email protected] (Ben Manes) | ||
*/ | ||
public final class LocalCacheFactoryGenerator { | ||
static final FieldSpec LOOKUP = FieldSpec.builder(MethodHandles.Lookup.class, "LOOKUP") | ||
.initializer("$T.lookup()", MethodHandles.class) | ||
.addModifiers(Modifier.PUBLIC, Modifier.STATIC, Modifier.FINAL) | ||
.build(); | ||
static final FieldSpec FACTORY = FieldSpec.builder(MethodType.class, "FACTORY") | ||
.initializer("$T.methodType($T.class, $T.class, $T.class, $T.class)", | ||
MethodType.class, void.class, BUILDER, CACHE_LOADER.rawType, TypeName.BOOLEAN) | ||
.addModifiers(Modifier.PUBLIC, Modifier.STATIC, Modifier.FINAL) | ||
.build(); | ||
|
||
final Feature[] featureByIndex = { null, null, Feature.LISTENING, Feature.STATS, | ||
Feature.MAXIMUM_SIZE, Feature.MAXIMUM_WEIGHT, Feature.EXPIRE_ACCESS, | ||
Feature.EXPIRE_WRITE, Feature.REFRESH_WRITE}; | ||
|
@@ -105,6 +120,7 @@ void generate() throws IOException { | |
.addModifiers(Modifier.FINAL) | ||
.addMethod(MethodSpec.constructorBuilder().addModifiers(Modifier.PRIVATE).build()); | ||
addClassJavaDoc(); | ||
addConstants(); | ||
generateLocalCaches(); | ||
addFactoryMethods(); | ||
writeJavaFile(); | ||
|
@@ -228,6 +244,11 @@ private void addClassJavaDoc() { | |
.addJavadoc("\n@author [email protected] (Ben Manes)\n"); | ||
} | ||
|
||
private void addConstants() { | ||
factory.addField(LOOKUP); | ||
factory.addField(FACTORY); | ||
} | ||
|
||
/** Returns an encoded form of the class name for compact use. */ | ||
private static String encode(String className) { | ||
return Feature.makeEnumName(className) | ||
|
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,15 +15,14 @@ | |
*/ | ||
package com.github.benmanes.caffeine.cache; | ||
|
||
import static com.github.benmanes.caffeine.cache.LocalCacheFactoryGenerator.FACTORY; | ||
import static com.github.benmanes.caffeine.cache.LocalCacheFactoryGenerator.LOOKUP; | ||
import static com.github.benmanes.caffeine.cache.Specifications.BOUNDED_LOCAL_CACHE; | ||
import static com.github.benmanes.caffeine.cache.Specifications.BUILDER; | ||
import static com.github.benmanes.caffeine.cache.Specifications.CACHE_LOADER; | ||
import static com.github.benmanes.caffeine.cache.Specifications.LOCAL_CACHE_FACTORY; | ||
|
||
import java.lang.reflect.Constructor; | ||
import java.lang.invoke.MethodHandle; | ||
|
||
import com.squareup.javapoet.CodeBlock; | ||
import com.squareup.javapoet.TypeName; | ||
|
||
/** | ||
* @author [email protected] (Ben Manes) | ||
|
@@ -100,15 +99,15 @@ private LocalCacheSelectorCode selector() { | |
.beginControlFlow("try") | ||
.addStatement("$T<?> clazz = $T.class.getClassLoader().loadClass(sb.toString())", | ||
Class.class, LOCAL_CACHE_FACTORY) | ||
.addStatement("$T<?> ctor = clazz.getDeclaredConstructor($T.class, $T.class, $T.class)", | ||
Constructor.class, BUILDER, CACHE_LOADER.rawType, TypeName.BOOLEAN) | ||
.add("@SuppressWarnings($S)\n", "unchecked") | ||
.addStatement("$1T factory = ($1T) ctor.newInstance(builder, cacheLoader, async)", | ||
.addStatement("$T handle = $N.findConstructor(clazz, $N)", | ||
MethodHandle.class, LOOKUP, FACTORY) | ||
.addStatement("return ($T) handle.invoke(builder, cacheLoader, async)", | ||
BOUNDED_LOCAL_CACHE) | ||
.addStatement("return factory") | ||
.nextControlFlow("catch ($T e)", ReflectiveOperationException.class) | ||
.addStatement("throw new $T(sb.toString(), e)", IllegalStateException.class) | ||
.endControlFlow(); | ||
.nextControlFlow("catch ($T | $T e)", RuntimeException.class, Error.class) | ||
.addStatement("throw e") | ||
.nextControlFlow("catch ($T t)", Throwable.class) | ||
.addStatement("throw new $T(sb.toString(), t)", IllegalStateException.class) | ||
.endControlFlow(); | ||
return this; | ||
} | ||
|
||
|
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
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
49 changes: 0 additions & 49 deletions
49
caffeine/src/jmh/java/com/github/benmanes/caffeine/profiler/SingleConsumerQueueProfiler.java
This file was deleted.
Oops, something went wrong.
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
Oops, something went wrong.