From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001 From: Taiyou06 Date: Sun, 6 Apr 2025 11:22:35 +0200 Subject: [PATCH] More virtual threads diff --git a/net/minecraft/Util.java b/net/minecraft/Util.java index b097f685e826e70008e3a096ee5f1d4fccf25680..aa79e95dc93927ce224c02e4382c7246cb48d51c 100644 --- a/net/minecraft/Util.java +++ b/net/minecraft/Util.java @@ -97,7 +97,8 @@ public class Util { public static final TracingExecutor DIMENSION_DATA_IO_POOL = makeExtraIoExecutor("Dimension-Data-IO-Worker-"); // Paper - Separate dimension data IO pool private static final TracingExecutor DOWNLOAD_POOL = makeIoExecutor("Download-", true); // Paper start - don't submit BLOCKING PROFILE LOOKUPS to the world gen thread - public static final ExecutorService PROFILE_EXECUTOR = Executors.newFixedThreadPool(2, new java.util.concurrent.ThreadFactory() { + // Leaf start - More virtual threads + public static final ExecutorService PROFILE_EXECUTOR = createProfileExecutor(); /* new Executors.newFixedThreadPool(2, new java.util.concurrent.ThreadFactory() { private final AtomicInteger count = new AtomicInteger(); @@ -110,7 +111,30 @@ public class Util { }); return ret; } - }); + }); */ + + private static ExecutorService createProfileExecutor() { + final java.util.concurrent.ThreadFactory factory; + if (org.dreeam.leaf.config.modules.opt.VT4ProfileExecutor.enabled && org.galemc.gale.virtualthread.VirtualThreadService.isSupported()) { + factory = org.galemc.gale.virtualthread.VirtualThreadService.get().createFactory(); + } else { + factory = new java.util.concurrent.ThreadFactory() { + private final AtomicInteger count = new AtomicInteger(); + + @Override + public Thread newThread(Runnable run) { + Thread ret = new Thread(run); + ret.setName("Profile Lookup Executor #" + this.count.getAndIncrement()); + ret.setUncaughtExceptionHandler((Thread thread, Throwable throwable) -> { + LOGGER.error("Uncaught exception in thread " + thread.getName(), throwable); + }); + return ret; + } + }; + } + return Executors.newFixedThreadPool(2, factory); + } + // Leaf end - More virtual threads // Paper end - don't submit BLOCKING PROFILE LOOKUPS to the world gen thread private static final DateTimeFormatter FILENAME_DATE_TIME_FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd_HH.mm.ss", Locale.ROOT); public static final int LINEAR_LOOKUP_THRESHOLD = 8; @@ -254,16 +278,31 @@ public class Util { } private static TracingExecutor makeIoExecutor(String name, boolean daemon) { - AtomicInteger atomicInteger = new AtomicInteger(1); - return new TracingExecutor(Executors.newCachedThreadPool(runnable -> { - Thread thread = new Thread(runnable); - String string = name + atomicInteger.getAndIncrement(); - TracyClient.setThreadName(string, name.hashCode()); - thread.setName(string); - thread.setDaemon(daemon); - thread.setUncaughtExceptionHandler(Util::onThreadException); - return thread; - })); + // Leaf start - More virtual threads + final java.util.concurrent.ThreadFactory factory; + final boolean useVirtualThreads; // Gale - virtual thread support + if (name.startsWith("Download-")) { // Gale - virtual thread support + useVirtualThreads = org.dreeam.leaf.config.modules.opt.VT4DownloadPool.enabled && org.galemc.gale.virtualthread.VirtualThreadService.isSupported(); // Gale - virtual thread support + } else { + useVirtualThreads = false; + } + + if (useVirtualThreads) { + factory = org.galemc.gale.virtualthread.VirtualThreadService.get().createFactory(); + } else { + AtomicInteger atomicInteger = new AtomicInteger(1); + factory = runnable -> { + Thread thread = new Thread(runnable); + String string = name + atomicInteger.getAndIncrement(); + TracyClient.setThreadName(string, name.hashCode()); + thread.setName(string); + thread.setDaemon(daemon); + thread.setUncaughtExceptionHandler(Util::onThreadException); + return thread; + }; + } + return new TracingExecutor(Executors.newCachedThreadPool(factory)); + // Leaf end - More virtual threads } // Paper start - Separate dimension data IO pool @@ -1099,7 +1138,7 @@ public class Util { } public static Typed readTypedOrThrow(Type type, Dynamic data, boolean partial) { - DataResult> dataResult = type.readTyped(data).map(Pair::getFirst); + DataResult> dataResult = type.readTyped(data).map(Pair::getFirst); // Paper - Fix generics issue // Gale - Fix generics issue try { return partial ? dataResult.getPartialOrThrow(IllegalStateException::new) : dataResult.getOrThrow(IllegalStateException::new); @@ -1148,7 +1187,7 @@ public class Util { } public void openUri(URI uri) { - throw new IllegalStateException("This method is not useful on dedicated servers."); // Paper - Fix warnings on build by removing client-only code + // throw new IllegalStateException("This method is not useful on dedicated servers."); // Paper - Fix warnings on build by removing client-only code // Leaf - More virtual threads - This method is not useful on dedicated servers } public void openFile(File file) {