jpountz commented on code in PR #13472: URL: https://github.com/apache/lucene/pull/13472#discussion_r1642954682
########## lucene/core/src/java/org/apache/lucene/search/TaskExecutor.java: ########## @@ -144,32 +128,45 @@ public boolean cancel(boolean mayInterruptIfRunning) { } List<T> invokeAll(Executor executor) throws IOException { - boolean runOnCallerThread = numberOfRunningTasksInCurrentThread.get() > 0; - for (Runnable runnable : futures) { - if (runOnCallerThread) { - runnable.run(); - } else { - executor.execute(runnable); + final int count = futures.size(); + // taskId provides the first index of an un-executed task in #futures + final AtomicInteger taskId = new AtomicInteger(0); + // we fork execution count - 1 tasks to execute at least one task on the current thread to + // minimize needless forking and blocking of the current thread + if (count > 1) { + final Runnable work = + () -> { + int id = taskId.getAndIncrement(); + if (id < count) { + futures.get(id).run(); + } + }; + for (int j = 0; j < count - 1; j++) { Review Comment: Thinking out loud: if we think that the task that runs in the current thread is likely to run first, it may make sense to run the first task in the current thread rather than the count-th task, to better work for users who organize their segments in a specific order: https://lucene.apache.org/core/9_11_0/core/org/apache/lucene/index/DirectoryReader.html#open(org.apache.lucene.index.IndexCommit,int,java.util.Comparator). -- 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: issues-unsubscr...@lucene.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org --------------------------------------------------------------------- To unsubscribe, e-mail: issues-unsubscr...@lucene.apache.org For additional commands, e-mail: issues-h...@lucene.apache.org