9
0
mirror of https://github.com/BX-Team/DivineMC.git synced 2025-12-22 00:09:15 +00:00

Async Chunk Sending

This commit is contained in:
NONPLAYT
2025-03-03 19:36:45 +03:00
parent 7f2f07fbdc
commit 0b19b8f13d
3 changed files with 129 additions and 0 deletions

View File

@@ -235,6 +235,28 @@ public class DivineConfig {
"modpacks where many structure mods are using very high weight values in their template pools.");
}
public static boolean asyncChunkSendingEnabled = true;
public static int asyncChunkSendingThreadCount = 1;
public static boolean asyncChunkSendingUseVirtualThreads = false;
public static int asyncChunkSendingRateLimitChunkSends = 50;
private void asyncChunkSending() {
asyncChunkSendingEnabled = getBoolean("settings.async-chunk-sending.enable", asyncChunkSendingEnabled,
"Enables chunk sending runs off-main thread.");
asyncChunkSendingThreadCount = getInt("settings.async-chunk-sending.thread-count", asyncChunkSendingThreadCount,
"Amount of threads to use for async chunk sending. (If use-virtual-threads is enabled, this will be ignored)");
asyncChunkSendingUseVirtualThreads = getBoolean("settings.async-chunk-sending.use-virtual-threads", asyncChunkSendingUseVirtualThreads,
"Similar to the 'virtual-thread' options. This will use the virtual thread executor for chunk sending.");
asyncChunkSendingRateLimitChunkSends = getInt("settings.async-chunk-sending.rate-limit-chunk-sends", asyncChunkSendingRateLimitChunkSends,
"DivineMC have optimization patches that speed ups world generation,",
"so chunk loading/generating may be faster and with this, server can spam a ton of chunk packets to the client on server join.",
"This setting will limit the amount of chunk packets sent to the client per tick, allowing a smoother ping when sending chunks on join",
"",
"Set to -1 to disable rate limiting (not recommended)");
}
public static boolean enableRegionizedChunkTicking = false;
public static int regionizedChunkTickingExecutorThreadCount = 4;
public static int regionizedChunkTickingExecutorThreadPriority = Thread.NORM_PRIORITY;

View File

@@ -0,0 +1,47 @@
package org.bxteam.divinemc.server.chunk;
import ca.spottedleaf.moonrise.common.util.TickThread;
import net.minecraft.server.MinecraftServer;
import net.minecraft.server.level.ServerLevel;
import org.bxteam.divinemc.DivineConfig;
import org.bxteam.divinemc.util.NamedAgnosticThreadFactory;
import org.jetbrains.annotations.NotNull;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public class ChunkSendingExecutor {
private static final ExecutorService SERVICE = DivineConfig.asyncChunkSendingEnabled
? DivineConfig.asyncChunkSendingUseVirtualThreads ?
Executors.newVirtualThreadPerTaskExecutor() :
Executors.newFixedThreadPool(
DivineConfig.asyncChunkSendingThreadCount,
new NamedAgnosticThreadFactory<>("chunk_sending", TickThread::new, Thread.NORM_PRIORITY)
)
: null;
public static void execute(Runnable runnable, ServerLevel level) {
runnable = wrapRunnable(runnable, level);
if (DivineConfig.asyncChunkSendingEnabled) {
SERVICE.submit(runnable);
} else {
runnable.run();
}
}
private static @NotNull Runnable wrapRunnable(Runnable runnable, final ServerLevel level) {
return () -> {
try {
runnable.run();
} catch (Throwable throwable) {
MinecraftServer.LOGGER.warn("Failed to send chunk data! Retrying...");
level.getServer().scheduleOnMain(() -> {
try {
runnable.run();
} catch (Throwable failed) {
MinecraftServer.LOGGER.error("Failed to send chunk data! (2nd attempt). Logging error log", failed);
}
});
}
};
}
}