ethbak commented on code in PR #16778:
URL: https://github.com/apache/lucene/pull/16778#discussion_r4667747098


##########
lucene/core/src/java/org/apache/lucene/util/hnsw/HnswConcurrentMergeBuilder.java:
##########
@@ -241,15 +242,45 @@ public OnHeapHnswGraph build(int maxOrd) throws 
IOException {
     if (plans.length > 0) {
       joinGraphs(plans, maxOrd, cumulativeWorkTimeNs);
     }
-    List<Callable<Void>> futures = new ArrayList<>();
-    for (int i = 0; i < workers.length; i++) {
+
+    // Reserve first worker for calling thread: only create Futures for 
remaining workers
+    List<Callable<Void>> tasks = new ArrayList<>(workers.length - 1);
+    for (int i = 1; i < workers.length; i++) {
       int finalI = i;
-      futures.add(() -> {
+      tasks.add(() -> {
         workers[finalI].run(maxOrd);
         return null;
       });
     }
-    taskExecutor.invokeAll(futures);
+
+    final List<Future<Void>> started = new ArrayList<>(workers.length);
+    try {
+      int start = workers[0].getStartPos(maxOrd);
+      int end;
+      while (start != -1) {
+        // Use this thread for the first worker
+        end = start + workers[0].batchSize;
+        if (end >= maxOrd) {
+          end = maxOrd; // No need to recruit more workers if this is the last 
batch
+        } else {
+          // Recruit other threads for workers that haven't started yet
+          while (started.size() < tasks.size()) {
+            var maybeFuture = 
taskExecutor.tryInvoke(tasks.get(started.size()));
+            if (maybeFuture.isEmpty()) {
+              break; // No room for more workers; break to process batch on 
this thread
+            }
+
+            started.add(maybeFuture.get());
+          }
+        }
+
+        workers[0].addVectors(start, end);
+        start = workers[0].getStartPos(maxOrd);
+      }
+    } finally {
+      TaskExecutor.collectResults(started);

Review Comment:
   It seems to me that when `mergeExec` queues, `tryInvoke` returns a future 
for every worker even though none has started, so this blocks until each one 
gets a thread
   
   You could consider cancelling the futures that haven't started once worker 0 
runs out of work, the way `TaskExecutor.Task#cancel` completes an unstarted 
task, before collecting?



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to