mirror of
https://github.com/Winds-Studio/Leaf.git
synced 2025-12-21 16:09:19 +00:00
94 lines
4.0 KiB
Diff
94 lines
4.0 KiB
Diff
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
|
|
From: Radiation_pi <pi1243039811@outlook.com>
|
|
Date: Mon, 25 Mar 2024 23:00:19 +0800
|
|
Subject: [PATCH] Virtual Thread for linear flusher
|
|
|
|
|
|
diff --git a/src/main/java/org/dreeam/leaf/config/modules/opt/VT4LinearFlusher.java b/src/main/java/org/dreeam/leaf/config/modules/opt/VT4LinearFlusher.java
|
|
new file mode 100644
|
|
index 0000000000000000000000000000000000000000..d4576c9930d12548d4302207f61a0eb8b1fa7b7a
|
|
--- /dev/null
|
|
+++ b/src/main/java/org/dreeam/leaf/config/modules/opt/VT4LinearFlusher.java
|
|
@@ -0,0 +1,29 @@
|
|
+package org.dreeam.leaf.config.modules.opt;
|
|
+
|
|
+import com.electronwill.nightconfig.core.file.CommentedFileConfig;
|
|
+import org.dreeam.leaf.config.ConfigInfo;
|
|
+import org.dreeam.leaf.config.EnumConfigCategory;
|
|
+import org.dreeam.leaf.config.IConfigModule;
|
|
+
|
|
+public class VT4LinearFlusher implements IConfigModule {
|
|
+
|
|
+ @Override
|
|
+ public EnumConfigCategory getCategory() {
|
|
+ return EnumConfigCategory.PERFORMANCE;
|
|
+ }
|
|
+
|
|
+ @Override
|
|
+ public String getBaseName() {
|
|
+ return "use_virtual_thread_for_linear_flusher";
|
|
+ }
|
|
+
|
|
+ @ConfigInfo(baseName = "enabled")
|
|
+ public static boolean enabled = false;
|
|
+
|
|
+ @Override
|
|
+ public void onLoaded(CommentedFileConfig config) {
|
|
+ config.setComment("performance.use_virtual_thread_for_linear_flusher", """
|
|
+ Use the new Virtual Thread introduced in JDK 21 for Linear Region Flusher.
|
|
+ """);
|
|
+ }
|
|
+}
|
|
diff --git a/src/main/java/org/purpurmc/purpur/region/LinearRegionFileFlusher.java b/src/main/java/org/purpurmc/purpur/region/LinearRegionFileFlusher.java
|
|
index 0d3d9193e8d8f72141dc155840c5eed1a744761c..339157b5e5a81e3c1c5b54fbd9e3c53ea3f51716 100644
|
|
--- a/src/main/java/org/purpurmc/purpur/region/LinearRegionFileFlusher.java
|
|
+++ b/src/main/java/org/purpurmc/purpur/region/LinearRegionFileFlusher.java
|
|
@@ -4,9 +4,12 @@ import com.google.common.util.concurrent.ThreadFactoryBuilder;
|
|
|
|
import java.util.Queue;
|
|
import java.util.concurrent.*;
|
|
+import java.lang.reflect.InvocationTargetException;
|
|
+import java.lang.reflect.Method;
|
|
|
|
import org.apache.logging.log4j.LogManager;
|
|
import org.apache.logging.log4j.Logger;
|
|
+import org.galemc.gale.virtualthread.VirtualThreadService;
|
|
import org.purpurmc.purpur.PurpurConfig;
|
|
import org.bukkit.Bukkit;
|
|
|
|
@@ -18,12 +21,28 @@ public class LinearRegionFileFlusher {
|
|
.setNameFormat("linear-flush-scheduler")
|
|
.build()
|
|
);
|
|
- private final ExecutorService executor = Executors.newFixedThreadPool(
|
|
- PurpurConfig.linearFlushThreads,
|
|
- new ThreadFactoryBuilder()
|
|
- .setNameFormat("linear-flusher-%d")
|
|
- .build()
|
|
- );
|
|
+
|
|
+ private ExecutorService executor;
|
|
+
|
|
+ {
|
|
+ try {
|
|
+ if (VirtualThreadService.getJavaMajorVersion() >= VirtualThreadService.minimumJavaMajorVersionWithoutFeaturePreview && org.dreeam.leaf.config.modules.opt.VT4LinearFlusher.enabled) {
|
|
+ Method newThreadPerTaskExecutor = Executors.class.getMethod("newThreadPerTaskExecutor", ThreadFactory.class);
|
|
+
|
|
+ executor = (ExecutorService) newThreadPerTaskExecutor.invoke(null, VirtualThreadService.get().createFactory());
|
|
+ } else {
|
|
+ throw new UnsupportedOperationException();
|
|
+ }
|
|
+ } catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException |
|
|
+ UnsupportedOperationException e) {
|
|
+ LOGGER.error("Failed to create Virtual Thread executor! Fallback to default executor.");
|
|
+ executor = Executors.newFixedThreadPool(
|
|
+ PurpurConfig.linearFlushThreads,
|
|
+ new ThreadFactoryBuilder()
|
|
+ .setNameFormat("linear-flusher-%d")
|
|
+ .build());
|
|
+ }
|
|
+ }
|
|
|
|
public LinearRegionFileFlusher() {
|
|
Bukkit.getLogger().info("Using " + PurpurConfig.linearFlushThreads + " threads for linear region flushing.");
|