From 65aa9dc3431867ae296438d1c2c9487d550b5fe0 Mon Sep 17 00:00:00 2001 From: CrazyDev22 Date: Sat, 18 May 2024 23:34:53 +0200 Subject: [PATCH] minor headless performance improvements --- .../iris/core/commands/CommandDeveloper.java | 6 ++- .../com/volmit/iris/core/nms/IHeadless.java | 2 +- .../methods/HeadlessPregenMethod.java | 16 ++++---- .../iris/core/tools/IrisPackBenchmarking.java | 6 ++- .../iris/core/nms/v1_20_R3/Headless.java | 40 ++++++++----------- 5 files changed, 33 insertions(+), 37 deletions(-) diff --git a/core/src/main/java/com/volmit/iris/core/commands/CommandDeveloper.java b/core/src/main/java/com/volmit/iris/core/commands/CommandDeveloper.java index cc26c7a04..fe95a0f50 100644 --- a/core/src/main/java/com/volmit/iris/core/commands/CommandDeveloper.java +++ b/core/src/main/java/com/volmit/iris/core/commands/CommandDeveloper.java @@ -152,10 +152,12 @@ public class CommandDeveloper implements DecreeExecutor { @Param(description = "The pack to bench", aliases = {"pack"}) IrisDimension dimension, @Param(description = "Headless", defaultValue = "false") - boolean headless + boolean headless, + @Param(description = "GUI", defaultValue = "false") + boolean gui ) { Iris.info("test"); - IrisPackBenchmarking benchmark = new IrisPackBenchmarking(dimension, 1, headless); + IrisPackBenchmarking benchmark = new IrisPackBenchmarking(dimension, 1, headless, gui); } diff --git a/core/src/main/java/com/volmit/iris/core/nms/IHeadless.java b/core/src/main/java/com/volmit/iris/core/nms/IHeadless.java index fe289a085..2b7accdc1 100644 --- a/core/src/main/java/com/volmit/iris/core/nms/IHeadless.java +++ b/core/src/main/java/com/volmit/iris/core/nms/IHeadless.java @@ -9,7 +9,7 @@ import java.io.Closeable; public interface IHeadless extends Closeable { - void saveAll(); + void save(); @ChunkCoordinates boolean exists(int x, int z); diff --git a/core/src/main/java/com/volmit/iris/core/pregenerator/methods/HeadlessPregenMethod.java b/core/src/main/java/com/volmit/iris/core/pregenerator/methods/HeadlessPregenMethod.java index 446a6c939..7196032fa 100644 --- a/core/src/main/java/com/volmit/iris/core/pregenerator/methods/HeadlessPregenMethod.java +++ b/core/src/main/java/com/volmit/iris/core/pregenerator/methods/HeadlessPregenMethod.java @@ -1,6 +1,7 @@ package com.volmit.iris.core.pregenerator.methods; import com.volmit.iris.Iris; +import com.volmit.iris.core.IrisSettings; import com.volmit.iris.core.nms.IHeadless; import com.volmit.iris.core.nms.INMS; import com.volmit.iris.core.pregenerator.PregenListener; @@ -15,14 +16,14 @@ import java.util.concurrent.Semaphore; public class HeadlessPregenMethod implements PregeneratorMethod { private final Engine engine; private final IHeadless headless; - private final MultiBurst burst; private final Semaphore semaphore; + private final int max; public HeadlessPregenMethod(Engine engine) { + this.max = IrisSettings.getThreadCount(IrisSettings.get().getConcurrency().getParallelism()); this.engine = engine; this.headless = INMS.get().createHeadless(engine); - this.burst = new MultiBurst("Iris Headless", Thread.MAX_PRIORITY); - this.semaphore = new Semaphore(1024); + this.semaphore = new Semaphore(max); } @Override @@ -31,10 +32,9 @@ public class HeadlessPregenMethod implements PregeneratorMethod { @Override public void close() { try { - semaphore.acquire(1024); + semaphore.acquire(max); } catch (InterruptedException ignored) {} - burst.close(); - headless.saveAll(); + headless.save(); try { headless.close(); } catch (IOException e) { @@ -45,7 +45,7 @@ public class HeadlessPregenMethod implements PregeneratorMethod { @Override public void save() { - headless.saveAll(); + headless.save(); } @Override @@ -69,7 +69,7 @@ public class HeadlessPregenMethod implements PregeneratorMethod { semaphore.release(); return; } - burst.complete(() -> { + MultiBurst.burst.complete(() -> { try { listener.onChunkGenerating(x, z); headless.generateChunk(x, z); diff --git a/core/src/main/java/com/volmit/iris/core/tools/IrisPackBenchmarking.java b/core/src/main/java/com/volmit/iris/core/tools/IrisPackBenchmarking.java index 6d438283a..f473d1f4a 100644 --- a/core/src/main/java/com/volmit/iris/core/tools/IrisPackBenchmarking.java +++ b/core/src/main/java/com/volmit/iris/core/tools/IrisPackBenchmarking.java @@ -45,15 +45,17 @@ public class IrisPackBenchmarking { private IrisDimension IrisDimension; private int radius; private final boolean headless; + private final boolean gui; private boolean finished = false; private Engine engine; PrecisionStopwatch stopwatch; - public IrisPackBenchmarking(IrisDimension dimension, int r, boolean headless) { + public IrisPackBenchmarking(IrisDimension dimension, int r, boolean headless, boolean gui) { instance = this; this.IrisDimension = dimension; this.radius = r; this.headless = headless; + this.gui = gui; runBenchmark(); } @@ -169,7 +171,7 @@ public class IrisPackBenchmarking { int z = 0; IrisToolbelt.pregenerate(PregenTask .builder() - .gui(false) + .gui(gui) .center(new Position2(x, z)) .width(5) .height(5) diff --git a/nms/v1_20_R3/src/main/java/com/volmit/iris/core/nms/v1_20_R3/Headless.java b/nms/v1_20_R3/src/main/java/com/volmit/iris/core/nms/v1_20_R3/Headless.java index 981f5fb2e..5284cbc83 100644 --- a/nms/v1_20_R3/src/main/java/com/volmit/iris/core/nms/v1_20_R3/Headless.java +++ b/nms/v1_20_R3/src/main/java/com/volmit/iris/core/nms/v1_20_R3/Headless.java @@ -48,7 +48,7 @@ public class Headless implements IHeadless, LevelHeightAccessor { private final Engine engine; private final RegionFileStorage storage; private final Queue chunkQueue = new ArrayDeque<>(); - private final ReentrantLock manualLock = new ReentrantLock(); + private final ReentrantLock saveLock = new ReentrantLock(); private final KMap> customBiomes = new KMap<>(); private final KMap> minecraftBiomes = new KMap<>(); private boolean closed = false; @@ -60,11 +60,7 @@ public class Headless implements IHeadless, LevelHeightAccessor { var queueLooper = new Looper() { @Override protected long loop() { - if (manualLock.isLocked()) { - manualLock.lock(); - manualLock.unlock(); - } - saveAll(); + save(); return closed ? -1 : 100; } }; @@ -84,26 +80,22 @@ public class Headless implements IHeadless, LevelHeightAccessor { } @Override - public void saveAll() { - manualLock.lock(); - try { - save(); - } finally { - manualLock.unlock(); - } - } - - private void save() { + public void save() { if (closed) return; - while (!chunkQueue.isEmpty()) { - ChunkAccess chunk = chunkQueue.poll(); - if (chunk == null) break; - try { - storage.write(chunk.getPos(), binding.serializeChunk(chunk, this)); - } catch (Throwable e) { - Iris.error("Failed to save chunk " + chunk.getPos().x + ", " + chunk.getPos().z); - e.printStackTrace(); + saveLock.lock(); + try { + while (!chunkQueue.isEmpty()) { + ChunkAccess chunk = chunkQueue.poll(); + if (chunk == null) break; + try { + storage.write(chunk.getPos(), binding.serializeChunk(chunk, this)); + } catch (Throwable e) { + Iris.error("Failed to save chunk " + chunk.getPos().x + ", " + chunk.getPos().z); + e.printStackTrace(); + } } + } finally { + saveLock.unlock(); } }