9
0
mirror of https://github.com/Winds-Studio/Leaf.git synced 2025-12-19 15:09:25 +00:00
Files
Leaf/leaf-server/minecraft-patches/features/0242-More-virtual-threads.patch
2025-06-06 00:26:36 +08:00

113 lines
5.7 KiB
Diff

From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: Taiyou06 <kaandindar21@gmail.com>
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 9918572306e983281d05c6d28c8a5d843348ad2d..1d4ad1370ca041753ce765b1a2feddae59f1f8ca 100644
--- a/net/minecraft/Util.java
+++ b/net/minecraft/Util.java
@@ -98,7 +98,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();
@@ -111,7 +112,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;
@@ -255,16 +279,31 @@ public class Util {
}
private static TracingExecutor makeIoExecutor(String name, boolean daemon) {
- AtomicInteger atomicInteger = new AtomicInteger(1);
- return new TracingExecutor(Executors.newCachedThreadPool(task -> {
- Thread thread = new Thread(task);
- 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 = task -> {
+ Thread thread = new Thread(task);
+ 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
@@ -1109,7 +1148,7 @@ public class Util {
}
public static <T> Typed<T> readTypedOrThrow(Type<T> type, Dynamic<?> data, boolean partial) {
- DataResult<Typed<T>> dataResult = type.readTyped(data).map(Pair::getFirst);
+ DataResult<Typed<T>> 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);
@@ -1158,7 +1197,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) {