Make thread count mixin less aggressive

Redirect the thread pool construction instead of overwriting
the method.
This commit is contained in:
Spottedleaf
2024-08-08 21:28:39 -07:00
parent cd5b6c037b
commit 13374ad4a0

View File

@@ -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;
@@ -55,29 +41,17 @@ abstract class UtilMixin {
* 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);
}
}