From 354293f192e840ae16c84818a708c59abb20cc9b Mon Sep 17 00:00:00 2001 From: Joshua Castle <26531652+Kas-tle@users.noreply.github.com> Date: Sat, 16 Aug 2025 00:54:38 -0700 Subject: [PATCH] :wqPerformance improvements: - Update dependencies - Use blocking queue for Tick.asyncTasksQueue - Do not call removed method Thread#stop in Tick#waitAndKillThreads - Use virtual threads for ClientConnection - Add build action Signed-off-by: Joshua Castle <26531652+Kas-tle@users.noreply.github.com> --- .github/workflows/build.yml | 30 ++++++++++ pom.xml | 58 +++++++++---------- src/main/java/com/loohp/limbo/Console.java | 2 +- .../loohp/limbo/network/ClientConnection.java | 16 +++-- .../loohp/limbo/network/ServerConnection.java | 9 ++- .../java/com/loohp/limbo/scheduler/Tick.java | 39 +++++-------- .../java/com/loohp/limbo/world/Schematic.java | 2 +- .../java/com/loohp/limbo/world/World.java | 4 +- 8 files changed, 96 insertions(+), 64 deletions(-) create mode 100644 .github/workflows/build.yml diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml new file mode 100644 index 0000000..83eaf2d --- /dev/null +++ b/.github/workflows/build.yml @@ -0,0 +1,30 @@ +name: Build + +on: + push: + branches: + - '**' + +jobs: + build: + runs-on: ubuntu-latest + steps: + - name: Checkout Repository + # See https://github.com/actions/checkout/releases + uses: actions/checkout@08c6903cd8c0fde910a37f88322edcfb5dd907a8 + - name: Set up JDK 21 + # See https://github.com/actions/setup-java/releases + uses: actions/setup-java@c5195efecf7bdfc987ee8bae7a71cb8b11521c00 + with: + java-version: 21 + distribution: temurin + - name: Build with Maven + run: mvn clean package + - name: Archive Artifacts + # See https://github.com/actions/upload-artifact/releases + uses: actions/upload-artifact@ea165f8d65b6e75b540449e92b4886f43607fa02 + if: success() + with: + name: Limbo + path: target/Limbo-*.jar + if-no-files-found: error \ No newline at end of file diff --git a/pom.xml b/pom.xml index 95a4ff3..b619334 100644 --- a/pom.xml +++ b/pom.xml @@ -34,8 +34,8 @@ UTF-8 ${project.version} - 1.8 - 1.8 + 21 + 21 @@ -91,17 +91,17 @@ maven-compiler-plugin - 3.8.0 + 3.14.0 - 1.8 - 1.8 + 21 + 21 ISO-8859-1 maven-javadoc-plugin - 3.2.0 + 3.11.2 false ISO-8859-1 @@ -124,7 +124,7 @@ org.apache.maven.plugins maven-source-plugin - 3.2.0 + 3.3.1 attach-sources @@ -170,7 +170,7 @@ org.apache.maven.plugins maven-source-plugin - 3.2.1 + 3.3.1 ${project.build.sourceEncoding} @@ -187,7 +187,7 @@ org.apache.maven.plugins maven-javadoc-plugin - 3.2.0 + 3.11.2 ${project.build.sourceEncoding} @@ -206,47 +206,39 @@ - - jitpack.io - https://jitpack.io - - - sonatype-oss-snapshots1 - https://s01.oss.sonatype.org/content/repositories/snapshots/ - - - bungeecord-repo - https://oss.sonatype.org/content/repositories/snapshots - maven_central Maven Central https://repo.maven.apache.org/maven2/ + + jitpack.io + https://jitpack.io + org.apache.commons commons-lang3 - 3.14.0 + 3.18.0 com.github.Querz NBT - 5.5 + 6.1 compile com.google.code.gson gson - 2.10.1 + 2.13.1 compile org.yaml snakeyaml - 2.3 + 2.4 compile @@ -258,7 +250,14 @@ net.md-5 bungeecord-chat - 1.18-R0.1-SNAPSHOT + 1.21-R0.3 + jar + compile + + + net.md-5 + bungeecord-serializer + 1.21-R0.3 jar compile @@ -295,21 +294,20 @@ org.fusesource.jansi jansi - 1.18 + 2.4.2 compile org.jline jline - 3.16.0 + 3.30.5 compile jline jline - 2.11 + 2.14.6 compile - diff --git a/src/main/java/com/loohp/limbo/Console.java b/src/main/java/com/loohp/limbo/Console.java index 5978585..5b4d56d 100644 --- a/src/main/java/com/loohp/limbo/Console.java +++ b/src/main/java/com/loohp/limbo/Console.java @@ -118,7 +118,7 @@ public class Console implements CommandSender { reader.setExpandEvents(false); reader.setHandleUserInterrupt(false); - terminal = TerminalBuilder.builder().streams(in, out).system(true).jansi(true).build(); + terminal = TerminalBuilder.builder().streams(in, out).jansi(true).build(); tabReader = LineReaderBuilder.builder().terminal(terminal).completer(new Completer() { @Override public void complete(LineReader reader, ParsedLine line, List candidates) { diff --git a/src/main/java/com/loohp/limbo/network/ClientConnection.java b/src/main/java/com/loohp/limbo/network/ClientConnection.java index a276a74..445cefd 100644 --- a/src/main/java/com/loohp/limbo/network/ClientConnection.java +++ b/src/main/java/com/loohp/limbo/network/ClientConnection.java @@ -151,15 +151,18 @@ import java.util.TimerTask; import java.util.UUID; import java.util.concurrent.TimeUnit; import java.util.concurrent.atomic.AtomicLong; +import java.util.concurrent.locks.Lock; +import java.util.concurrent.locks.ReentrantLock; import java.util.stream.Collectors; -public class ClientConnection extends Thread { +public class ClientConnection implements Runnable { private static final Key DEFAULT_HANDLER_NAMESPACE = Key.key("default"); private static final String BRAND_ANNOUNCE_CHANNEL = Key.key("brand").toString(); private final Random random = new Random(); private final Socket clientSocket; + private final Lock packetSendLock = new ReentrantLock(); protected Channel channel; private boolean running; private volatile ClientState state; @@ -234,9 +237,14 @@ public class ClientConnection extends Thread { sendPacket(packet); } - public synchronized void sendPacket(PacketOut packet) throws IOException { - if (channel.writePacket(packet)) { - setLastPacketTimestamp(System.currentTimeMillis()); + public void sendPacket(PacketOut packet) throws IOException { + packetSendLock.lock(); + try { + if (channel.writePacket(packet)) { + setLastPacketTimestamp(System.currentTimeMillis()); + } + } finally { + packetSendLock.unlock(); } } diff --git a/src/main/java/com/loohp/limbo/network/ServerConnection.java b/src/main/java/com/loohp/limbo/network/ServerConnection.java index 6185faa..76fbf13 100644 --- a/src/main/java/com/loohp/limbo/network/ServerConnection.java +++ b/src/main/java/com/loohp/limbo/network/ServerConnection.java @@ -27,12 +27,15 @@ import java.net.ServerSocket; import java.net.Socket; import java.util.ArrayList; import java.util.List; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; public class ServerConnection extends Thread { private final String ip; private final int port; private final boolean silent; + private final ExecutorService virtualThreadExecutor = Executors.newVirtualThreadPerTaskExecutor(); private ServerSocket serverSocket; private List clients; @@ -53,9 +56,9 @@ public class ServerConnection extends Thread { } while (true) { Socket connection = serverSocket.accept(); - ClientConnection sc = new ClientConnection(connection); - clients.add(sc); - sc.start(); + ClientConnection clientTask = new ClientConnection(connection); + clients.add(clientTask); + virtualThreadExecutor.submit(clientTask); } } catch(IOException e) { e.printStackTrace(); diff --git a/src/main/java/com/loohp/limbo/scheduler/Tick.java b/src/main/java/com/loohp/limbo/scheduler/Tick.java index 80a12e3..faa63d1 100644 --- a/src/main/java/com/loohp/limbo/scheduler/Tick.java +++ b/src/main/java/com/loohp/limbo/scheduler/Tick.java @@ -26,8 +26,8 @@ import com.loohp.limbo.scheduler.LimboScheduler.LimboSchedulerTask; import java.io.IOException; import java.util.ArrayList; import java.util.List; -import java.util.Queue; -import java.util.concurrent.ConcurrentLinkedQueue; +import java.util.concurrent.BlockingQueue; +import java.util.concurrent.LinkedBlockingQueue; import java.util.concurrent.TimeUnit; import java.util.concurrent.atomic.AtomicLong; @@ -37,7 +37,7 @@ public class Tick { private AtomicLong tick = new AtomicLong(0); private List threads = new ArrayList<>(); - private Queue asyncTasksQueue = new ConcurrentLinkedQueue<>(); + private BlockingQueue asyncTasksQueue = new LinkedBlockingQueue<>(); public Tick(Limbo instance) { new Thread(() -> { @@ -46,22 +46,19 @@ public class Tick { for (int i = 0; i < 4; i++) { Thread thread = new Thread(() -> { while (instance.isRunning()) { - LimboSchedulerTask task = asyncTasksQueue.poll(); - if (task == null) { - try { - TimeUnit.NANOSECONDS.sleep(10000); - } catch (InterruptedException e) { - e.printStackTrace(); - } - } else { - LimboTask limboTask = task.getTask(); - try { - limboTask.run(); - } catch (Throwable e) { - System.err.println("Task " + task.getTaskId() + " threw an exception: " + e.getLocalizedMessage()); - e.printStackTrace(); - } - } + try { + LimboSchedulerTask task = asyncTasksQueue.take(); + LimboTask limboTask = task.getTask(); + try { + limboTask.run(); + } catch (Throwable e) { + System.err.println("Task " + task.getTaskId() + " threw an exception: " + e.getLocalizedMessage()); + e.printStackTrace(); + } + } catch (InterruptedException e) { + Thread.currentThread().interrupt(); + e.printStackTrace(); + } } }); thread.start(); @@ -124,7 +121,6 @@ public class Tick { return tick.get(); } - @SuppressWarnings("deprecation") public void waitAndKillThreads(long waitTime) { long end = System.currentTimeMillis() + waitTime; for (Thread thread : threads) { @@ -133,9 +129,6 @@ public class Tick { } catch (InterruptedException e) { e.printStackTrace(); } - if (thread.isAlive()) { - thread.stop(); - } } } diff --git a/src/main/java/com/loohp/limbo/world/Schematic.java b/src/main/java/com/loohp/limbo/world/Schematic.java index 9568984..49bbc35 100644 --- a/src/main/java/com/loohp/limbo/world/Schematic.java +++ b/src/main/java/com/loohp/limbo/world/Schematic.java @@ -97,7 +97,7 @@ public class Schematic { CompoundTag heightMap = new CompoundTag(); heightMap.putLongArray("MOTION_BLOCKING", new long[] {1371773531765642314L,1389823183635651148L,1371738278539598925L,1389823183635388492L,1353688558756731469L,1389823114781694027L,1317765589597723213L,1371773531899860042L,1389823183635651149L,1371773462911685197L,1389823183635650636L,1353688626805119565L,1371773531900123211L,1335639250618849869L,1371738278674077258L,1389823114781694028L,1353723811310638154L,1371738278674077259L,1335674228429068364L,1335674228429067338L,1335674228698027594L,1317624576693539402L,1335709481520370249L,1299610178184057417L,1335638906349064264L,1299574993811968586L,1299574924958011464L,1299610178184056904L,1299574924958011464L,1299610109330100296L,1299574924958011464L,1299574924823793736L,1299574924958011465L,1281525273222484040L,1299574924958011464L,1281525273222484040L,9548107335L}); chunk.setHeightMaps(heightMap); - chunk.setBiomes(new int[256]); + chunk.setBiomes(new int[1024]); chunk.cleanupPalettesAndBlockStates(); } } diff --git a/src/main/java/com/loohp/limbo/world/World.java b/src/main/java/com/loohp/limbo/world/World.java index d9f23c2..e98efe2 100644 --- a/src/main/java/com/loohp/limbo/world/World.java +++ b/src/main/java/com/loohp/limbo/world/World.java @@ -64,7 +64,7 @@ public class World { EMPTY_CHUNK.cleanupPalettesAndBlockStates(); EMPTY_CHUNK.setHeightMaps(HEIGHT_MAP.clone()); - EMPTY_CHUNK.setBiomes(new int[256]); + EMPTY_CHUNK.setBiomes(new int[1024]); EMPTY_CHUNK.setTileEntities(new ListTag<>(CompoundTag.class)); } @@ -90,7 +90,7 @@ public class World { Chunk chunk = chunks[x][z]; chunk.cleanupPalettesAndBlockStates(); chunk.setHeightMaps(HEIGHT_MAP.clone()); - chunk.setBiomes(new int[256]); + chunk.setBiomes(new int[1024]); chunk.setTileEntities(new ListTag<>(CompoundTag.class)); } }