Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace guava cache with caffeine #3796

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,11 @@

package com.google.errorprone.dataflow;

import com.github.benmanes.caffeine.cache.CacheLoader;
import com.github.benmanes.caffeine.cache.Caffeine;
import com.github.benmanes.caffeine.cache.LoadingCache;
import com.google.auto.value.AutoValue;
import com.google.common.base.Preconditions;
import com.google.common.cache.CacheBuilder;
import com.google.common.cache.CacheLoader;
import com.google.common.cache.LoadingCache;
import com.google.common.util.concurrent.UncheckedExecutionException;
import com.sun.source.tree.BlockTree;
import com.sun.source.tree.ClassTree;
import com.sun.source.tree.CompilationUnitTree;
Expand All @@ -46,6 +45,8 @@
import org.checkerframework.errorprone.dataflow.cfg.UnderlyingAST;
import org.checkerframework.errorprone.dataflow.cfg.builder.CFGBuilder;

import java.util.concurrent.CompletionException;

/**
* Provides a wrapper around {@link org.checkerframework.errorprone.dataflow.analysis.Analysis}.
*
Expand Down Expand Up @@ -73,7 +74,7 @@ public interface Result<
* TODO(b/158869538): Write a test that checks these assumptions
*/
private static final LoadingCache<AnalysisParams, Analysis<?, ?, ?>> analysisCache =
CacheBuilder.newBuilder()
Caffeine.newBuilder()
.build(
new CacheLoader<AnalysisParams, Analysis<?, ?, ?>>() {
@Override
Expand All @@ -89,7 +90,7 @@ public interface Result<
});

private static final LoadingCache<CfgParams, ControlFlowGraph> cfgCache =
CacheBuilder.newBuilder()
Caffeine.newBuilder()
.maximumSize(1)
.build(
new CacheLoader<CfgParams, ControlFlowGraph>() {
Expand Down Expand Up @@ -173,13 +174,13 @@ Result<A, S, T> methodDataflow(TreePath methodPath, Context context, T transfer)

ControlFlowGraph cfg;
try {
cfg = cfgCache.getUnchecked(CfgParams.create(methodPath, env));
} catch (UncheckedExecutionException e) {
cfg = cfgCache.get(CfgParams.create(methodPath, env));
} catch (CompletionException e) {
throw e.getCause() instanceof CompletionFailure ? (CompletionFailure) e.getCause() : e;
}
AnalysisParams aparams = AnalysisParams.create(transfer, cfg, env);
@SuppressWarnings("unchecked")
Analysis<A, S, T> analysis = (Analysis<A, S, T>) analysisCache.getUnchecked(aparams);
Analysis<A, S, T> analysis = (Analysis<A, S, T>) analysisCache.get(aparams);

return new Result<A, S, T>() {
@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,15 @@

import static com.google.common.base.Preconditions.checkArgument;

import com.github.benmanes.caffeine.cache.CacheLoader;
import com.github.benmanes.caffeine.cache.Caffeine;
import com.github.benmanes.caffeine.cache.LoadingCache;
import com.google.auto.value.AutoValue;
import com.google.common.cache.CacheBuilder;
import com.google.common.cache.CacheLoader;
import com.google.common.cache.LoadingCache;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Streams;
import com.google.common.graph.GraphBuilder;
import com.google.common.graph.MutableGraph;
import com.google.common.util.concurrent.UncheckedExecutionException;
import com.google.errorprone.dataflow.nullnesspropagation.Nullness;
import com.google.errorprone.dataflow.nullnesspropagation.NullnessAnnotations;
import com.sun.source.tree.ArrayAccessTree;
Expand Down Expand Up @@ -64,6 +63,7 @@
import java.util.Iterator;
import java.util.List;
import java.util.Optional;
import java.util.concurrent.CompletionException;
import java.util.function.Consumer;
import javax.annotation.Nullable;
import javax.lang.model.type.TypeVariable;
Expand All @@ -78,7 +78,7 @@
public class NullnessQualifierInference extends TreeScanner<Void, Void> {

private static final LoadingCache<Tree, InferredNullability> inferenceCache =
CacheBuilder.newBuilder()
Caffeine.newBuilder()
.maximumSize(1)
.build(
new CacheLoader<Tree, InferredNullability>() {
Expand All @@ -100,8 +100,8 @@ public static InferredNullability getInferredNullability(Tree methodOrInitialize
"Tree `%s` is not a lambda, initializer, or method.",
methodOrInitializerOrLambda);
try {
return inferenceCache.getUnchecked(methodOrInitializerOrLambda);
} catch (UncheckedExecutionException e) {
return inferenceCache.get(methodOrInitializerOrLambda);
} catch (CompletionException e) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@throws CompletionException if a checked exception was thrown while loading the value

CompletionFailure is a runtime exception, so I think that you can catch it directly?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks. We want the CompletionFailure to propagate, so I don't think we need to the catch here at all anymore

throw e.getCause() instanceof CompletionFailure ? (CompletionFailure) e.getCause() : e;
}
}
Expand Down