Skip to content

Commit

Permalink
Make thread count mixin less aggressive
Browse files Browse the repository at this point in the history
Redirect the thread pool construction instead of overwriting
the method.
  • Loading branch information
Spottedleaf committed Aug 9, 2024
1 parent cd5b6c0 commit 13374ad
Showing 1 changed file with 12 additions and 38 deletions.
Original file line number Diff line number Diff line change
@@ -1,37 +1,23 @@
package ca.spottedleaf.moonrise.mixin.util_thread_counts;

import com.google.common.util.concurrent.MoreExecutors;
import net.minecraft.Util;
import net.minecraft.util.Mth;
import org.slf4j.Logger;
import org.spongepowered.asm.mixin.Final;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Overwrite;
import org.spongepowered.asm.mixin.Shadow;
import org.spongepowered.asm.mixin.Unique;
import java.util.concurrent.ExecutorService;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Redirect;
import java.util.concurrent.ForkJoinPool;
import java.util.concurrent.ForkJoinWorkerThread;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;

@Mixin(Util.class)
abstract class UtilMixin {

@Shadow
@Final
static Logger LOGGER;

@Shadow
private static int getMaxThreads() {
return 0;
}

@Shadow
private static void onThreadException(Thread thread, Throwable throwable) {

}

@Unique
private static int getThreadCounts(final int min, final int max) {
final int cpus = Runtime.getRuntime().availableProcessors() / 2;
Expand All @@ -55,29 +41,17 @@ private static int getThreadCounts(final int min, final int max) {
* not lost due to some timeout, as G1GC has some issues cleaning up those.
* @author Spottedleaf
*/
@Overwrite
public static ExecutorService makeExecutor(final String name) {
@Redirect(
method = "makeExecutor",
at = @At(
value = "NEW",
target = "(ILjava/util/concurrent/ForkJoinPool$ForkJoinWorkerThreadFactory;Ljava/lang/Thread$UncaughtExceptionHandler;Z)Ljava/util/concurrent/ForkJoinPool;"
)
)
private static ForkJoinPool modifyExecutor(final int parallelism, final ForkJoinPool.ForkJoinWorkerThreadFactory factory,
final Thread.UncaughtExceptionHandler handler, final boolean asyncMode) {
final int threads = getThreadCounts(1, getMaxThreads());
if (threads <= 0) {
return MoreExecutors.newDirectExecutorService();
}

final AtomicInteger workerCount = new AtomicInteger();

return new ForkJoinPool(threads, (forkJoinPool) -> {
ForkJoinWorkerThread forkJoinWorkerThread = new ForkJoinWorkerThread(forkJoinPool) {
protected void onTermination(final Throwable throwable) {
if (throwable != null) {
LOGGER.error(this.getName() + " died", throwable);
} else {
LOGGER.debug(this.getName() + " shutdown");
}

super.onTermination(throwable);
}
};
forkJoinWorkerThread.setName("Worker-" + name + "-" + workerCount.getAndIncrement());
return forkJoinWorkerThread;
}, UtilMixin::onThreadException, true, 0, Integer.MAX_VALUE, 1, null, 365, TimeUnit.DAYS);
return new ForkJoinPool(threads, factory, handler, asyncMode, 0, Integer.MAX_VALUE, 1, null, 365, TimeUnit.DAYS);
}
}

0 comments on commit 13374ad

Please sign in to comment.