Add ThreadSleepPerformancePatch variants for Build 42.21 to optimize waiting loops. Replace Thread.sleep calls with hook-based alternatives. Include hooks, utility and tests.
This commit is contained in:
1 parent
529e48ed5a
commit
88cd68b5cb
12 files changed
+603
-1
No files matched your search
+50
@@ -0,0 +1,50 @@
|
||||
package dev.overlord.zomboidforge.compat.b42.hooks;
|
||||
|
||||
import dev.overlord.zomboidforge.compat.b42.util.WakeableSleep;
|
||||
|
||||
/**
|
||||
* Hooks for the waiting loops of the shutdown and the world save, which poll with
|
||||
* {@code Thread.sleep(100)} or {@code Thread.sleep(500)}: {@code ServerChunkLoader.quit},
|
||||
* {@code ZipBackup.waitFinish}, {@code ServerPlayersVehicles.stop},
|
||||
* {@code ServerPlayerDB.saveFinishWait}, and the round sleep of the
|
||||
* {@code ServerPlayersVehicles} thread. The loops check the same conditions as in vanilla; the
|
||||
* hooks only let them notice sooner that the condition holds.
|
||||
*/
|
||||
public final class WaitHooks
|
||||
{
|
||||
/** Longest sleep of a polling loop. */
|
||||
private static final long POLL_MILLIS = 5L;
|
||||
|
||||
/** The round sleep of the {@code ServerPlayersVehicles} thread. */
|
||||
private static final WakeableSleep VEHICLE_ROUND = new WakeableSleep();
|
||||
|
||||
private WaitHooks() {}
|
||||
|
||||
/** Replaces {@code Thread.sleep(millis)} in a polling loop: sleeps at most 5 ms. */
|
||||
public static void pollSleep(long millis) throws InterruptedException
|
||||
{
|
||||
Thread.sleep(Math.min(millis, WaitHooks.POLL_MILLIS));
|
||||
}
|
||||
|
||||
/**
|
||||
* Replaces {@code Thread.sleep(500)} at the end of a round of the {@code ServerPlayersVehicles}
|
||||
* thread ({@code SPVThread.runInner}): the same sleep, unless {@link #wakeVehiclesAndPoll}
|
||||
* ends it early.
|
||||
*/
|
||||
public static void vehicleRoundSleep(long millis) throws InterruptedException
|
||||
{
|
||||
WaitHooks.VEHICLE_ROUND.sleep(millis);
|
||||
}
|
||||
|
||||
/**
|
||||
* Replaces {@code Thread.sleep(100)} in loops that wait for the {@code ServerPlayersVehicles}
|
||||
* thread ({@code ServerPlayersVehicles.stop}: until it ended; {@code
|
||||
* ServerPlayerDB.saveFinishWait}: until it saved the queued players): wakes the thread, so its
|
||||
* next round starts now, then sleeps at most 5 ms.
|
||||
*/
|
||||
public static void wakeVehiclesAndPoll(long millis) throws InterruptedException
|
||||
{
|
||||
WaitHooks.VEHICLE_ROUND.wake();
|
||||
Thread.sleep(Math.min(millis, WaitHooks.POLL_MILLIS));
|
||||
}
|
||||
}
|
||||
+42
@@ -0,0 +1,42 @@
|
||||
package dev.overlord.zomboidforge.compat.b42.util;
|
||||
|
||||
/**
|
||||
* A sleep that another thread can end early. {@link #wake()} ends the current
|
||||
* {@link #sleep(long)}, or the next one if no thread sleeps at the moment, so a wake between two
|
||||
* sleeps is not lost. Unlike {@code Thread.interrupt()}, waking does not close NIO channels the
|
||||
* sleeping thread uses.
|
||||
*/
|
||||
public final class WakeableSleep
|
||||
{
|
||||
private final Object lock = new Object();
|
||||
private boolean woken;
|
||||
|
||||
/** Sleeps {@code millis} milliseconds, or until {@link #wake()}; nothing for {@code millis <= 0}, as {@code Thread.sleep}. */
|
||||
public void sleep(long millis) throws InterruptedException
|
||||
{
|
||||
if (millis <= 0) return;
|
||||
|
||||
long deadline = System.nanoTime() + millis * 1_000_000L;
|
||||
synchronized (this.lock)
|
||||
{
|
||||
// wait() may return without a notify; the loop keeps the full time unless woken.
|
||||
long remaining = millis;
|
||||
while (!this.woken && remaining > 0)
|
||||
{
|
||||
this.lock.wait(remaining);
|
||||
remaining = (deadline - System.nanoTime()) / 1_000_000L;
|
||||
}
|
||||
this.woken = false;
|
||||
}
|
||||
}
|
||||
|
||||
/** Ends the current or the next {@link #sleep(long)}. */
|
||||
public void wake()
|
||||
{
|
||||
synchronized (this.lock)
|
||||
{
|
||||
this.woken = true;
|
||||
this.lock.notifyAll();
|
||||
}
|
||||
}
|
||||
}
|
||||
+74
@@ -0,0 +1,74 @@
|
||||
package dev.overlord.zomboidforge.compat.b42.util;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.util.concurrent.CountDownLatch;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
class WakeableSleepTest
|
||||
{
|
||||
private static long millisOf(Runnable sleep)
|
||||
{
|
||||
long start = System.nanoTime();
|
||||
sleep.run();
|
||||
return (System.nanoTime() - start) / 1_000_000L;
|
||||
}
|
||||
|
||||
private static Runnable sleeping(WakeableSleep sleep, long millis)
|
||||
{
|
||||
return () -> {
|
||||
try { sleep.sleep(millis); }
|
||||
catch (InterruptedException e) { throw new IllegalStateException(e); }
|
||||
};
|
||||
}
|
||||
|
||||
@Test
|
||||
void sleepsTheFullTimeWithoutAWake()
|
||||
{
|
||||
long slept = WakeableSleepTest.millisOf(WakeableSleepTest.sleeping(new WakeableSleep(), 200));
|
||||
|
||||
assertTrue(slept >= 195, () -> "Slept only " + slept + " ms.");
|
||||
}
|
||||
|
||||
@Test
|
||||
void aWakeEndsTheSleepEarly() throws InterruptedException
|
||||
{
|
||||
WakeableSleep sleep = new WakeableSleep();
|
||||
CountDownLatch started = new CountDownLatch(1);
|
||||
long[] slept = new long[1];
|
||||
|
||||
Thread sleeper = new Thread(() -> {
|
||||
started.countDown();
|
||||
slept[0] = WakeableSleepTest.millisOf(WakeableSleepTest.sleeping(sleep, 10_000));
|
||||
});
|
||||
sleeper.start();
|
||||
started.await();
|
||||
Thread.sleep(50);
|
||||
|
||||
sleep.wake();
|
||||
sleeper.join(5_000);
|
||||
|
||||
assertTrue(slept[0] < 5_000, () -> "Slept " + slept[0] + " ms despite the wake.");
|
||||
}
|
||||
|
||||
@Test
|
||||
void aWakeBeforeTheSleepEndsTheNextSleep()
|
||||
{
|
||||
WakeableSleep sleep = new WakeableSleep();
|
||||
sleep.wake();
|
||||
|
||||
long first = WakeableSleepTest.millisOf(WakeableSleepTest.sleeping(sleep, 5_000));
|
||||
long second = WakeableSleepTest.millisOf(WakeableSleepTest.sleeping(sleep, 100));
|
||||
|
||||
assertTrue(first < 1_000, () -> "Slept " + first + " ms despite the earlier wake.");
|
||||
assertTrue(second >= 95, () -> "The wake ended a second sleep too: " + second + " ms.");
|
||||
}
|
||||
|
||||
@Test
|
||||
void doesNotSleepForZeroOrLess()
|
||||
{
|
||||
assertTrue(WakeableSleepTest.millisOf(WakeableSleepTest.sleeping(new WakeableSleep(), 0)) < 50);
|
||||
assertTrue(WakeableSleepTest.millisOf(WakeableSleepTest.sleeping(new WakeableSleep(), -1)) < 50);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user