Skip to content

Commit

Permalink
Replace Guava Cache with Caffeine in several classes.
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 380657997
  • Loading branch information
seandc authored and copybara-github committed Jun 21, 2021
1 parent c140625 commit b158a0f
Show file tree
Hide file tree
Showing 18 changed files with 93 additions and 91 deletions.
1 change: 1 addition & 0 deletions src/main/java/com/google/devtools/build/lib/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ java_library(
"//src/main/java/com/google/devtools/build/lib/util:resource_converter",
"//src/main/java/com/google/devtools/build/lib/vfs:pathfragment",
"//src/main/java/com/google/devtools/common/options",
"//third_party:caffeine",
"//third_party:flogger",
"//third_party:guava",
"//third_party:jsr305",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,16 @@
// limitations under the License.
package com.google.devtools.build.lib.buildtool;

import com.github.benmanes.caffeine.cache.CaffeineSpec;
import com.google.common.annotations.VisibleForTesting;
import com.google.common.cache.CacheBuilderSpec;
import com.google.common.flogger.GoogleLogger;
import com.google.devtools.build.lib.actions.LocalHostCapacity;
import com.google.devtools.build.lib.util.OptionsUtils;
import com.google.devtools.build.lib.util.ResourceConverter;
import com.google.devtools.build.lib.vfs.PathFragment;
import com.google.devtools.common.options.BoolOrEnumConverter;
import com.google.devtools.common.options.Converters;
import com.google.devtools.common.options.Converters.CacheBuilderSpecConverter;
import com.google.devtools.common.options.Converters.CaffeineSpecConverter;
import com.google.devtools.common.options.Converters.RangeConverter;
import com.google.devtools.common.options.Option;
import com.google.devtools.common.options.OptionDefinition;
Expand Down Expand Up @@ -283,11 +283,11 @@ public class BuildRequestOptions extends OptionsBase {
defaultValue = "maximumSize=100000",
documentationCategory = OptionDocumentationCategory.UNDOCUMENTED,
effectTags = {OptionEffectTag.EXECUTION},
converter = CacheBuilderSpecConverter.class,
converter = CaffeineSpecConverter.class,
help =
"Describes the cache used to store known regular directories as they're created. Parent"
+ " directories of output files are created on-demand during action execution.")
public CacheBuilderSpec directoryCreationCacheSpec;
public CaffeineSpec directoryCreationCacheSpec;

@Option(
name = "aspects",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@
// limitations under the License.
package com.google.devtools.build.lib.skyframe;

import com.google.common.cache.CacheBuilder;
import com.google.common.cache.CacheBuilderSpec;
import com.github.benmanes.caffeine.cache.Caffeine;
import com.github.benmanes.caffeine.cache.CaffeineSpec;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Lists;
import com.google.common.util.concurrent.Striped;
Expand Down Expand Up @@ -51,10 +51,10 @@ private enum DirectoryState {
CREATED
}

ActionOutputDirectoryHelper(CacheBuilderSpec cacheBuilderSpec) {
ActionOutputDirectoryHelper(CaffeineSpec cacheBuilderSpec) {
knownDirectories =
CacheBuilder.from(cacheBuilderSpec)
.concurrencyLevel(Runtime.getRuntime().availableProcessors())
Caffeine.from(cacheBuilderSpec)
.initialCapacity(Runtime.getRuntime().availableProcessors())
.<PathFragment, DirectoryState>build()
.asMap();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,8 @@
// limitations under the License.
package com.google.devtools.build.lib.skyframe;

import com.google.common.cache.CacheBuilder;
import com.google.common.cache.CacheLoader;
import com.google.common.cache.LoadingCache;
import com.github.benmanes.caffeine.cache.Caffeine;
import com.github.benmanes.caffeine.cache.LoadingCache;
import com.google.devtools.build.lib.actions.Action;
import com.google.devtools.build.lib.actions.ActionKeyContext;
import com.google.devtools.build.lib.actions.ActionLookupData;
Expand Down Expand Up @@ -59,10 +58,10 @@ public static SketchKey key(ActionLookupData key) {
@AutoCodec
static class SketchKey extends AbstractSkyKey<ActionLookupData> {
private static final LoadingCache<ActionLookupData, SketchKey> keyCache =
CacheBuilder.newBuilder()
Caffeine.newBuilder()
.weakKeys()
.concurrencyLevel(BlazeInterners.concurrencyLevel())
.build(CacheLoader.from(SketchKey::new));
.initialCapacity(BlazeInterners.concurrencyLevel())
.build(SketchKey::new);

private SketchKey(ActionLookupData arg) {
super(arg);
Expand All @@ -71,7 +70,7 @@ private SketchKey(ActionLookupData arg) {
@AutoCodec.VisibleForSerialization
@AutoCodec.Instantiator
static SketchKey create(ActionLookupData arg) {
return keyCache.getUnchecked(arg);
return keyCache.get(arg);
}

@Override
Expand Down
5 changes: 3 additions & 2 deletions src/main/java/com/google/devtools/build/lib/skyframe/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -303,7 +303,6 @@ java_library(
"//src/main/java/com/google/devtools/build/lib/rules:repository/workspace_file_helper",
"//src/main/java/com/google/devtools/build/lib/rules/cpp:cpp_interface",
"//src/main/java/com/google/devtools/build/lib/skyframe/serialization/autocodec",
"//src/main/java/com/google/devtools/build/lib/skyframe/serialization/autocodec:serialization-constant",
"//src/main/java/com/google/devtools/build/lib/util",
"//src/main/java/com/google/devtools/build/lib/util:TestType",
"//src/main/java/com/google/devtools/build/lib/util:abrupt_exit_exception",
Expand All @@ -324,6 +323,7 @@ java_library(
"//src/main/protobuf:analysis_v2_java_proto",
"//src/main/protobuf:failure_details_java_proto",
"//third_party:auto_value",
"//third_party:caffeine",
"//third_party:flogger",
"//third_party:guava",
"//third_party:jsr305",
Expand Down Expand Up @@ -518,7 +518,7 @@ java_library(
"//src/main/java/com/google/devtools/build/lib/util",
"//src/main/java/com/google/devtools/build/skyframe",
"//src/main/java/com/google/devtools/build/skyframe:skyframe-objects",
"//third_party:guava",
"//third_party:caffeine",
"//third_party:jsr305",
],
)
Expand Down Expand Up @@ -978,6 +978,7 @@ java_library(
"//src/main/java/com/google/devtools/build/lib/vfs:pathfragment",
"//src/main/java/com/google/devtools/build/skyframe:skyframe-objects",
"//src/main/java/com/google/devtools/common/options",
"//third_party:caffeine",
"//third_party:guava",
"//third_party:jsr305",
],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,10 @@
// limitations under the License.
package com.google.devtools.build.lib.skyframe;

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.Throwables;
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.ImmutableSortedMap;
import com.google.devtools.build.lib.actions.ActionEnvironment;
Expand All @@ -37,7 +36,7 @@
import com.google.devtools.build.skyframe.SkyKey;
import com.google.devtools.build.skyframe.SkyValue;
import java.lang.reflect.InvocationTargetException;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.CompletionException;
import net.starlark.java.eval.StarlarkSemantics;

/** A builder for {@link BuildConfigurationValue} instances. */
Expand All @@ -49,15 +48,7 @@ public final class BuildConfigurationFunction implements SkyFunction {
private final BlazeDirectories directories;
private final ConfiguredRuleClassProvider ruleClassProvider;
private final LoadingCache<FragmentKey, Fragment> fragmentCache =
CacheBuilder.newBuilder()
.weakValues()
.build(
new CacheLoader<FragmentKey, Fragment>() {
@Override
public Fragment load(FragmentKey key) throws InvalidConfigurationException {
return makeFragment(key);
}
});
Caffeine.newBuilder().weakValues().build(BuildConfigurationFunction::makeFragment);

public BuildConfigurationFunction(
BlazeDirectories directories, RuleClassProvider ruleClassProvider) {
Expand Down Expand Up @@ -128,9 +119,9 @@ private ImmutableSortedMap<Class<? extends Fragment>, Fragment> getConfiguration
FragmentKey fragmentKey = FragmentKey.create(trimmedOptions, fragmentClass);
try {
fragment = fragmentCache.get(fragmentKey);
} catch (ExecutionException e) {
} catch (CompletionException e) {
Throwables.propagateIfPossible(e.getCause(), InvalidConfigurationException.class);
throw new IllegalStateException(e);
throw e;
}
if (fragment != NULL_MARKER) {
fragments.put(fragmentClass, fragment);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@
// limitations under the License.
package com.google.devtools.build.lib.skyframe;

import com.github.benmanes.caffeine.cache.Cache;
import com.github.benmanes.caffeine.cache.Caffeine;
import com.google.common.base.Preconditions;
import com.google.common.base.Predicates;
import com.google.common.cache.Cache;
import com.google.common.cache.CacheBuilder;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Lists;
Expand Down Expand Up @@ -1204,8 +1204,8 @@ private void reset() {
Preconditions.checkState(
cacheSize >= 0, "Expected positive Starlark cache size if caching. %s", cacheSize);
cache =
CacheBuilder.newBuilder()
.concurrencyLevel(BlazeInterners.concurrencyLevel())
Caffeine.newBuilder()
.initialCapacity(BlazeInterners.concurrencyLevel())
.maximumSize(cacheSize)
.recordStats()
.build();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@
import static com.google.common.base.Preconditions.checkNotNull;
import static com.google.common.base.Preconditions.checkState;

import com.github.benmanes.caffeine.cache.Cache;
import com.google.common.base.Throwables;
import com.google.common.cache.Cache;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,15 @@
import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.base.Preconditions.checkNotNull;

import com.github.benmanes.caffeine.cache.Caffeine;
import com.github.benmanes.caffeine.cache.LoadingCache;
import com.google.common.base.MoreObjects;
import com.google.common.base.Throwables;
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.ImmutableMap;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Interner;
import com.google.common.collect.Iterables;
import com.google.common.util.concurrent.UncheckedExecutionException;
import com.google.devtools.build.lib.analysis.PlatformOptions;
import com.google.devtools.build.lib.analysis.config.BuildOptions;
import com.google.devtools.build.lib.analysis.config.FragmentOptions;
Expand All @@ -45,7 +43,7 @@
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.CompletionException;
import javax.annotation.Nullable;

/**
Expand Down Expand Up @@ -162,27 +160,10 @@ public String toString() {
this.flagsToPlatforms = checkNotNull(flagsToPlatforms);
this.optionsClasses = checkNotNull(optionsClasses);
this.parserCache =
CacheBuilder.newBuilder()
Caffeine.newBuilder()
.initialCapacity(platformsToFlags.size() + flagsToPlatforms.size())
.build(
new CacheLoader<ImmutableSet<String>, OptionsParsingResult>() {
@Override
public OptionsParsingResult load(ImmutableSet<String> args)
throws OptionsParsingException {
return parse(args);
}
});
this.mappingCache =
CacheBuilder.newBuilder()
.weakKeys()
.build(
new CacheLoader<BuildConfigurationValue.Key, BuildConfigurationValue.Key>() {
@Override
public BuildConfigurationValue.Key load(BuildConfigurationValue.Key original)
throws OptionsParsingException {
return computeMapping(original);
}
});
.build(this::parse);
this.mappingCache = Caffeine.newBuilder().weakKeys().build(this::computeMapping);
}

/**
Expand All @@ -209,9 +190,9 @@ public BuildConfigurationValue.Key map(BuildConfigurationValue.Key original)
throws OptionsParsingException {
try {
return mappingCache.get(original);
} catch (ExecutionException | UncheckedExecutionException e) {
} catch (CompletionException e) {
Throwables.propagateIfPossible(e.getCause(), OptionsParsingException.class);
throw new IllegalStateException(e);
throw e;
}
}

Expand Down Expand Up @@ -265,9 +246,9 @@ private OptionsParsingResult parseWithCache(ImmutableSet<String> args)
throws OptionsParsingException {
try {
return parserCache.get(args);
} catch (ExecutionException e) {
} catch (CompletionException e) {
Throwables.propagateIfPossible(e.getCause(), OptionsParsingException.class);
throw new IllegalStateException(e);
throw e;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,14 @@
import static com.google.devtools.build.lib.skyframe.ArtifactConflictFinder.ACTION_CONFLICTS;
import static com.google.devtools.build.lib.skyframe.ArtifactConflictFinder.NUM_JOBS;

import com.github.benmanes.caffeine.cache.Cache;
import com.github.benmanes.caffeine.cache.Caffeine;
import com.google.common.annotations.VisibleForTesting;
import com.google.common.base.Preconditions;
import com.google.common.base.Predicate;
import com.google.common.base.Predicates;
import com.google.common.base.Stopwatch;
import com.google.common.base.Throwables;
import com.google.common.cache.Cache;
import com.google.common.cache.CacheBuilder;
import com.google.common.collect.ArrayListMultimap;
import com.google.common.collect.Collections2;
import com.google.common.collect.ImmutableCollection;
Expand Down Expand Up @@ -297,7 +297,7 @@ public abstract class SkyframeExecutor implements WalkableGraphFactory, Configur
// Cache of parsed bzl files, for use when we're inlining BzlCompileFunction in
// BzlLoadFunction. See the comments in BzlLoadFunction for motivations and details.
private final Cache<BzlCompileValue.Key, BzlCompileValue> bzlCompileCache =
CacheBuilder.newBuilder().build();
Caffeine.newBuilder().build();

private final AtomicInteger numPackagesLoaded = new AtomicInteger(0);
@Nullable private final PackageProgressReceiver packageProgress;
Expand Down Expand Up @@ -1150,12 +1150,12 @@ public void injectExtraPrecomputedValues(List<PrecomputedValue.Injected> extraPr
}

protected Cache<PackageIdentifier, LoadedPackageCacheEntry> newPkgFunctionCache() {
return CacheBuilder.newBuilder().build();
return Caffeine.newBuilder().build();
}

protected Cache<PackageIdentifier, PackageFunction.CompiledBuildFile>
newCompiledBuildFileCache() {
return CacheBuilder.newBuilder().build();
return Caffeine.newBuilder().build();
}

private void setShowLoadingProgress(boolean showLoadingProgressValue) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@

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

import com.github.benmanes.caffeine.cache.Cache;
import com.github.benmanes.caffeine.cache.Caffeine;
import com.google.common.annotations.VisibleForTesting;
import com.google.common.base.Preconditions;
import com.google.common.base.Suppliers;
import com.google.common.cache.Cache;
import com.google.common.cache.CacheBuilder;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet;
Expand Down Expand Up @@ -440,7 +440,7 @@ private ImmutableMap<SkyFunctionName, SkyFunction> makeFreshSkyFunctions() {
AtomicReference<TimestampGranularityMonitor> tsgm =
new AtomicReference<>(new TimestampGranularityMonitor(BlazeClock.instance()));
Cache<PackageIdentifier, LoadedPackageCacheEntry> packageFunctionCache =
CacheBuilder.newBuilder().build();
Caffeine.newBuilder().build();
AtomicReference<FilesystemCalls> syscallCacheRef =
new AtomicReference<>(
PerBuildSyscallCache.newBuilder()
Expand Down Expand Up @@ -485,7 +485,7 @@ public Path getBuildFileForPackage(PackageIdentifier packageName) {
.put(
SkyFunctions.BZL_LOAD,
BzlLoadFunction.create(
pkgFactory, directories, hashFunction, CacheBuilder.newBuilder().build()))
pkgFactory, directories, hashFunction, Caffeine.newBuilder().build()))
.put(SkyFunctions.WORKSPACE_NAME, new WorkspaceNameFunction())
.put(
WorkspaceFileValue.WORKSPACE_FILE,
Expand All @@ -500,7 +500,7 @@ public Path getBuildFileForPackage(PackageIdentifier packageName) {
cachingPackageLocator,
/*showLoadingProgress=*/ new AtomicBoolean(false),
packageFunctionCache,
/*compiledBuildFileCache=*/ CacheBuilder.newBuilder().build(),
/*compiledBuildFileCache=*/ Caffeine.newBuilder().build(),
/*numPackagesLoaded=*/ new AtomicInteger(0),
/*bzlLoadFunctionForInlining=*/ null,
/*packageProgress=*/ null,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ java_library(
"//src/main/java/com/google/devtools/build/skyframe",
"//src/main/java/com/google/devtools/build/skyframe:skyframe-objects",
"//src/main/java/net/starlark/java/eval",
"//src/main/java/net/starlark/java/syntax",
"//third_party:caffeine",
"//third_party:guava",
"//third_party:jsr305",
],
Expand Down
Loading

0 comments on commit b158a0f

Please sign in to comment.