From 72112432e7ce35b5dac7ea4fa62a6ce214c2fca0 Mon Sep 17 00:00:00 2001 From: Steveplays28 Date: Wed, 7 Aug 2024 11:34:40 +0200 Subject: [PATCH] feat: Optimise GLX initialization System information is now fetched off-thread and only supplied when it's fetched and a call to `RenderSystem#assertInInitPhase` is skipped with an `Integer` comparison. This saves ~500ms on my system. --- .../client/blaze3d/platform/GLXMixin.java | 42 +++++++++++++++++++ .../blinkload/util/ThreadUtil.java | 6 +++ .../resources/blinkload-common.mixins.json | 1 + 3 files changed, 49 insertions(+) create mode 100644 common/src/main/java/io/github/steveplays28/blinkload/mixin/client/blaze3d/platform/GLXMixin.java diff --git a/common/src/main/java/io/github/steveplays28/blinkload/mixin/client/blaze3d/platform/GLXMixin.java b/common/src/main/java/io/github/steveplays28/blinkload/mixin/client/blaze3d/platform/GLXMixin.java new file mode 100644 index 0000000..2ca7f7f --- /dev/null +++ b/common/src/main/java/io/github/steveplays28/blinkload/mixin/client/blaze3d/platform/GLXMixin.java @@ -0,0 +1,42 @@ +package io.github.steveplays28.blinkload.mixin.client.blaze3d.platform; + +import com.mojang.blaze3d.platform.GLX; +import io.github.steveplays28.blinkload.util.ThreadUtil; +import net.fabricmc.api.EnvType; +import net.fabricmc.api.Environment; +import net.minecraft.client.gl.GlDebug; +import org.jetbrains.annotations.NotNull; +import org.spongepowered.asm.mixin.Mixin; +import org.spongepowered.asm.mixin.Shadow; +import org.spongepowered.asm.mixin.injection.At; +import org.spongepowered.asm.mixin.injection.Inject; +import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; +import oshi.SystemInfo; +import oshi.hardware.CentralProcessor; + +import java.util.Locale; +import java.util.concurrent.CompletableFuture; + +@Environment(EnvType.CLIENT) +@Mixin(GLX.class) +public class GLXMixin { + @Shadow + private static String cpuInfo; + + @Inject(method = "_init", at = @At(value = "NEW", target = "()Loshi/SystemInfo;"), cancellable = true) + private static void blinkload$optimizeSystemInfoFetch(int debugVerbosity, boolean debugSync, @NotNull CallbackInfo ci) { + CompletableFuture.supplyAsync(() -> { + CentralProcessor centralProcessor = new SystemInfo().getHardware().getProcessor(); + return String.format( + Locale.ROOT, "%dx %s", centralProcessor.getLogicalProcessorCount(), + centralProcessor.getProcessorIdentifier().getName() + ).replaceAll("\\s+", " "); + }, ThreadUtil.getGLXInitializerThreadPoolExecutor()).whenComplete((string, throwable) -> cpuInfo = string); + + if (debugVerbosity > 0) { + GlDebug.enableDebug(debugVerbosity, debugSync); + } + + ci.cancel(); + } +} diff --git a/common/src/main/java/io/github/steveplays28/blinkload/util/ThreadUtil.java b/common/src/main/java/io/github/steveplays28/blinkload/util/ThreadUtil.java index d78871c..37c2c6d 100644 --- a/common/src/main/java/io/github/steveplays28/blinkload/util/ThreadUtil.java +++ b/common/src/main/java/io/github/steveplays28/blinkload/util/ThreadUtil.java @@ -14,6 +14,8 @@ public class ThreadUtil { 1, new ThreadFactoryBuilder().setNameFormat(String.format("%s Atlas Texture Loader", BlinkLoad.MOD_NAME)).build()); private static final @NotNull Executor BOOTSTRAP_INITIALIZER_THREAD_POOL_EXECUTOR = Executors.newFixedThreadPool( 1, new ThreadFactoryBuilder().setNameFormat(BOOTSTRAPPER_THREAD_POOL_NAME).build()); + private static final @NotNull Executor GLX_INITIALIXER_THREAD_POOL_EXECUTOR = Executors.newFixedThreadPool( + 1, new ThreadFactoryBuilder().setNameFormat(String.format("%s GLX Initializer", BlinkLoad.MOD_NAME)).build()); public static @NotNull Executor getAtlasTextureLoaderThreadPoolExecutor() { return ATLAS_TEXTURE_LOADER_THREAD_POOL_EXECUTOR; @@ -22,4 +24,8 @@ public class ThreadUtil { public static @NotNull Executor getBootstrapInitializerThreadPoolExecutor() { return BOOTSTRAP_INITIALIZER_THREAD_POOL_EXECUTOR; } + + public static @NotNull Executor getGLXInitializerThreadPoolExecutor() { + return GLX_INITIALIXER_THREAD_POOL_EXECUTOR; + } } diff --git a/common/src/main/resources/blinkload-common.mixins.json b/common/src/main/resources/blinkload-common.mixins.json index 5e9ef91..1711bbf 100644 --- a/common/src/main/resources/blinkload-common.mixins.json +++ b/common/src/main/resources/blinkload-common.mixins.json @@ -13,6 +13,7 @@ "client.MinecraftClientMixin", "client.SpriteLoaderMixin", "client.accessor.SpriteContentsAccessor", + "client.blaze3d.platform.GLXMixin", "client.main.ClientMainMixin" ], "injectors": {