Skip to content

Commit 3864770

Browse files
committed
perf: slightly more perf
1 parent 7b58b1d commit 3864770

File tree

3 files changed

+34
-2
lines changed

3 files changed

+34
-2
lines changed

c2me-rewrites-chunk-system/src/main/java/com/ishland/c2me/rewrites/chunksystem/common/structs/ChunkSystemExecutors.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
public class ChunkSystemExecutors {
1212

13-
private static final ThreadLocal<Queue<Runnable>> CONSOLIDATING_QUEUE = new ThreadLocal<>();
13+
public static final ThreadLocal<Queue<Runnable>> CONSOLIDATING_QUEUE = new ThreadLocal<>();
1414

1515
public static final Executor backingBackgroundExecutor = GlobalExecutors.prioritizedScheduler.executor(15);
1616
public static final Scheduler backgroundScheduler = Schedulers.from(backingBackgroundExecutor);

c2me-rewrites-chunk-system/src/main/java/com/ishland/c2me/rewrites/chunksystem/mixin/MixinServerChunkManager.java

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,11 @@
22

33
import com.ishland.c2me.base.mixin.access.IThreadedAnvilChunkStorage;
44
import com.ishland.c2me.rewrites.chunksystem.common.ducks.IChunkSystemAccess;
5+
import com.ishland.c2me.rewrites.chunksystem.common.structs.ChunkSystemExecutors;
56
import com.llamalad7.mixinextras.injector.wrapoperation.Operation;
67
import com.llamalad7.mixinextras.injector.wrapoperation.WrapOperation;
78
import net.minecraft.server.world.ChunkHolder;
9+
import net.minecraft.server.world.ChunkLevelManager;
810
import net.minecraft.server.world.OptionalChunk;
911
import net.minecraft.server.world.ServerChunkLoadingManager;
1012
import net.minecraft.server.world.ServerChunkManager;
@@ -24,6 +26,8 @@
2426
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
2527
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;
2628

29+
import java.util.ArrayDeque;
30+
import java.util.Queue;
2731
import java.util.concurrent.CompletableFuture;
2832

2933
@Mixin(ServerChunkManager.class)
@@ -85,4 +89,32 @@ public String getDebugString() {
8589
return Integer.toString(((IChunkSystemAccess) this.chunkLoadingManager).c2me$getTheChunkSystem().itemCount()) + ", " + Integer.toString(this.getLoadedChunkCount());
8690
}
8791

92+
@WrapOperation(method = "updateChunks", at = @At(value = "INVOKE", target = "Lnet/minecraft/server/world/ChunkLevelManager;update(Lnet/minecraft/server/world/ServerChunkLoadingManager;)Z"))
93+
private boolean consolidateSchedules(ChunkLevelManager instance, ServerChunkLoadingManager chunkLoadingManager, Operation<Boolean> original) {
94+
Queue<Runnable> runnables = ChunkSystemExecutors.CONSOLIDATING_QUEUE.get();
95+
if (runnables != null) {
96+
new Throwable("CONSOLIDATING_QUEUE leak").printStackTrace();
97+
return original.call(instance, chunkLoadingManager);
98+
}
99+
100+
ChunkSystemExecutors.CONSOLIDATING_QUEUE.set(runnables = new ArrayDeque<>());
101+
try {
102+
return original.call(instance, chunkLoadingManager);
103+
} finally {
104+
Queue<Runnable> finalRunnables = runnables;
105+
if (!finalRunnables.isEmpty()) {
106+
ChunkSystemExecutors.backingBackgroundExecutor.execute(() -> {
107+
while (!finalRunnables.isEmpty()) {
108+
try {
109+
finalRunnables.remove().run();
110+
} catch (Throwable t) {
111+
t.printStackTrace();
112+
}
113+
}
114+
});
115+
}
116+
ChunkSystemExecutors.CONSOLIDATING_QUEUE.remove();
117+
}
118+
}
119+
88120
}

0 commit comments

Comments
 (0)