From 6774a84f6172ce7129729d9e9c9f840f1eacd4b3 Mon Sep 17 00:00:00 2001 From: s809 <43530948+s809@users.noreply.github.com> Date: Fri, 3 Jan 2025 20:22:36 +0500 Subject: [PATCH 1/5] Remove unused field --- .../distanthorizons/core/level/AbstractDhServerLevel.java | 4 ---- 1 file changed, 4 deletions(-) diff --git a/core/src/main/java/com/seibel/distanthorizons/core/level/AbstractDhServerLevel.java b/core/src/main/java/com/seibel/distanthorizons/core/level/AbstractDhServerLevel.java index c49b3b113..2ea69d063 100644 --- a/core/src/main/java/com/seibel/distanthorizons/core/level/AbstractDhServerLevel.java +++ b/core/src/main/java/com/seibel/distanthorizons/core/level/AbstractDhServerLevel.java @@ -51,8 +51,6 @@ public abstract class AbstractDhServerLevel extends AbstractDhLevel implements I private final FullDataSourceRequestHandler requestHandler = new FullDataSourceRequestHandler(this); - private final boolean NSizedGenerationSupported = false; - //=============// // constructor // @@ -217,8 +215,6 @@ public abstract class AbstractDhServerLevel extends AbstractDhLevel implements I // world gen // //===========// - public boolean isNSizedGenerationSupported() { return this.NSizedGenerationSupported; } - @Override public void onWorldGenTaskComplete(long pos) { From 5a31be1e42101fd4532cf890d308e12251c6659d Mon Sep 17 00:00:00 2001 From: s809 <43530948+s809@users.noreply.github.com> Date: Sat, 4 Jan 2025 19:21:39 +0500 Subject: [PATCH 2/5] Fix re-queueing of positions to reload cancelling the tick --- .../seibel/distanthorizons/core/render/LodQuadTree.java | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/core/src/main/java/com/seibel/distanthorizons/core/render/LodQuadTree.java b/core/src/main/java/com/seibel/distanthorizons/core/render/LodQuadTree.java index 5feb0bd57..132c48177 100644 --- a/core/src/main/java/com/seibel/distanthorizons/core/render/LodQuadTree.java +++ b/core/src/main/java/com/seibel/distanthorizons/core/render/LodQuadTree.java @@ -40,10 +40,7 @@ import org.apache.logging.log4j.Logger; import javax.annotation.WillNotClose; import java.awt.*; -import java.util.ArrayList; -import java.util.HashSet; -import java.util.Iterator; -import java.util.Objects; +import java.util.*; import java.util.concurrent.ConcurrentLinkedQueue; import java.util.concurrent.ThreadPoolExecutor; import java.util.concurrent.atomic.AtomicBoolean; @@ -426,7 +423,7 @@ public class LodQuadTree extends QuadTree implements IDebugRen if (positionsToRequeue.contains(pos)) { // don't attempt to re-load positions that are already in the process of reloading - break; + continue; } try @@ -450,7 +447,6 @@ public class LodQuadTree extends QuadTree implements IDebugRen // if we don't trigger it again the LOD will be out of date // and may be invisible/missing positionsToRequeue.add(pos); - break; } } } From e770943fc9f19fe8b60a1c655db867d751099dd2 Mon Sep 17 00:00:00 2001 From: s809 <43530948+s809@users.noreply.github.com> Date: Sat, 4 Jan 2025 19:22:27 +0500 Subject: [PATCH 3/5] Improve task prioritization --- .../core/util/threading/PrioritySemaphore.java | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/core/src/main/java/com/seibel/distanthorizons/core/util/threading/PrioritySemaphore.java b/core/src/main/java/com/seibel/distanthorizons/core/util/threading/PrioritySemaphore.java index cc7a3aed5..b4e60053c 100644 --- a/core/src/main/java/com/seibel/distanthorizons/core/util/threading/PrioritySemaphore.java +++ b/core/src/main/java/com/seibel/distanthorizons/core/util/threading/PrioritySemaphore.java @@ -2,9 +2,7 @@ package com.seibel.distanthorizons.core.util.threading; import org.jetbrains.annotations.NotNull; -import java.util.Comparator; import java.util.Random; -import java.util.concurrent.ConcurrentLinkedQueue; import java.util.concurrent.PriorityBlockingQueue; import java.util.concurrent.Semaphore; import java.util.concurrent.locks.ReentrantLock; @@ -67,12 +65,10 @@ public class PrioritySemaphore // this has to be outside the try-finally to prevent holding the lock while waiting synchronized (thread) { - // random value between -5 and +5 is used to prevent task starvation - // while still allowing higher priority tasks to run sooner - int priority = executor.priority + this.random.nextInt(11) - 5; + int priority = (int) ((executor.priority + 1) * executor.getTaskCount() * 100000 / executor.getAverageRunTimeInMs()); // this thread will be run when a permit is available - this.queue.put(new ThreadWithPriority(thread,priority)); + this.queue.put(new ThreadWithPriority(thread, priority)); thread.wait(); } From 8686e1727f65c9b6d682722d5e5cd9bda96762ea Mon Sep 17 00:00:00 2001 From: s809 <43530948+s809@users.noreply.github.com> Date: Sat, 4 Jan 2025 19:23:17 +0500 Subject: [PATCH 4/5] Move ThreadPoolExecutor#beforeExecute into correct place --- .../core/util/threading/RateLimitedThreadPoolExecutor.java | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/core/src/main/java/com/seibel/distanthorizons/core/util/threading/RateLimitedThreadPoolExecutor.java b/core/src/main/java/com/seibel/distanthorizons/core/util/threading/RateLimitedThreadPoolExecutor.java index 0b4161d9a..aa99da104 100644 --- a/core/src/main/java/com/seibel/distanthorizons/core/util/threading/RateLimitedThreadPoolExecutor.java +++ b/core/src/main/java/com/seibel/distanthorizons/core/util/threading/RateLimitedThreadPoolExecutor.java @@ -89,8 +89,6 @@ public class RateLimitedThreadPoolExecutor extends ThreadPoolExecutor implements @Override protected void beforeExecute(Thread thread, Runnable runnable) { - super.beforeExecute(thread, runnable); - long deltaMs = TimeUnit.NANOSECONDS.toMillis(this.lastRunDurationNanoTimeRef.get()); this.runTimeInMsRollingAverage.addValue(deltaMs); @@ -121,12 +119,15 @@ public class RateLimitedThreadPoolExecutor extends ThreadPoolExecutor implements this.runStartNanoTimeRef.set(System.nanoTime()); + + super.beforeExecute(thread, runnable); } @Override protected void afterExecute(Runnable runnable, Throwable throwable) { super.afterExecute(runnable, throwable); + this.lastRunDurationNanoTimeRef.set(System.nanoTime() - this.runStartNanoTimeRef.get()); From a916fe1db13533c3d55dcd452697afbe16e021c1 Mon Sep 17 00:00:00 2001 From: s809 <43530948+s809@users.noreply.github.com> Date: Sat, 4 Jan 2025 19:44:23 +0500 Subject: [PATCH 5/5] Add a comment to priority calculation --- .../core/util/threading/PrioritySemaphore.java | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/core/src/main/java/com/seibel/distanthorizons/core/util/threading/PrioritySemaphore.java b/core/src/main/java/com/seibel/distanthorizons/core/util/threading/PrioritySemaphore.java index b4e60053c..0bb7cc5ff 100644 --- a/core/src/main/java/com/seibel/distanthorizons/core/util/threading/PrioritySemaphore.java +++ b/core/src/main/java/com/seibel/distanthorizons/core/util/threading/PrioritySemaphore.java @@ -65,6 +65,12 @@ public class PrioritySemaphore // this has to be outside the try-finally to prevent holding the lock while waiting synchronized (thread) { + // Calculation rules: + // - Executors with higher priority need less tasks to run before other executors + // If one executor has the priority of 3 and other if of 4, + // the latter one will need 1/4 fewer tasks in queue to get its tasks running + // - Executors with short-lived tasks run before longer lived ones + // 100k value is a multiplier to prevent precision loss int priority = (int) ((executor.priority + 1) * executor.getTaskCount() * 100000 / executor.getAverageRunTimeInMs()); // this thread will be run when a permit is available