Expand exclude patterns in glob()s using the glob result instead of the file
system. Using actual file operations could be beneficial in theory as the glob
might return a large result that we have to linearly scan through it, whereas we
could just remove a handful of files found by a handful of stats.

However, measurements indicate that this is not beneficial in practice and
that the additional file systems stats are more costly.

RELNOTES: None.
PiperOrigin-RevId: 234870066
diff --git a/src/main/java/com/google/devtools/build/lib/packages/GlobCache.java b/src/main/java/com/google/devtools/build/lib/packages/GlobCache.java
index 31b36ab..a968c4e 100644
--- a/src/main/java/com/google/devtools/build/lib/packages/GlobCache.java
+++ b/src/main/java/com/google/devtools/build/lib/packages/GlobCache.java
@@ -17,7 +17,6 @@
 import com.google.common.base.Preconditions;
 import com.google.common.base.Predicate;
 import com.google.common.base.Throwables;
-import com.google.common.collect.Iterables;
 import com.google.common.collect.Lists;
 import com.google.common.util.concurrent.SettableFuture;
 import com.google.devtools.build.lib.cmdline.PackageIdentifier;
@@ -241,22 +240,17 @@
     // Start globbing all patterns in parallel. The getGlob() calls below will
     // block on an individual pattern's results, but the other globs can
     // continue in the background.
-    for (String pattern : Iterables.concat(includes, excludes)) {
+    for (String pattern : includes) {
       @SuppressWarnings("unused") 
       Future<?> possiblyIgnoredError = getGlobUnsortedAsync(pattern, excludeDirs);
     }
 
     HashSet<String> results = new HashSet<>();
+    Preconditions.checkState(!results.contains(null), "glob returned null");
     for (String pattern : includes) {
       results.addAll(getGlobUnsorted(pattern, excludeDirs));
     }
-    for (String pattern : excludes) {
-      for (String excludeMatch : getGlobUnsorted(pattern, excludeDirs)) {
-        results.remove(excludeMatch);
-      }
-    }
-
-    Preconditions.checkState(!results.contains(null), "glob returned null");
+    UnixGlob.removeExcludes(results, excludes);
     return new ArrayList<>(results);
   }