Introduce FrameBudget utility and ServerMapCellBudgetPerformancePatch for frame-based workload distribution.
This commit is contained in:
1 parent
38a15db299
commit
56e01c5cbe
6 files changed
+429
-1
No files matched your search
+48
@@ -0,0 +1,48 @@
|
||||
package dev.overlord.zomboidforge.compat.b42.hooks;
|
||||
|
||||
import dev.overlord.zomboidforge.compat.b42.util.FrameBudget;
|
||||
|
||||
import zombie.network.ServerLOS;
|
||||
import zombie.network.ServerMap;
|
||||
|
||||
/** Hooks called by patched {@code zombie.network.ServerMap} code. */
|
||||
public final class ServerMapHooks
|
||||
{
|
||||
/**
|
||||
* Main-thread time per frame for integrating finished cells (frames target 100 ms). One
|
||||
* integration takes 70 to 300 ms on new terrain, so in practice a frame integrates one cell,
|
||||
* or a few small ones.
|
||||
*/
|
||||
private static final long BUDGET_NANOS = 40_000_000L;
|
||||
|
||||
private static final FrameBudget CELL_INTEGRATION = new FrameBudget(ServerMapHooks.BUDGET_NANOS, System::nanoTime);
|
||||
|
||||
private ServerMapHooks() {}
|
||||
|
||||
/**
|
||||
* Replaces {@code ServerLOS.instance.suspend()} in {@code ServerMap.preupdate}, which precedes
|
||||
* the loop that integrates the finished cells of this frame: suspends, then starts the
|
||||
* frame's integration budget.
|
||||
*/
|
||||
public static void beginIntegration(ServerLOS los)
|
||||
{
|
||||
los.suspend();
|
||||
ServerMapHooks.CELL_INTEGRATION.begin();
|
||||
}
|
||||
|
||||
/**
|
||||
* Replaces {@code cell.Load2()} in that loop. Vanilla integrates every finished cell in the
|
||||
* frame it finishes; here a cell beyond the frame's budget is skipped: {@code false} keeps it
|
||||
* in {@code ServerCell.loaded2}, at the front, and the loop of the next frame integrates it.
|
||||
* The work per cell is unchanged; a cell becomes active up to a few frames later.
|
||||
*/
|
||||
public static boolean integrate(ServerMap.ServerCell cell)
|
||||
{
|
||||
if (!ServerMapHooks.CELL_INTEGRATION.allowsAnother()) return false;
|
||||
|
||||
boolean integrated = cell.Load2();
|
||||
if (integrated) ServerMapHooks.CELL_INTEGRATION.count();
|
||||
|
||||
return integrated;
|
||||
}
|
||||
}
|
||||
+40
@@ -0,0 +1,40 @@
|
||||
package dev.overlord.zomboidforge.compat.b42.util;
|
||||
|
||||
import java.util.function.LongSupplier;
|
||||
|
||||
/**
|
||||
* A time budget for work spread over frames: after {@link #begin()}, work units are allowed
|
||||
* until the budget is used up, but always the first one, so every frame makes progress.
|
||||
* Not thread-safe; one instance per thread.
|
||||
*/
|
||||
public final class FrameBudget
|
||||
{
|
||||
private final long budgetNanos;
|
||||
private final LongSupplier clock;
|
||||
|
||||
private long start;
|
||||
private int units;
|
||||
|
||||
/** @param clock nanoseconds, e.g. {@code System::nanoTime} */
|
||||
public FrameBudget(long budgetNanos, LongSupplier clock)
|
||||
{
|
||||
this.budgetNanos = budgetNanos;
|
||||
this.clock = clock;
|
||||
}
|
||||
|
||||
/** Starts the budget of a new frame. */
|
||||
public void begin()
|
||||
{
|
||||
this.start = this.clock.getAsLong();
|
||||
this.units = 0;
|
||||
}
|
||||
|
||||
/** Whether another unit may run: the first of the frame always, later ones while time is left. */
|
||||
public boolean allowsAnother()
|
||||
{
|
||||
return this.units == 0 || this.clock.getAsLong() - this.start < this.budgetNanos;
|
||||
}
|
||||
|
||||
/** Records a unit that ran. */
|
||||
public void count() { this.units++; }
|
||||
}
|
||||
+68
@@ -0,0 +1,68 @@
|
||||
package dev.overlord.zomboidforge.compat.b42.util;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
class FrameBudgetTest
|
||||
{
|
||||
/** A clock the test sets; nanoseconds. */
|
||||
private long now;
|
||||
|
||||
private FrameBudget budget(long budgetNanos)
|
||||
{
|
||||
return new FrameBudget(budgetNanos, () -> this.now);
|
||||
}
|
||||
|
||||
@Test
|
||||
void firstUnitOfAFrameRunsEvenWhenTheBudgetIsUsedUp()
|
||||
{
|
||||
FrameBudget budget = this.budget(40);
|
||||
budget.begin();
|
||||
|
||||
this.now += 1_000;
|
||||
assertTrue(budget.allowsAnother());
|
||||
}
|
||||
|
||||
@Test
|
||||
void laterUnitsRunWhileTimeIsLeft()
|
||||
{
|
||||
FrameBudget budget = this.budget(40);
|
||||
budget.begin();
|
||||
budget.count();
|
||||
|
||||
this.now += 39;
|
||||
assertTrue(budget.allowsAnother());
|
||||
|
||||
this.now += 1;
|
||||
assertFalse(budget.allowsAnother());
|
||||
}
|
||||
|
||||
@Test
|
||||
void aNewFrameStartsAFreshBudget()
|
||||
{
|
||||
FrameBudget budget = this.budget(40);
|
||||
budget.begin();
|
||||
budget.count();
|
||||
this.now += 100;
|
||||
assertFalse(budget.allowsAnother());
|
||||
|
||||
budget.begin();
|
||||
assertTrue(budget.allowsAnother());
|
||||
budget.count();
|
||||
assertTrue(budget.allowsAnother());
|
||||
}
|
||||
|
||||
@Test
|
||||
void anUnusedSkipDoesNotCountAsAUnit()
|
||||
{
|
||||
// A frame where nothing ran yet still allows its first unit, however late.
|
||||
FrameBudget budget = this.budget(40);
|
||||
budget.begin();
|
||||
this.now += 100;
|
||||
|
||||
assertTrue(budget.allowsAnother());
|
||||
assertTrue(budget.allowsAnother());
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user