Skip to content

Commit

Permalink
Removed unneccesary top-level cache fields
Browse files Browse the repository at this point in the history
Previously the cache implemented AbstractMap for the its convenient
putAll, equals, hashCode, and toString implementations. This came at
the cost of its private, unused keySet and values fields. Those Object
methods iterated over the entrySet, which returns a copy of the entry
to avoid instabiliy due to concurrent mutations. This changes to
internal iterators which avoids this unnecessary garbage creation.

Equality is a complex subject and the implementation's internal docs
now better clarify the semantics. Due to the cache potentially holding
entries that are pending removal (expiration, reference collection),
we expect that usages call Cache.cleanUp() first.

Removed an adapter for the async cache loader as not needed, dropping
two instance fields.

Simplified some serialization code by removing redundancies.
  • Loading branch information
ben-manes committed Mar 14, 2022
1 parent 2c09076 commit 003be23
Show file tree
Hide file tree
Showing 15 changed files with 536 additions and 186 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@
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.ASYNC_CACHE_LOADER;
import static com.github.benmanes.caffeine.cache.Specifications.ASYNC_CACHE_LOADER_PARAM;
import static com.github.benmanes.caffeine.cache.Specifications.kTypeVar;
import static com.github.benmanes.caffeine.cache.Specifications.vTypeVar;
import static java.nio.charset.StandardCharsets.UTF_8;
Expand Down Expand Up @@ -90,7 +90,7 @@ public final class LocalCacheFactoryGenerator {
.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)
MethodType.class, void.class, BUILDER, ASYNC_CACHE_LOADER.rawType, TypeName.BOOLEAN)
.addModifiers(Modifier.PUBLIC, Modifier.STATIC, Modifier.FINAL)
.build();

Expand Down Expand Up @@ -136,7 +136,7 @@ private void addFactoryMethods() {
.addModifiers(Modifier.STATIC)
.addCode(LocalCacheSelectorCode.get())
.addParameter(BUILDER_PARAM)
.addParameter(CACHE_LOADER_PARAM.toBuilder().addAnnotation(Nullable.class).build())
.addParameter(ASYNC_CACHE_LOADER_PARAM.toBuilder().addAnnotation(Nullable.class).build())
.addParameter(boolean.class, "async")
.addJavadoc("Returns a cache optimized for this configuration.\n")
.build());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,10 +76,10 @@ public final class Specifications {
ClassName.get(PACKAGE_NAME, "BoundedLocalCache"), kTypeVar, vTypeVar);
public static final TypeName NODE = ParameterizedTypeName.get(nodeType, kTypeVar, vTypeVar);

public static final ParameterizedTypeName CACHE_LOADER = ParameterizedTypeName.get(
ClassName.get(PACKAGE_NAME, "CacheLoader"), TypeVariableName.get("? super K"), vTypeVar);
public static final ParameterSpec CACHE_LOADER_PARAM =
ParameterSpec.builder(CACHE_LOADER, "cacheLoader").build();
public static final ParameterizedTypeName ASYNC_CACHE_LOADER = ParameterizedTypeName.get(
ClassName.get(PACKAGE_NAME, "AsyncCacheLoader"), TypeVariableName.get("? super K"), vTypeVar);
public static final ParameterSpec ASYNC_CACHE_LOADER_PARAM =
ParameterSpec.builder(ASYNC_CACHE_LOADER, "cacheLoader").build();

public static final TypeName REMOVAL_LISTENER = ParameterizedTypeName.get(
ClassName.get(PACKAGE_NAME, "RemovalListener"), kTypeVar, vTypeVar);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@
*/
package com.github.benmanes.caffeine.cache.local;

import static com.github.benmanes.caffeine.cache.Specifications.ASYNC_CACHE_LOADER_PARAM;
import static com.github.benmanes.caffeine.cache.Specifications.BOUNDED_LOCAL_CACHE;
import static com.github.benmanes.caffeine.cache.Specifications.BUILDER_PARAM;
import static com.github.benmanes.caffeine.cache.Specifications.CACHE_LOADER_PARAM;

/**
* Adds the constructor to the cache.
Expand All @@ -33,17 +33,16 @@ protected boolean applies() {

@Override
protected void execute() {
String cacheLoader;
context.constructor
.addParameter(BUILDER_PARAM)
.addParameter(ASYNC_CACHE_LOADER_PARAM)
.addParameter(boolean.class, "async");
if (context.superClass.equals(BOUNDED_LOCAL_CACHE)) {
cacheLoader = "(CacheLoader<K, V>) cacheLoader";
context.suppressedWarnings.add("unchecked");
context.constructor.addStatement(
"super(builder, (AsyncCacheLoader<K, V>) cacheLoader, async)");
} else {
cacheLoader = "cacheLoader";
context.constructor.addStatement("super(builder, cacheLoader, async)");
}
context.constructor
.addParameter(BUILDER_PARAM)
.addParameter(CACHE_LOADER_PARAM)
.addParameter(boolean.class, "async")
.addStatement("super(builder, $L, async)", cacheLoader);
}
}
Loading

0 comments on commit 003be23

Please sign in to comment.