From d3501d8325e7ea054f06cd218fb3d182ca0ead13 Mon Sep 17 00:00:00 2001 From: Googler Date: Wed, 15 Aug 2018 13:39:01 -0700 Subject: Use ConcurrentHashMap instead of Cache where we aren't using any of the LoadingCache's capabilities. According to profiles, using ConcurrentHashMap is about 3x more efficient. RELNOTES: None PiperOrigin-RevId: 208874176 --- .../devtools/build/lib/skyframe/GlobFunction.java | 6 ++--- .../google/devtools/build/lib/vfs/UnixGlob.java | 27 +++++++--------------- 2 files changed, 10 insertions(+), 23 deletions(-) diff --git a/src/main/java/com/google/devtools/build/lib/skyframe/GlobFunction.java b/src/main/java/com/google/devtools/build/lib/skyframe/GlobFunction.java index 9d2c94ac8f..5820ac8e70 100644 --- a/src/main/java/com/google/devtools/build/lib/skyframe/GlobFunction.java +++ b/src/main/java/com/google/devtools/build/lib/skyframe/GlobFunction.java @@ -14,8 +14,6 @@ package com.google.devtools.build.lib.skyframe; import com.google.common.base.Preconditions; -import com.google.common.cache.Cache; -import com.google.common.cache.CacheBuilder; import com.google.common.collect.Maps; import com.google.common.collect.Sets; import com.google.devtools.build.lib.actions.FileValue; @@ -33,6 +31,7 @@ import com.google.devtools.build.skyframe.SkyFunctionException.Transience; import com.google.devtools.build.skyframe.SkyKey; import com.google.devtools.build.skyframe.SkyValue; import java.util.Map; +import java.util.concurrent.ConcurrentHashMap; import java.util.regex.Pattern; import javax.annotation.Nullable; @@ -43,8 +42,7 @@ import javax.annotation.Nullable; */ public final class GlobFunction implements SkyFunction { - private final Cache regexPatternCache = - CacheBuilder.newBuilder().maximumSize(10000).concurrencyLevel(4).build(); + private final ConcurrentHashMap regexPatternCache = new ConcurrentHashMap<>(); private final boolean alwaysUseDirListing; diff --git a/src/main/java/com/google/devtools/build/lib/vfs/UnixGlob.java b/src/main/java/com/google/devtools/build/lib/vfs/UnixGlob.java index ab421f218c..36273a2cc8 100644 --- a/src/main/java/com/google/devtools/build/lib/vfs/UnixGlob.java +++ b/src/main/java/com/google/devtools/build/lib/vfs/UnixGlob.java @@ -21,9 +21,6 @@ import com.google.common.base.Predicate; import com.google.common.base.Predicates; import com.google.common.base.Splitter; import com.google.common.base.Throwables; -import com.google.common.cache.Cache; -import com.google.common.cache.CacheBuilder; -import com.google.common.cache.CacheLoader; import com.google.common.collect.ImmutableList; import com.google.common.collect.Iterables; import com.google.common.collect.Lists; @@ -42,6 +39,7 @@ import java.util.Collections; import java.util.List; import java.util.Objects; import java.util.Set; +import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ExecutionException; import java.util.concurrent.Future; import java.util.concurrent.ThreadPoolExecutor; @@ -148,7 +146,7 @@ public final class UnixGlob { } /** - * Calls {@link #matches(String, String, Cache) matches(pattern, str, null)} + * Calls {@link #matches(String, String, ConcurrentHashMap) matches(pattern, str, null)} */ public static boolean matches(String pattern, String str) { return matches(pattern, str, null); @@ -164,7 +162,7 @@ public final class UnixGlob { * {@code null} to skip caching */ public static boolean matches(String pattern, String str, - Cache patternCache) { + ConcurrentHashMap patternCache) { if (pattern.length() == 0 || str.length() == 0) { return false; } @@ -196,13 +194,10 @@ public final class UnixGlob { return str.startsWith(pattern.substring(0, lastIndex)); } - Pattern regex = patternCache == null ? null : patternCache.getIfPresent(pattern); - if (regex == null) { - regex = makePatternFromWildcard(pattern); - if (patternCache != null) { - patternCache.put(pattern, regex); - } - } + Pattern regex = + patternCache == null + ? makePatternFromWildcard(pattern) + : patternCache.computeIfAbsent(pattern, p -> makePatternFromWildcard(p)); return regex.matcher(str).matches(); } @@ -477,13 +472,7 @@ public final class UnixGlob { private static final class GlobVisitor { // These collections are used across workers and must therefore be thread-safe. private final Collection results = Sets.newConcurrentHashSet(); - private final Cache cache = CacheBuilder.newBuilder().build( - new CacheLoader() { - @Override - public Pattern load(String wildcard) { - return makePatternFromWildcard(wildcard); - } - }); + private final ConcurrentHashMap cache = new ConcurrentHashMap<>(); private final GlobFuture result; private final ThreadPoolExecutor executor; -- cgit v1.2.3