Skip to content

Commit

Permalink
Replace guava cache with caffeine
Browse files Browse the repository at this point in the history
Resolves #3759

Fixes #3796

COPYBARA_INTEGRATE_REVIEW=#3796 from Sineaggi:fixes fffb316
PiperOrigin-RevId: 512769533
  • Loading branch information
Sineaggi authored and Error Prone Team committed Feb 28, 2023
1 parent 8bf9a19 commit 70942e0
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 25 deletions.
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 @@ -31,7 +30,6 @@
import com.sun.source.tree.Tree;
import com.sun.source.tree.VariableTree;
import com.sun.source.util.TreePath;
import com.sun.tools.javac.code.Symbol.CompletionFailure;
import com.sun.tools.javac.processing.JavacProcessingEnvironment;
import com.sun.tools.javac.util.Context;
import javax.annotation.Nullable;
Expand Down Expand Up @@ -73,7 +71,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 +87,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 @@ -171,15 +169,10 @@ private static <T> TreePath findEnclosingMethodOrLambdaOrInitializer(TreePath pa
Result<A, S, T> methodDataflow(TreePath methodPath, Context context, T transfer) {
ProcessingEnvironment env = JavacProcessingEnvironment.instance(context);

ControlFlowGraph cfg;
try {
cfg = cfgCache.getUnchecked(CfgParams.create(methodPath, env));
} catch (UncheckedExecutionException e) {
throw e.getCause() instanceof CompletionFailure ? (CompletionFailure) e.getCause() : e;
}
ControlFlowGraph cfg = cfgCache.get(CfgParams.create(methodPath, env));
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 All @@ -47,7 +46,6 @@
import com.sun.source.tree.VariableTree;
import com.sun.source.util.TreeScanner;
import com.sun.tools.javac.code.Symbol;
import com.sun.tools.javac.code.Symbol.CompletionFailure;
import com.sun.tools.javac.code.Symbol.MethodSymbol;
import com.sun.tools.javac.code.Symbol.TypeVariableSymbol;
import com.sun.tools.javac.code.Symbol.VarSymbol;
Expand Down Expand Up @@ -78,7 +76,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 @@ -99,11 +97,7 @@ public static InferredNullability getInferredNullability(Tree methodOrInitialize
|| methodOrInitializerOrLambda instanceof VariableTree,
"Tree `%s` is not a lambda, initializer, or method.",
methodOrInitializerOrLambda);
try {
return inferenceCache.getUnchecked(methodOrInitializerOrLambda);
} catch (UncheckedExecutionException e) {
throw e.getCause() instanceof CompletionFailure ? (CompletionFailure) e.getCause() : e;
}
return inferenceCache.get(methodOrInitializerOrLambda);
}

/**
Expand Down

0 comments on commit 70942e0

Please sign in to comment.