Speed-up the search for desugared classes in case of many lambdas by limiting the search to the directory where we expect the file.

--
PiperOrigin-RevId: 150049563
MOS_MIGRATED_REVID=150049563
diff --git a/src/tools/android/java/com/google/devtools/build/android/desugar/Desugar.java b/src/tools/android/java/com/google/devtools/build/android/desugar/Desugar.java
index 877d230..3ef5489 100644
--- a/src/tools/android/java/com/google/devtools/build/android/desugar/Desugar.java
+++ b/src/tools/android/java/com/google/devtools/build/android/desugar/Desugar.java
@@ -235,8 +235,7 @@
 
       // Write out the lambda classes we generated along the way
       for (Map.Entry<Path, LambdaInfo> lambdaClass : lambdas.drain().entrySet()) {
-        try (InputStream bytecode =
-            Files.newInputStream(dumpDirectory.resolve(lambdaClass.getKey()))) {
+        try (InputStream bytecode = Files.newInputStream(lambdaClass.getKey())) {
           ClassReader reader = rewriter.reader(bytecode);
           CoreLibraryRewriter.UnprefixingClassWriter writer =
               rewriter.writer(ClassWriter.COMPUTE_MAXS /*for invoking bridges*/);
diff --git a/src/tools/android/java/com/google/devtools/build/android/desugar/LambdaClassMaker.java b/src/tools/android/java/com/google/devtools/build/android/desugar/LambdaClassMaker.java
index d8f2e28..2a9f9be 100644
--- a/src/tools/android/java/com/google/devtools/build/android/desugar/LambdaClassMaker.java
+++ b/src/tools/android/java/com/google/devtools/build/android/desugar/LambdaClassMaker.java
@@ -54,7 +54,7 @@
   }
 
   /**
-   * Returns relative paths to .class files generated since the last call to this method together
+   * Returns absolute paths to .class files generated since the last call to this method together
    * with a string descriptor of the factory method.
    */
   public Map<Path, LambdaInfo> drain() {
@@ -68,12 +68,12 @@
     // will not contain '/' and searches will fail.  So, construct an absolute path from the given
     // string and use its string representation to find the file we need regardless of host
     // system's file system
-    final String rootPathPrefixStr = rootDirectory.resolve(pathPrefix).toString();
+    Path rootPathPrefix = rootDirectory.resolve(pathPrefix);
+    final String rootPathPrefixStr = rootPathPrefix.toString();
 
-    // TODO(kmb): Investigate making this faster in the case of many lambdas
     // TODO(bazel-team): This could be much nicer with lambdas
-    try (Stream<Path> results =
-        Files.walk(rootDirectory)
+    try (Stream<Path> paths =
+        Files.list(rootPathPrefix.getParent())
             .filter(
                 new Predicate<Path>() {
                   @Override
@@ -82,7 +82,7 @@
                         && !generatedClasses.containsKey(path);
                   }
                 })) {
-      return Iterators.getOnlyElement(results.iterator());
+      return Iterators.getOnlyElement(paths.iterator());
     }
   }
 }