Add AssetLoadingHooks and GameServerAssetLoadingPerformancePatch for Build 42.21. Optimize asset loading during startup by increasing loader threads and reducing main thread spinning. Include hooks, unit tests, and integrate the patch into the performance patch set.
This commit is contained in:
1 parent
43df96391c
commit
99f4f49a7c
5 files changed
+344
-1
No files matched your search
+91
@@ -0,0 +1,91 @@
|
||||
package dev.overlord.zomboidforge.compat.b42.hooks;
|
||||
|
||||
import dev.overlord.zomboidforge.compat.b42.ForgeInstance;
|
||||
import dev.overlord.zomboidforge.core.config.Feature;
|
||||
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.lang.invoke.MethodHandles;
|
||||
import java.lang.invoke.VarHandle;
|
||||
import java.util.concurrent.ExecutorService;
|
||||
import java.util.concurrent.ThreadPoolExecutor;
|
||||
import java.util.concurrent.locks.LockSupport;
|
||||
|
||||
import zombie.fileSystem.FileSystem;
|
||||
import zombie.fileSystem.FileSystemImpl;
|
||||
|
||||
/**
|
||||
* Hooks for the asset loading at startup ({@link Feature#EXPERIMENTAL_PERFORMANCE}): the loop
|
||||
* in {@code GameServer.doMinimumInit}
|
||||
*
|
||||
* <pre>{@code
|
||||
* while (GameWindow.fileSystem.hasWork()) GameWindow.fileSystem.updateAsyncTransactions();
|
||||
* }</pre>
|
||||
*
|
||||
* hands file tasks (model and animation imports, 16 at most in flight) to the loader threads of
|
||||
* {@code FileSystemImpl} and collects their results on the main thread. It spins: the main
|
||||
* thread takes a CPU that the loaders could use. Vanilla has 2 loader threads with up to 4
|
||||
* CPUs, 4 with more.
|
||||
*/
|
||||
@SuppressWarnings("unused")
|
||||
public final class AssetLoadingHooks
|
||||
{
|
||||
/** Pause of the main thread after each update; one task takes about 8 ms of loader time. */
|
||||
private static final long PARK_NANOS = 500_000L;
|
||||
|
||||
/** {@code FileSystemImpl.executor}, private; {@code null} when it cannot be reached. */
|
||||
private static final @Nullable VarHandle EXECUTOR;
|
||||
|
||||
static
|
||||
{
|
||||
VarHandle executor = null;
|
||||
try
|
||||
{
|
||||
executor = MethodHandles.privateLookupIn(FileSystemImpl.class, MethodHandles.lookup())
|
||||
.findVarHandle(FileSystemImpl.class, "executor", ExecutorService.class);
|
||||
}
|
||||
catch (ReflectiveOperationException e) { System.out.println("[ZomboidForge] Asset loading keeps its loader threads: 'FileSystemImpl.executor' not found: " + e); }
|
||||
|
||||
EXECUTOR = executor;
|
||||
}
|
||||
|
||||
/** Whether the loader pool was checked; the main thread is the only caller. */
|
||||
private static boolean poolChecked;
|
||||
|
||||
private AssetLoadingHooks() {}
|
||||
|
||||
/**
|
||||
* Replaces {@code fileSystem.updateAsyncTransactions()} in that loop. With the feature off,
|
||||
* only that call. On: the first call grows the loader pool to one thread per CPU but one (at
|
||||
* least 2), and every call parks the main thread for 0.5 ms after the update instead of
|
||||
* spinning. Fewer threads with a spinning main thread, or more threads alone, load slower.
|
||||
* The pool keeps its size after startup; the loaders run the same tasks as in vanilla.
|
||||
*/
|
||||
public static void updateAsyncTransactions(FileSystem fileSystem)
|
||||
{
|
||||
if (!ForgeInstance.FORGE.settings().enabled(Feature.EXPERIMENTAL_PERFORMANCE))
|
||||
{
|
||||
fileSystem.updateAsyncTransactions();
|
||||
return;
|
||||
}
|
||||
|
||||
if (!AssetLoadingHooks.poolChecked)
|
||||
{
|
||||
AssetLoadingHooks.poolChecked = true;
|
||||
|
||||
Object executor = AssetLoadingHooks.EXECUTOR != null && fileSystem instanceof FileSystemImpl ? AssetLoadingHooks.EXECUTOR.get(fileSystem) : null;
|
||||
int threads = Math.max(2, Runtime.getRuntime().availableProcessors() - 1);
|
||||
|
||||
// A fixed pool: maximum first, the core size may not exceed it.
|
||||
if (executor instanceof ThreadPoolExecutor pool && pool.getMaximumPoolSize() < threads)
|
||||
{
|
||||
System.out.println("[ZomboidForge] Asset loading: " + threads + " loader threads instead of " + pool.getMaximumPoolSize() + ".");
|
||||
pool.setMaximumPoolSize(threads);
|
||||
pool.setCorePoolSize(threads);
|
||||
}
|
||||
}
|
||||
|
||||
fileSystem.updateAsyncTransactions();
|
||||
LockSupport.parkNanos(AssetLoadingHooks.PARK_NANOS);
|
||||
}
|
||||
}
|
||||
+68
@@ -0,0 +1,68 @@
|
||||
package dev.overlord.zomboidforge.compat.b42.hooks;
|
||||
|
||||
import dev.overlord.zomboidforge.compat.b42.ForgeInstance;
|
||||
import dev.overlord.zomboidforge.core.Forge;
|
||||
import dev.overlord.zomboidforge.core.config.Feature;
|
||||
import dev.overlord.zomboidforge.core.config.ForgeSettings;
|
||||
|
||||
import org.junit.jupiter.api.AfterEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import zombie.fileSystem.FileSystemImpl;
|
||||
|
||||
import java.lang.reflect.Field;
|
||||
import java.time.Duration;
|
||||
import java.util.Set;
|
||||
import java.util.concurrent.ThreadPoolExecutor;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
/** The hook on a real {@code FileSystemImpl}; the feature is switched through the settings of {@code ForgeInstance.FORGE}. */
|
||||
class AssetLoadingHooksTest
|
||||
{
|
||||
private static void settings(ForgeSettings settings) throws ReflectiveOperationException
|
||||
{
|
||||
Field field = Forge.class.getDeclaredField("settings");
|
||||
field.setAccessible(true);
|
||||
field.set(ForgeInstance.FORGE, settings);
|
||||
}
|
||||
|
||||
private static ThreadPoolExecutor pool(FileSystemImpl fileSystem) throws ReflectiveOperationException
|
||||
{
|
||||
Field field = FileSystemImpl.class.getDeclaredField("executor");
|
||||
field.setAccessible(true);
|
||||
return (ThreadPoolExecutor) field.get(fileSystem);
|
||||
}
|
||||
|
||||
@AfterEach
|
||||
void resetSettings() throws ReflectiveOperationException
|
||||
{
|
||||
AssetLoadingHooksTest.settings(ForgeSettings.DEFAULTS);
|
||||
}
|
||||
|
||||
/** One test: the first call with the feature on grows the pool once per JVM. */
|
||||
@Test
|
||||
void growsTheLoaderPoolOnlyWithTheFeature() throws ReflectiveOperationException
|
||||
{
|
||||
FileSystemImpl fileSystem = new FileSystemImpl();
|
||||
ThreadPoolExecutor pool = AssetLoadingHooksTest.pool(fileSystem);
|
||||
int vanilla = Runtime.getRuntime().availableProcessors() <= 4 ? 2 : 4;
|
||||
int expected = Math.max(vanilla, Math.max(2, Runtime.getRuntime().availableProcessors() - 1));
|
||||
|
||||
try
|
||||
{
|
||||
assertEquals(vanilla, pool.getMaximumPoolSize());
|
||||
|
||||
AssetLoadingHooks.updateAsyncTransactions(fileSystem);
|
||||
assertEquals(vanilla, pool.getCorePoolSize());
|
||||
assertEquals(vanilla, pool.getMaximumPoolSize());
|
||||
|
||||
AssetLoadingHooksTest.settings(new ForgeSettings(Duration.ofMillis(2), Duration.ofSeconds(10), Set.of(Feature.EXPERIMENTAL_PERFORMANCE)));
|
||||
AssetLoadingHooks.updateAsyncTransactions(fileSystem);
|
||||
AssetLoadingHooks.updateAsyncTransactions(fileSystem);
|
||||
assertEquals(expected, pool.getCorePoolSize());
|
||||
assertEquals(expected, pool.getMaximumPoolSize());
|
||||
}
|
||||
finally { pool.shutdownNow(); }
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user