Skip to content

Commit

Permalink
switch some things to fastutil (#54)
Browse files Browse the repository at this point in the history
Co-authored-by: ev chang <[email protected]>
  • Loading branch information
MicrocontrollersDev and Wyvest authored Jul 17, 2024
1 parent 8247911 commit 37f2ea7
Show file tree
Hide file tree
Showing 3 changed files with 177 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
package club.sk1er.patcher.mixins.performance.forge;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.MessageToMessageCodec;
import it.unimi.dsi.fastutil.bytes.Byte2ObjectMap;
import it.unimi.dsi.fastutil.bytes.Byte2ObjectOpenHashMap;
import it.unimi.dsi.fastutil.objects.Object2ByteMap;
import it.unimi.dsi.fastutil.objects.Object2ByteOpenHashMap;
import net.minecraft.network.PacketBuffer;
import net.minecraftforge.fml.common.FMLLog;
import net.minecraftforge.fml.common.network.FMLIndexedMessageToMessageCodec;
import net.minecraftforge.fml.common.network.NetworkRegistry;
import net.minecraftforge.fml.common.network.internal.FMLProxyPacket;
import org.apache.logging.log4j.Level;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Overwrite;
import org.spongepowered.asm.mixin.Shadow;
import org.spongepowered.asm.mixin.Unique;

import java.lang.ref.WeakReference;
import java.util.List;

import static net.minecraftforge.fml.common.network.FMLIndexedMessageToMessageCodec.INBOUNDPACKETTRACKER;

@Mixin(value = FMLIndexedMessageToMessageCodec.class, remap = false)
public abstract class FMLIndexedMessageToMessageCodecMixin_FastUtil<A> extends MessageToMessageCodec<FMLProxyPacket, A> {
//#if MC==10809
@Shadow
public abstract void encodeInto(ChannelHandlerContext ctx, A msg, ByteBuf target) throws Exception;
@Shadow
protected abstract void testMessageValidity(FMLProxyPacket msg);
@Shadow
public abstract void decodeInto(ChannelHandlerContext ctx, ByteBuf source, A msg);
@Unique
private final Byte2ObjectMap<Class<? extends A>> patcher$discriminators = new Byte2ObjectOpenHashMap<>();
@Unique
private final Object2ByteMap<Class<? extends A>> patcher$types = new Object2ByteOpenHashMap<>();

/**
* @author Microcontrollers
* @reason Use FastUtil
*/
@Overwrite
public FMLIndexedMessageToMessageCodec<A> addDiscriminator(int discriminator, Class<? extends A> type) {
patcher$discriminators.put((byte)discriminator, type);
patcher$types.put(type, (byte)discriminator);
return (FMLIndexedMessageToMessageCodec) (Object) this;
}

/**
* @author Microcontrollers
* @reason Use FastUtil
*/
@Overwrite
@Override
protected final void encode(ChannelHandlerContext ctx, A msg, List<Object> out) throws Exception {
PacketBuffer buffer = new PacketBuffer(Unpooled.buffer());
@SuppressWarnings("unchecked") // Stupid unnecessary cast I can't seem to kill
Class<? extends A> clazz = (Class<? extends A>) msg.getClass();
byte discriminator = patcher$types.get(clazz);
buffer.writeByte(discriminator);
this.encodeInto(ctx, msg, buffer);
FMLProxyPacket proxy = new FMLProxyPacket(buffer/*.copy()*/, ctx.channel().attr(NetworkRegistry.FML_CHANNEL).get());
WeakReference<FMLProxyPacket> ref = ctx.attr(INBOUNDPACKETTRACKER).get().get();
FMLProxyPacket old = ref == null ? null : ref.get();
if (old != null) {
proxy.setDispatcher(old.getDispatcher());
}
out.add(proxy);
}

/**
* @author Microcontrollers
* @reason Use FastUtil
*/
@Overwrite
@Override
protected final void decode(ChannelHandlerContext ctx, FMLProxyPacket msg, List<Object> out) throws Exception {
this.testMessageValidity(msg);
ByteBuf payload = msg.payload().duplicate();
if (payload.readableBytes() < 1) {
FMLLog.log(Level.ERROR, "The FMLIndexedCodec has received an empty buffer on channel %s, likely a result of a LAN server issue. Pipeline parts : %s", ctx.channel().attr(NetworkRegistry.FML_CHANNEL), ctx.pipeline().toString());
}
byte discriminator = payload.readByte();
Class<? extends A> clazz = patcher$discriminators.get(discriminator);
if(clazz == null) {
throw new NullPointerException("Undefined message for discriminator " + discriminator + " in channel " + msg.channel());
}
A newMsg = clazz.newInstance();
ctx.attr(INBOUNDPACKETTRACKER).get().set(new WeakReference<FMLProxyPacket>(msg));
this.decodeInto(ctx, payload.slice(), newMsg);
out.add(newMsg);
}
//#endif
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
package club.sk1er.patcher.mixins.performance.forge;

import com.google.common.collect.Maps;
import it.unimi.dsi.fastutil.ints.Int2ObjectMap;
import it.unimi.dsi.fastutil.ints.Int2ObjectOpenHashMap;
import net.minecraft.client.renderer.ItemModelMesher;
import net.minecraft.client.resources.model.IBakedModel;
import net.minecraft.client.resources.model.ModelManager;
//#if MC==10809
import net.minecraft.client.resources.model.ModelResourceLocation;
//#endif
import net.minecraft.item.Item;
import net.minecraftforge.client.ItemModelMesherForge;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Unique;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;

import java.util.Map;

@Mixin(ItemModelMesherForge.class)
public class ItemModelMesherForgeMixin_FastUtil extends ItemModelMesher {
public ItemModelMesherForgeMixin_FastUtil(ModelManager modelManager) {
super(modelManager);
}

//#if MC==10809
@Unique
final Map<Item, Int2ObjectMap<ModelResourceLocation>> patcher$locations = Maps.newIdentityHashMap();
@Unique
final Map<Item, Int2ObjectMap<IBakedModel>> patcher$models = Maps.newIdentityHashMap();

@Inject(method = "getItemModel", at = @At("HEAD"), cancellable = true)
private void patcher$fastUtilGetModel(Item item, int meta, CallbackInfoReturnable<IBakedModel> cir) {
Int2ObjectMap<IBakedModel> map = patcher$models.get(item);
cir.setReturnValue(map == null ? null : map.get(meta));
}

@Inject(method = "register", at = @At("HEAD"), cancellable = true)
private void patcher$fastUtilRegisterModel(Item item, int meta, ModelResourceLocation location, CallbackInfo ci) {
Int2ObjectMap<ModelResourceLocation> locs = patcher$locations.get(item);
Int2ObjectMap<IBakedModel> mods = patcher$models.get(item);
if (locs == null) {
locs = new Int2ObjectOpenHashMap<>();
patcher$locations.put(item, locs);
}
if (mods == null) {
mods = new Int2ObjectOpenHashMap<>();
patcher$models.put(item, mods);
}
locs.put(meta, location);
mods.put(meta, this.getModelManager().getModel(location));
ci.cancel();
}

@Inject(method = "rebuildCache", at = @At("HEAD"), cancellable = true)
private void patcher$fastUtilRebuildCache(CallbackInfo ci) {
final ModelManager manager = this.getModelManager();
for (Map.Entry<Item, Int2ObjectMap<ModelResourceLocation>> e : patcher$locations.entrySet()) {
Int2ObjectMap<IBakedModel> mods = patcher$models.get(e.getKey());
if (mods != null) {
mods.clear();
}
else {
mods = new Int2ObjectOpenHashMap<>();
patcher$models.put(e.getKey(), mods);
}
final Int2ObjectMap<IBakedModel> map = mods;
for (Int2ObjectMap.Entry<ModelResourceLocation> entry : e.getValue().int2ObjectEntrySet()) {
map.put(entry.getIntKey(), manager.getModel(entry.getValue()));
}
}
ci.cancel();
}
//#endif
}
2 changes: 2 additions & 0 deletions src/main/resources/mixins.patcher.json
Original file line number Diff line number Diff line change
Expand Up @@ -262,9 +262,11 @@
"performance.forge.FMLClientHandlerMixin_GetErrors",
"performance.forge.FMLClientHandlerMixin_Optimization",
"performance.forge.FMLControlledNamespacedRegistryMixin_TrimAvailabilityMap",
"performance.forge.FMLIndexedMessageToMessageCodecMixin_FastUtil",
"performance.forge.ForgeBlockStateV1Mixin_OptimizeTransforms",
"performance.forge.ForgeHooksClientMixin_OptimizeTransforms",
"performance.forge.ItemLayerModelMixin_ReduceQuadCount",
"performance.forge.ItemModelMesherForgeMixin_FastUtil",
"performance.forge.JarDiscovererMixin_ReplaceRegex",
"performance.forge.LightUtilMixin_OptimizeNormals",
"performance.forge.ModDiscovererMixin_Logging",
Expand Down

0 comments on commit 37f2ea7

Please sign in to comment.