Defer branch visibility checks and reuse CodeOwnerResolver

In AbstractGetCodeOwnersForPath, filterCodeOwners previously checked
RefPermission.READ (isVisibleTo) on all candidate code owners across
the entire OWNERS hierarchy before scoring, sorting, and limiting the
results. Additionally, codeOwnerResolver.get() was called for each
visited OWNERS file, discarding the per-instance TransientCodeOwnerCache
between hierarchy levels.

Move getVisibleCodeOwners into sortAndLimit so branch visibility is
evaluated lazily on the score-sorted stream only until the requested
limit is reached, and reuse a single CodeOwnerResolver instance across
the hierarchy traversal and global code owner resolution.

Testing & Benchmark Results (https://paste.googleplex.com/6755399436516647)

Bug: Google b/360136905
Change-Id: I7cefffe9e66b16bdd0de308eec1d42b4e4b2d215
diff --git a/java/com/google/gerrit/plugins/codeowners/restapi/AbstractGetCodeOwnersForPath.java b/java/com/google/gerrit/plugins/codeowners/restapi/AbstractGetCodeOwnersForPath.java
index 9c293db..1ef672b 100644
--- a/java/com/google/gerrit/plugins/codeowners/restapi/AbstractGetCodeOwnersForPath.java
+++ b/java/com/google/gerrit/plugins/codeowners/restapi/AbstractGetCodeOwnersForPath.java
@@ -224,13 +224,14 @@
     ImmutableList.Builder<DebugMessage> debugLogsBuilder = ImmutableList.builder();
     ImmutableList.Builder<CodeOwnerConfigFileInfo> codeOwnerConfigFileInfosBuilder =
         ImmutableList.builder();
+    CodeOwnerResolver resolver = codeOwnerResolver.get();
     codeOwnerConfigHierarchy.visit(
         rsrc.getBranch(),
         rsrc.getRevision(),
         rsrc.getPath(),
         codeOwnerConfig -> {
           CodeOwnerResolverResult pathCodeOwners =
-              codeOwnerResolver.get().resolvePathCodeOwners(codeOwnerConfig, rsrc.getPath());
+              resolver.resolvePathCodeOwners(codeOwnerConfig, rsrc.getPath());
 
           codeOwnerConfigFileInfosBuilder.add(
               codeOwnerConfigFileJson.format(
@@ -283,7 +284,8 @@
         });
 
     if (!ownedByAllUsers.get()) {
-      CodeOwnerResolverResult globalCodeOwners = getGlobalCodeOwners(rsrc.getBranch().project());
+      CodeOwnerResolverResult globalCodeOwners =
+          getGlobalCodeOwners(resolver, rsrc.getBranch().project());
 
       debugLogsBuilder.add(DebugMessage.createMessage("resolve global code owners"));
       debugLogsBuilder.addAll(globalCodeOwners.messages());
@@ -325,17 +327,14 @@
     ImmutableMap<CodeOwner, Double> scoredCodeOwners =
         codeOwnerScorings.getScorings(filteredCodeOwners);
 
-    ImmutableList<CodeOwner> sortedAndLimitedCodeOwners = sortAndLimit(scoredCodeOwners);
+    ImmutableList<CodeOwner> sortedAndLimitedCodeOwners = sortAndLimit(rsrc, scoredCodeOwners);
 
-    if (highestScoreOnly) {
-      Optional<Double> highestScore =
-          scoredCodeOwners.values().stream().max(Comparator.naturalOrder());
-      if (highestScore.isPresent()) {
-        sortedAndLimitedCodeOwners =
-            sortedAndLimitedCodeOwners.stream()
-                .filter(codeOwner -> scoredCodeOwners.get(codeOwner).equals(highestScore.get()))
-                .collect(toImmutableList());
-      }
+    if (highestScoreOnly && !sortedAndLimitedCodeOwners.isEmpty()) {
+      Double highestScore = scoredCodeOwners.get(sortedAndLimitedCodeOwners.get(0));
+      sortedAndLimitedCodeOwners =
+          sortedAndLimitedCodeOwners.stream()
+              .filter(codeOwner -> scoredCodeOwners.get(codeOwner).equals(highestScore))
+              .collect(toImmutableList());
     }
     ImmutableMap<CodeOwner, ImmutableMap<CodeOwnerScore, Integer>> codeOwnerToScorings =
         sortedAndLimitedCodeOwners.stream()
@@ -368,12 +367,11 @@
     return CodeOwnerScorings.create(codeOwnerScorings.build());
   }
 
-  private CodeOwnerResolverResult getGlobalCodeOwners(Project.NameKey projectName) {
+  private CodeOwnerResolverResult getGlobalCodeOwners(
+      CodeOwnerResolver resolver, Project.NameKey projectName) {
     CodeOwnerResolverResult globalCodeOwners =
-        codeOwnerResolver
-            .get()
-            .resolve(
-                codeOwnersPluginConfiguration.getProjectConfig(projectName).getGlobalCodeOwners());
+        resolver.resolve(
+            codeOwnersPluginConfiguration.getProjectConfig(projectName).getGlobalCodeOwners());
     logger.atFine().log("including global code owners = %s", globalCodeOwners);
     return globalCodeOwners;
   }
@@ -392,28 +390,22 @@
   }
 
   /**
-   * Filters out code owners that should not be suggested.
+   * Filters out code owners that should not be suggested, as defined by subclasses.
    *
-   * <p>The following code owners are filtered out:
-   *
-   * <ul>
-   *   <li>code owners that cannot see the branch: Code owners that cannot see the branch cannot
-   *       approve paths in this branch. Hence returning them to the client is not useful.
-   *   <li>code owners that are service users: Requesting a code owner approval from a service user
-   *       normally doesn't make sense since they will not react to review requests.
-   * </ul>
+   * <p>Note that filtering out code owners that cannot see the branch is deferred to {@link
+   * #sortAndLimit} so that visibility checks are only performed until the limit is reached.
    */
   private ImmutableSet<CodeOwner> filterCodeOwners(
       R rsrc,
       ImmutableMultimap<CodeOwner, CodeOwnerAnnotation> annotations,
       ImmutableSet<CodeOwner> codeOwners,
       ImmutableList.Builder<DebugMessage> debugLogs) {
-    return filterCodeOwners(rsrc, annotations, getVisibleCodeOwners(rsrc, codeOwners), debugLogs)
+    return filterCodeOwners(rsrc, annotations, codeOwners.stream(), debugLogs)
         .collect(toImmutableSet());
   }
 
   /**
-   * To be overridden by subclasses to filter out additional code owners.
+   * To be overridden by subclasses to filter out code owners that should not be suggested.
    *
    * @param rsrc resource on which the request is being performed
    * @param annotations annotations that were set on the code owners
@@ -441,22 +433,27 @@
     return Optional.empty();
   }
 
-  private Stream<CodeOwner> getVisibleCodeOwners(R rsrc, ImmutableSet<CodeOwner> allCodeOwners) {
-    return allCodeOwners.stream()
-        .filter(
-            codeOwner -> {
-              if (isVisibleTo(rsrc, codeOwner)) {
-                return true;
-              }
-              logger.atFine().log(
-                  "Filtering out %s because this code owner cannot see the branch %s",
-                  codeOwner, rsrc.getBranch().branch());
-              return false;
-            });
+  /**
+   * Filters out code owners that cannot see the branch.
+   *
+   * <p>Code owners that cannot see the branch cannot approve paths in this branch. Hence returning
+   * them to the client is not useful.
+   */
+  private Stream<CodeOwner> getVisibleCodeOwners(R rsrc, Stream<CodeOwner> codeOwners) {
+    return codeOwners.filter(
+        codeOwner -> {
+          if (isVisibleTo(rsrc, codeOwner)) {
+            return true;
+          }
+          logger.atFine().log(
+              "Filtering out %s because this code owner cannot see the branch %s",
+              codeOwner, rsrc.getBranch().branch());
+          return false;
+        });
   }
 
   /** Whether the given resource is visible to the given code owner. */
-  private boolean isVisibleTo(R rsrc, CodeOwner codeOwner) {
+  protected boolean isVisibleTo(R rsrc, CodeOwner codeOwner) {
     // We always check for the visibility of the branch.
     // This is also correct for the GetCodeOwnersForPathInChange subclass where branch is the
     // destination branch of the change. For changes the intention of the visibility check is to
@@ -507,8 +504,15 @@
     return fillOptions;
   }
 
-  private ImmutableList<CodeOwner> sortAndLimit(ImmutableMap<CodeOwner, Double> scoredCodeOwners) {
-    return sortCodeOwners(seed, scoredCodeOwners).limit(limit).collect(toImmutableList());
+  /**
+   * Sorts the code owners by score, filters out code owners that cannot see the branch, and limits
+   * the result to {@link #limit}.
+   */
+  private ImmutableList<CodeOwner> sortAndLimit(
+      R rsrc, ImmutableMap<CodeOwner, Double> scoredCodeOwners) {
+    return getVisibleCodeOwners(rsrc, sortCodeOwners(seed, scoredCodeOwners))
+        .limit(limit)
+        .collect(toImmutableList());
   }
 
   /**
diff --git a/java/com/google/gerrit/plugins/codeowners/restapi/GetCodeOwnersForPathInChange.java b/java/com/google/gerrit/plugins/codeowners/restapi/GetCodeOwnersForPathInChange.java
index 2ee456d..00862bd 100644
--- a/java/com/google/gerrit/plugins/codeowners/restapi/GetCodeOwnersForPathInChange.java
+++ b/java/com/google/gerrit/plugins/codeowners/restapi/GetCodeOwnersForPathInChange.java
@@ -168,6 +168,17 @@
       // #{LAST_RESORT_SUGGESTION}.
       return filteredCodeOwners.stream();
     }
+
+    boolean lastResortOwnersWereDropped =
+        filteredCodeOwnersWithoutCodeOwnersThatAreAnnotatedWithLastResortSuggestion.size()
+            < filteredCodeOwners.size();
+    if (lastResortOwnersWereDropped
+        && filteredCodeOwnersWithoutCodeOwnersThatAreAnnotatedWithLastResortSuggestion.stream()
+            .noneMatch(codeOwner -> isVisibleTo(rsrc, codeOwner))) {
+      // None of the remaining code owners can see the branch, hence return code owners that are
+      // annotated with #{LAST_RESORT_SUGGESTION}.
+      return filteredCodeOwners.stream();
+    }
     return filteredCodeOwnersWithoutCodeOwnersThatAreAnnotatedWithLastResortSuggestion.stream();
   }
 
diff --git a/javatests/com/google/gerrit/plugins/codeowners/acceptance/api/GetCodeOwnersForPathInChangeIT.java b/javatests/com/google/gerrit/plugins/codeowners/acceptance/api/GetCodeOwnersForPathInChangeIT.java
index 18363d1..756053b 100644
--- a/javatests/com/google/gerrit/plugins/codeowners/acceptance/api/GetCodeOwnersForPathInChangeIT.java
+++ b/javatests/com/google/gerrit/plugins/codeowners/acceptance/api/GetCodeOwnersForPathInChangeIT.java
@@ -16,6 +16,7 @@
 
 import static com.google.common.truth.Truth.assertThat;
 import static com.google.common.truth.Truth.assertWithMessage;
+import static com.google.gerrit.acceptance.testsuite.project.TestProjectUpdate.block;
 import static com.google.gerrit.plugins.codeowners.testing.CodeOwnerInfoSubject.hasAccountId;
 import static com.google.gerrit.plugins.codeowners.testing.CodeOwnerInfoSubject.hasScoring;
 import static com.google.gerrit.plugins.codeowners.testing.CodeOwnersInfoSubject.assertThat;
@@ -33,6 +34,7 @@
 import com.google.gerrit.acceptance.testsuite.request.RequestScopeOperations;
 import com.google.gerrit.entities.Account;
 import com.google.gerrit.entities.AccountGroup;
+import com.google.gerrit.entities.Permission;
 import com.google.gerrit.extensions.restapi.BadRequestException;
 import com.google.gerrit.extensions.restapi.ResourceNotFoundException;
 import com.google.gerrit.extensions.restapi.RestApiException;
@@ -410,6 +412,15 @@
         .addMember(serviceUser.id())
         .update();
 
+    // Create a user that cannot see the destination branch.
+    TestAccount user2 = accountCreator.user2();
+    AccountGroup.UUID blockedGroupUuid = groupOperations.newGroup().addMember(user2.id()).create();
+    projectOperations
+        .project(project)
+        .forUpdate()
+        .add(block(Permission.READ).ref("refs/heads/master").group(blockedGroupUuid))
+        .update();
+
     codeOwnerConfigOperations
         .newCodeOwnerConfig()
         .project(project)
@@ -419,6 +430,7 @@
             CodeOwnerSet.builder()
                 .addCodeOwnerEmail(serviceUser.email())
                 .addCodeOwnerEmail(changeOwner.email())
+                .addCodeOwnerEmail(user2.email())
                 .addCodeOwnerEmail(admin.email())
                 .addAnnotation(
                     admin.email(), CodeOwnerAnnotations.LAST_RESORT_SUGGESTION_ANNOTATION)
@@ -427,9 +439,9 @@
                 .build())
         .create();
 
-    // Expectation: The service user and the change owner are filtered out. admin and user get
-    // suggested despite of the LAST_RESORT_SUGGESTION annotation since ignoring them would make
-    // the result empty.
+    // Expectation: The service user, the change owner, and user2 (who cannot see the branch) are
+    // filtered out. admin and user get suggested despite of the LAST_RESORT_SUGGESTION annotation
+    // since ignoring them would make the result empty.
     CodeOwnersInfo codeOwnersInfo = queryCodeOwners("foo/bar/baz.md");
     assertThat(codeOwnersInfo)
         .hasCodeOwnersThat()