original-brownbear commented on code in PR #13472:
URL: https://github.com/apache/lucene/pull/13472#discussion_r1642975132
##########
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:
This currently happens out of the box I think? The task id counter is
incremented right when starting a task. So if the worker pool is slow to get to
work, then the current thread will be first to run (probably true at least
every time there is any sort of queue).
--
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]