Skip to content

Commit

Permalink
feat: Optimise GLX initialization
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
Steveplays28 committed Aug 7, 2024
1 parent 208dce1 commit 7211243
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -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();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
}
}
1 change: 1 addition & 0 deletions common/src/main/resources/blinkload-common.mixins.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
"client.MinecraftClientMixin",
"client.SpriteLoaderMixin",
"client.accessor.SpriteContentsAccessor",
"client.blaze3d.platform.GLXMixin",
"client.main.ClientMainMixin"
],
"injectors": {
Expand Down

0 comments on commit 7211243

Please sign in to comment.