ServerChunkManager - Alloc reduction

This commit is contained in:
Taiyou06
2024-07-06 07:54:01 +03:00
parent 870c519bc3
commit 8314c4b05f
2 changed files with 41 additions and 1 deletions

View File

@@ -0,0 +1,39 @@
package net.gensokyoreimagined.nitori.core;
import net.minecraft.server.level.ChunkHolder;
import net.minecraft.server.level.ServerChunkCache;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.Redirect;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
import java.util.ArrayList;
import java.util.function.BooleanSupplier;
@Mixin(ServerChunkCache.class)
public class ServerChunkManagerMixin {
private final ArrayList<ChunkHolder> cachedChunkList = new ArrayList<>();
@Redirect(
method = "tickChunks()V",
at = @At(
value = "INVOKE",
target = "Lcom/google/common/collect/Lists;newArrayListWithCapacity(I)Ljava/util/ArrayList;",
remap = false
)
)
private ArrayList<ChunkHolder> redirectChunksListClone(int initialArraySize) {
ArrayList<ChunkHolder> list = this.cachedChunkList;
list.clear(); // Ensure the list is empty before re-using it
list.ensureCapacity(initialArraySize);
return list;
}
@Inject(method = "tick(Ljava/util/function/BooleanSupplier;Z)V", at = @At("HEAD"))
private void preTick(BooleanSupplier shouldKeepTicking, boolean tickChunks, CallbackInfo ci) {
// Ensure references aren't leaked through this list
this.cachedChunkList.clear();
}
}

View File

@@ -31,6 +31,7 @@
"MixinPlayerList",
"MixinCraftPlayer",
"StateMixin",
"WorldMixin"
"WorldMixin",
"ServerChunkManagerMixin"
]
}