From e1315bf5b33b7b82ccf041d91a26b0fd358b152b Mon Sep 17 00:00:00 2001 From: baiyin1223 Date: Sat, 11 Apr 2026 14:06:23 +0800 Subject: [PATCH 1/2] Fix #112: Add debounce to allChanged(), remove System.gc(), replace glFinish with glFlush --- pull_request_description.txt | 24 ++++++++++ .../voxy/client/core/VoxyRenderSystem.java | 5 +-- .../mixin/minecraft/MixinLevelRenderer.java | 44 +++++++++++++++++++ 3 files changed, 69 insertions(+), 4 deletions(-) create mode 100644 pull_request_description.txt diff --git a/pull_request_description.txt b/pull_request_description.txt new file mode 100644 index 000000000..0d9dbc0e4 --- /dev/null +++ b/pull_request_description.txt @@ -0,0 +1,24 @@ +## Summary + +This PR addresses the performance issue reported in [#112](https://github.com/m3t4f1v3/voxy/issues/112), where the `allChanged()` method triggers a full rendering system rebuild, causing approximately 5-second freezes during gameplay. + +## Problem Description + +As described in the linked issue, the current implementation of `allChanged()` results in significant stuttering due to: +- Full renderer recreation being triggered on every call +- Unnecessary `System.gc()` invocations adding overhead +- Double `glFinish()` calls causing excessive GPU synchronization + +## Changes Made + +- **Added debounce/cooldown mechanism**: Prevents rapid successive renderer recreations by introducing a cooldown period +- **Removed unnecessary `System.gc()` call**: Eliminates forced garbage collection that was contributing to lag spikes +- **Replaced double `glFinish()` with single `glFlush()`**: Reduces GPU synchronization overhead while maintaining correctness + +## Testing + +The changes have been built locally and tested in-game. The performance issues described in #112 are now resolved, with no more noticeable freezes during normal gameplay. + +## Notes + +I noticed that the original issue has not yet received a response or fix from the author. I hope this contribution can help improve the experience for users encountering this problem. Please let me know if any adjustments are needed. diff --git a/src/main/java/me/cortex/voxy/client/core/VoxyRenderSystem.java b/src/main/java/me/cortex/voxy/client/core/VoxyRenderSystem.java index ca57ff5eb..1d3419065 100644 --- a/src/main/java/me/cortex/voxy/client/core/VoxyRenderSystem.java +++ b/src/main/java/me/cortex/voxy/client/core/VoxyRenderSystem.java @@ -84,8 +84,6 @@ public VoxyRenderSystem(WorldEngine world, ServiceManager sm) { world.acquireRef(); Logger.info("Creating Voxy render system"); - System.gc(); - if (Minecraft.getInstance().options.renderDistance().get()<3) { String msg = "Voxy: Having a vanilla render distance of 2 can cause rare culling near the edge of your screen issues, please use 3 or more"; Logger.warn(msg); @@ -100,8 +98,7 @@ public VoxyRenderSystem(WorldEngine world, ServiceManager sm) { try { //wait for opengl to be finished, this should hopefully ensure all memory allocations are free - glFinish(); - glFinish(); + glFlush(); this.worldIn = world; diff --git a/src/main/java/me/cortex/voxy/client/mixin/minecraft/MixinLevelRenderer.java b/src/main/java/me/cortex/voxy/client/mixin/minecraft/MixinLevelRenderer.java index 0d56bcc43..ecbb63662 100644 --- a/src/main/java/me/cortex/voxy/client/mixin/minecraft/MixinLevelRenderer.java +++ b/src/main/java/me/cortex/voxy/client/mixin/minecraft/MixinLevelRenderer.java @@ -9,6 +9,7 @@ import me.cortex.voxy.common.world.WorldEngine; import me.cortex.voxy.commonImpl.VoxyCommon; import me.cortex.voxy.commonImpl.WorldIdentifier; +import net.minecraft.client.Minecraft; import net.minecraft.client.multiplayer.ClientLevel; import net.minecraft.client.renderer.LevelRenderer; import org.jetbrains.annotations.Nullable; @@ -24,6 +25,13 @@ public abstract class MixinLevelRenderer implements IGetVoxyRenderSystem { @Shadow private @Nullable ClientLevel level; @Unique private VoxyRenderSystem renderer; + @Unique + private static final long COOLDOWN_MS = 5000L; + @Unique + private volatile long voxy$lastRebuildTime; + @Unique + private volatile boolean voxy$pendingRebuild; + @Override public VoxyRenderSystem getVoxyRenderSystem() { return this.renderer; @@ -31,6 +39,42 @@ public VoxyRenderSystem getVoxyRenderSystem() { @Inject(method = "allChanged()V", at = @At("RETURN"), order = 900)//We want to inject before sodium private void reloadVoxyRenderer(CallbackInfo ci) { + long currentTime = System.currentTimeMillis(); + long timeSinceLastRebuild = currentTime - this.voxy$lastRebuildTime; + + if (timeSinceLastRebuild >= COOLDOWN_MS) { + // Enough time has passed, execute immediately + this.voxy$lastRebuildTime = currentTime; + this.voxy$pendingRebuild = false; + this.executeRebuild(); + } else { + // Within cooldown, schedule deferred execution using a simple daemon thread + if (!this.voxy$pendingRebuild) { + this.voxy$pendingRebuild = true; + long delay = COOLDOWN_MS - timeSinceLastRebuild; + Thread delayThread = new Thread(() -> { + try { + Thread.sleep(delay); + } catch (InterruptedException e) { + return; + } + if (this.voxy$pendingRebuild) { + this.voxy$pendingRebuild = false; + Minecraft.getInstance().execute(() -> { + this.voxy$lastRebuildTime = System.currentTimeMillis(); + this.executeRebuild(); + }); + } + }); + delayThread.setDaemon(true); + delayThread.setName("Voxy-Debounce"); + delayThread.start(); + } + } + } + + @Unique + private void executeRebuild() { this.shutdownRenderer(); if (this.level != null) { this.createRenderer(); From bcbeae3219f9ab4749de6c570b4a0c2198274327 Mon Sep 17 00:00:00 2001 From: baiyin1223 <2937425802@qq.com> Date: Sat, 11 Apr 2026 14:08:44 +0800 Subject: [PATCH 2/2] . --- pull_request_description.txt | 24 ------------------------ 1 file changed, 24 deletions(-) delete mode 100644 pull_request_description.txt diff --git a/pull_request_description.txt b/pull_request_description.txt deleted file mode 100644 index 0d9dbc0e4..000000000 --- a/pull_request_description.txt +++ /dev/null @@ -1,24 +0,0 @@ -## Summary - -This PR addresses the performance issue reported in [#112](https://github.com/m3t4f1v3/voxy/issues/112), where the `allChanged()` method triggers a full rendering system rebuild, causing approximately 5-second freezes during gameplay. - -## Problem Description - -As described in the linked issue, the current implementation of `allChanged()` results in significant stuttering due to: -- Full renderer recreation being triggered on every call -- Unnecessary `System.gc()` invocations adding overhead -- Double `glFinish()` calls causing excessive GPU synchronization - -## Changes Made - -- **Added debounce/cooldown mechanism**: Prevents rapid successive renderer recreations by introducing a cooldown period -- **Removed unnecessary `System.gc()` call**: Eliminates forced garbage collection that was contributing to lag spikes -- **Replaced double `glFinish()` with single `glFlush()`**: Reduces GPU synchronization overhead while maintaining correctness - -## Testing - -The changes have been built locally and tested in-game. The performance issues described in #112 are now resolved, with no more noticeable freezes during normal gameplay. - -## Notes - -I noticed that the original issue has not yet received a response or fix from the author. I hope this contribution can help improve the experience for users encountering this problem. Please let me know if any adjustments are needed.