From b933246a950f87c92702934ca7588db048239622 Mon Sep 17 00:00:00 2001 From: fullwall Date: Wed, 29 Dec 2021 00:30:53 +0800 Subject: [PATCH] Implement a surprisingly missed optimisation --- .../nms/v1_10_R1/entity/MobEntityController.java | 9 ++++----- .../nms/v1_11_R1/entity/MobEntityController.java | 9 ++++----- .../nms/v1_12_R1/entity/MobEntityController.java | 8 ++++---- .../nms/v1_13_R2/entity/MobEntityController.java | 9 ++++----- .../nms/v1_14_R1/entity/MobEntityController.java | 9 ++++----- .../nms/v1_15_R1/entity/MobEntityController.java | 9 ++++----- .../nms/v1_16_R3/entity/MobEntityController.java | 8 ++++---- .../nms/v1_17_R1/entity/MobEntityController.java | 8 ++++---- .../nms/v1_18_R1/entity/MobEntityController.java | 8 ++++---- .../nms/v1_8_R3/entity/MobEntityController.java | 8 ++++---- 10 files changed, 40 insertions(+), 45 deletions(-) diff --git a/v1_10_R1/src/main/java/net/citizensnpcs/nms/v1_10_R1/entity/MobEntityController.java b/v1_10_R1/src/main/java/net/citizensnpcs/nms/v1_10_R1/entity/MobEntityController.java index 2966f549a..1ffb11c69 100644 --- a/v1_10_R1/src/main/java/net/citizensnpcs/nms/v1_10_R1/entity/MobEntityController.java +++ b/v1_10_R1/src/main/java/net/citizensnpcs/nms/v1_10_R1/entity/MobEntityController.java @@ -3,14 +3,13 @@ package net.citizensnpcs.nms.v1_10_R1.entity; import java.lang.invoke.MethodHandle; import java.lang.reflect.Constructor; import java.util.Map; +import java.util.WeakHashMap; import org.bukkit.Location; import org.bukkit.block.BlockFace; import org.bukkit.craftbukkit.v1_10_R1.CraftWorld; import org.bukkit.entity.Entity; -import com.google.common.collect.Maps; - import net.citizensnpcs.api.npc.NPC; import net.citizensnpcs.npc.AbstractEntityController; import net.citizensnpcs.util.NMS; @@ -58,14 +57,14 @@ public abstract class MobEntityController extends AbstractEntityController { if (constructor != null) return constructor; try { - return clazz.getConstructor(World.class, NPC.class); + CONSTRUCTOR_CACHE.put(clazz, constructor = clazz.getConstructor(World.class, NPC.class)); + return constructor; } catch (Exception ex) { throw new IllegalStateException("unable to find an entity constructor"); } } - private static final Map, Constructor> CONSTRUCTOR_CACHE = Maps.newHashMap(); - + private static final Map, Constructor> CONSTRUCTOR_CACHE = new WeakHashMap, Constructor>(); private static final MethodHandle UUID_FIELD = NMS.getSetter(net.minecraft.server.v1_10_R1.Entity.class, "uniqueID"); } \ No newline at end of file diff --git a/v1_11_R1/src/main/java/net/citizensnpcs/nms/v1_11_R1/entity/MobEntityController.java b/v1_11_R1/src/main/java/net/citizensnpcs/nms/v1_11_R1/entity/MobEntityController.java index 237eb9c2a..2a7cf3d62 100644 --- a/v1_11_R1/src/main/java/net/citizensnpcs/nms/v1_11_R1/entity/MobEntityController.java +++ b/v1_11_R1/src/main/java/net/citizensnpcs/nms/v1_11_R1/entity/MobEntityController.java @@ -3,14 +3,13 @@ package net.citizensnpcs.nms.v1_11_R1.entity; import java.lang.invoke.MethodHandle; import java.lang.reflect.Constructor; import java.util.Map; +import java.util.WeakHashMap; import org.bukkit.Location; import org.bukkit.block.BlockFace; import org.bukkit.craftbukkit.v1_11_R1.CraftWorld; import org.bukkit.entity.Entity; -import com.google.common.collect.Maps; - import net.citizensnpcs.api.npc.NPC; import net.citizensnpcs.npc.AbstractEntityController; import net.citizensnpcs.util.NMS; @@ -58,14 +57,14 @@ public abstract class MobEntityController extends AbstractEntityController { if (constructor != null) return constructor; try { - return clazz.getConstructor(World.class, NPC.class); + CONSTRUCTOR_CACHE.put(clazz, constructor = clazz.getConstructor(World.class, NPC.class)); + return constructor; } catch (Exception ex) { throw new IllegalStateException("unable to find an entity constructor"); } } - private static final Map, Constructor> CONSTRUCTOR_CACHE = Maps.newHashMap(); - + private static final Map, Constructor> CONSTRUCTOR_CACHE = new WeakHashMap, Constructor>(); private static final MethodHandle UUID_FIELD = NMS.getSetter(net.minecraft.server.v1_11_R1.Entity.class, "uniqueID"); } \ No newline at end of file diff --git a/v1_12_R1/src/main/java/net/citizensnpcs/nms/v1_12_R1/entity/MobEntityController.java b/v1_12_R1/src/main/java/net/citizensnpcs/nms/v1_12_R1/entity/MobEntityController.java index 645098c8d..5c54eb546 100644 --- a/v1_12_R1/src/main/java/net/citizensnpcs/nms/v1_12_R1/entity/MobEntityController.java +++ b/v1_12_R1/src/main/java/net/citizensnpcs/nms/v1_12_R1/entity/MobEntityController.java @@ -3,14 +3,13 @@ package net.citizensnpcs.nms.v1_12_R1.entity; import java.lang.invoke.MethodHandle; import java.lang.reflect.Constructor; import java.util.Map; +import java.util.WeakHashMap; import org.bukkit.Location; import org.bukkit.block.BlockFace; import org.bukkit.craftbukkit.v1_12_R1.CraftWorld; import org.bukkit.entity.Entity; -import com.google.common.collect.Maps; - import net.citizensnpcs.api.npc.NPC; import net.citizensnpcs.npc.AbstractEntityController; import net.citizensnpcs.util.NMS; @@ -59,13 +58,14 @@ public abstract class MobEntityController extends AbstractEntityController { if (constructor != null) return constructor; try { - return clazz.getConstructor(World.class, NPC.class); + CONSTRUCTOR_CACHE.put(clazz, constructor = clazz.getConstructor(World.class, NPC.class)); + return constructor; } catch (Exception ex) { throw new IllegalStateException("unable to find an entity constructor"); } } - private static final Map, Constructor> CONSTRUCTOR_CACHE = Maps.newHashMap(); + private static final Map, Constructor> CONSTRUCTOR_CACHE = new WeakHashMap, Constructor>(); private static final MethodHandle UUID_FIELD = NMS.getSetter(net.minecraft.server.v1_12_R1.Entity.class, "uniqueID"); } \ No newline at end of file diff --git a/v1_13_R2/src/main/java/net/citizensnpcs/nms/v1_13_R2/entity/MobEntityController.java b/v1_13_R2/src/main/java/net/citizensnpcs/nms/v1_13_R2/entity/MobEntityController.java index 85b52b2d2..2a9376465 100644 --- a/v1_13_R2/src/main/java/net/citizensnpcs/nms/v1_13_R2/entity/MobEntityController.java +++ b/v1_13_R2/src/main/java/net/citizensnpcs/nms/v1_13_R2/entity/MobEntityController.java @@ -3,14 +3,13 @@ package net.citizensnpcs.nms.v1_13_R2.entity; import java.lang.invoke.MethodHandle; import java.lang.reflect.Constructor; import java.util.Map; +import java.util.WeakHashMap; import org.bukkit.Location; import org.bukkit.block.BlockFace; import org.bukkit.craftbukkit.v1_13_R2.CraftWorld; import org.bukkit.entity.Entity; -import com.google.common.collect.Maps; - import net.citizensnpcs.api.npc.NPC; import net.citizensnpcs.npc.AbstractEntityController; import net.citizensnpcs.util.NMS; @@ -58,14 +57,14 @@ public abstract class MobEntityController extends AbstractEntityController { if (constructor != null) return constructor; try { - return clazz.getConstructor(World.class, NPC.class); + CONSTRUCTOR_CACHE.put(clazz, constructor = clazz.getConstructor(World.class, NPC.class)); + return constructor; } catch (Exception ex) { throw new IllegalStateException("unable to find an entity constructor"); } } - private static final Map, Constructor> CONSTRUCTOR_CACHE = Maps.newHashMap(); - + private static final Map, Constructor> CONSTRUCTOR_CACHE = new WeakHashMap, Constructor>(); private static final MethodHandle UUID_FIELD = NMS.getSetter(net.minecraft.server.v1_13_R2.Entity.class, "uniqueID"); } \ No newline at end of file diff --git a/v1_14_R1/src/main/java/net/citizensnpcs/nms/v1_14_R1/entity/MobEntityController.java b/v1_14_R1/src/main/java/net/citizensnpcs/nms/v1_14_R1/entity/MobEntityController.java index 850c1a357..df2968c8a 100644 --- a/v1_14_R1/src/main/java/net/citizensnpcs/nms/v1_14_R1/entity/MobEntityController.java +++ b/v1_14_R1/src/main/java/net/citizensnpcs/nms/v1_14_R1/entity/MobEntityController.java @@ -3,14 +3,13 @@ package net.citizensnpcs.nms.v1_14_R1.entity; import java.lang.invoke.MethodHandle; import java.lang.reflect.Constructor; import java.util.Map; +import java.util.WeakHashMap; import org.bukkit.Location; import org.bukkit.block.BlockFace; import org.bukkit.craftbukkit.v1_14_R1.CraftWorld; import org.bukkit.entity.Entity; -import com.google.common.collect.Maps; - import net.citizensnpcs.api.npc.NPC; import net.citizensnpcs.nms.v1_14_R1.util.NMSImpl; import net.citizensnpcs.npc.AbstractEntityController; @@ -63,14 +62,14 @@ public abstract class MobEntityController extends AbstractEntityController { if (constructor != null) return constructor; try { - return clazz.getConstructor(EntityTypes.class, World.class, NPC.class); + CONSTRUCTOR_CACHE.put(clazz, constructor = clazz.getConstructor(EntityTypes.class, World.class, NPC.class)); + return constructor; } catch (Exception ex) { throw new IllegalStateException("unable to find an entity constructor"); } } - private static final Map, Constructor> CONSTRUCTOR_CACHE = Maps.newHashMap(); - + private static final Map, Constructor> CONSTRUCTOR_CACHE = new WeakHashMap, Constructor>(); private static final MethodHandle UUID_FIELD = NMS.getSetter(net.minecraft.server.v1_14_R1.Entity.class, "uniqueID"); } \ No newline at end of file diff --git a/v1_15_R1/src/main/java/net/citizensnpcs/nms/v1_15_R1/entity/MobEntityController.java b/v1_15_R1/src/main/java/net/citizensnpcs/nms/v1_15_R1/entity/MobEntityController.java index 658a9b58b..d34d30c33 100644 --- a/v1_15_R1/src/main/java/net/citizensnpcs/nms/v1_15_R1/entity/MobEntityController.java +++ b/v1_15_R1/src/main/java/net/citizensnpcs/nms/v1_15_R1/entity/MobEntityController.java @@ -3,14 +3,13 @@ package net.citizensnpcs.nms.v1_15_R1.entity; import java.lang.invoke.MethodHandle; import java.lang.reflect.Constructor; import java.util.Map; +import java.util.WeakHashMap; import org.bukkit.Location; import org.bukkit.block.BlockFace; import org.bukkit.craftbukkit.v1_15_R1.CraftWorld; import org.bukkit.entity.Entity; -import com.google.common.collect.Maps; - import net.citizensnpcs.api.npc.NPC; import net.citizensnpcs.nms.v1_15_R1.util.NMSImpl; import net.citizensnpcs.npc.AbstractEntityController; @@ -63,14 +62,14 @@ public abstract class MobEntityController extends AbstractEntityController { if (constructor != null) return constructor; try { - return clazz.getConstructor(EntityTypes.class, World.class, NPC.class); + CONSTRUCTOR_CACHE.put(clazz, constructor = clazz.getConstructor(EntityTypes.class, World.class, NPC.class)); + return constructor; } catch (Exception ex) { throw new IllegalStateException("unable to find an entity constructor"); } } - private static final Map, Constructor> CONSTRUCTOR_CACHE = Maps.newHashMap(); - + private static final Map, Constructor> CONSTRUCTOR_CACHE = new WeakHashMap, Constructor>(); private static final MethodHandle UUID_FIELD = NMS.getSetter(net.minecraft.server.v1_15_R1.Entity.class, "uniqueID"); } \ No newline at end of file diff --git a/v1_16_R3/src/main/java/net/citizensnpcs/nms/v1_16_R3/entity/MobEntityController.java b/v1_16_R3/src/main/java/net/citizensnpcs/nms/v1_16_R3/entity/MobEntityController.java index f018b6616..e2eaf1d95 100644 --- a/v1_16_R3/src/main/java/net/citizensnpcs/nms/v1_16_R3/entity/MobEntityController.java +++ b/v1_16_R3/src/main/java/net/citizensnpcs/nms/v1_16_R3/entity/MobEntityController.java @@ -3,14 +3,13 @@ package net.citizensnpcs.nms.v1_16_R3.entity; import java.lang.invoke.MethodHandle; import java.lang.reflect.Constructor; import java.util.Map; +import java.util.WeakHashMap; import org.bukkit.Location; import org.bukkit.block.BlockFace; import org.bukkit.craftbukkit.v1_16_R3.CraftWorld; import org.bukkit.entity.Entity; -import com.google.common.collect.Maps; - import net.citizensnpcs.api.npc.NPC; import net.citizensnpcs.nms.v1_16_R3.util.NMSImpl; import net.citizensnpcs.npc.AbstractEntityController; @@ -63,13 +62,14 @@ public abstract class MobEntityController extends AbstractEntityController { if (constructor != null) return constructor; try { - return clazz.getConstructor(EntityTypes.class, World.class, NPC.class); + CONSTRUCTOR_CACHE.put(clazz, constructor = clazz.getConstructor(EntityTypes.class, World.class, NPC.class)); + return constructor; } catch (Exception ex) { throw new IllegalStateException("unable to find an entity constructor"); } } - private static final Map, Constructor> CONSTRUCTOR_CACHE = Maps.newHashMap(); + private static final Map, Constructor> CONSTRUCTOR_CACHE = new WeakHashMap, Constructor>(); private static final MethodHandle UUID_FIELD = NMS.getSetter(net.minecraft.server.v1_16_R3.Entity.class, "uniqueID"); } \ No newline at end of file diff --git a/v1_17_R1/src/main/java/net/citizensnpcs/nms/v1_17_R1/entity/MobEntityController.java b/v1_17_R1/src/main/java/net/citizensnpcs/nms/v1_17_R1/entity/MobEntityController.java index 49a869ebf..bdfff3e87 100644 --- a/v1_17_R1/src/main/java/net/citizensnpcs/nms/v1_17_R1/entity/MobEntityController.java +++ b/v1_17_R1/src/main/java/net/citizensnpcs/nms/v1_17_R1/entity/MobEntityController.java @@ -2,14 +2,13 @@ package net.citizensnpcs.nms.v1_17_R1.entity; import java.lang.reflect.Constructor; import java.util.Map; +import java.util.WeakHashMap; import org.bukkit.Location; import org.bukkit.block.BlockFace; import org.bukkit.craftbukkit.v1_17_R1.CraftWorld; import org.bukkit.entity.Entity; -import com.google.common.collect.Maps; - import net.citizensnpcs.api.npc.NPC; import net.citizensnpcs.nms.v1_17_R1.util.NMSImpl; import net.citizensnpcs.npc.AbstractEntityController; @@ -57,11 +56,12 @@ public abstract class MobEntityController extends AbstractEntityController { if (constructor != null) return constructor; try { - return clazz.getConstructor(EntityType.class, Level.class, NPC.class); + CONSTRUCTOR_CACHE.put(clazz, constructor = clazz.getConstructor(EntityType.class, Level.class, NPC.class)); + return constructor; } catch (Exception ex) { throw new IllegalStateException("unable to find an entity constructor"); } } - private static final Map, Constructor> CONSTRUCTOR_CACHE = Maps.newHashMap(); + private static final Map, Constructor> CONSTRUCTOR_CACHE = new WeakHashMap, Constructor>(); } \ No newline at end of file diff --git a/v1_18_R1/src/main/java/net/citizensnpcs/nms/v1_18_R1/entity/MobEntityController.java b/v1_18_R1/src/main/java/net/citizensnpcs/nms/v1_18_R1/entity/MobEntityController.java index 2a5551a2c..4efbeacb2 100644 --- a/v1_18_R1/src/main/java/net/citizensnpcs/nms/v1_18_R1/entity/MobEntityController.java +++ b/v1_18_R1/src/main/java/net/citizensnpcs/nms/v1_18_R1/entity/MobEntityController.java @@ -2,14 +2,13 @@ package net.citizensnpcs.nms.v1_18_R1.entity; import java.lang.reflect.Constructor; import java.util.Map; +import java.util.WeakHashMap; import org.bukkit.Location; import org.bukkit.block.BlockFace; import org.bukkit.craftbukkit.v1_18_R1.CraftWorld; import org.bukkit.entity.Entity; -import com.google.common.collect.Maps; - import net.citizensnpcs.api.npc.NPC; import net.citizensnpcs.nms.v1_18_R1.util.NMSImpl; import net.citizensnpcs.npc.AbstractEntityController; @@ -57,11 +56,12 @@ public abstract class MobEntityController extends AbstractEntityController { if (constructor != null) return constructor; try { - return clazz.getConstructor(EntityType.class, Level.class, NPC.class); + CONSTRUCTOR_CACHE.put(clazz, constructor = clazz.getConstructor(EntityType.class, Level.class, NPC.class)); + return constructor; } catch (Exception ex) { throw new IllegalStateException("unable to find an entity constructor"); } } - private static final Map, Constructor> CONSTRUCTOR_CACHE = Maps.newHashMap(); + private static final Map, Constructor> CONSTRUCTOR_CACHE = new WeakHashMap, Constructor>(); } \ No newline at end of file diff --git a/v1_8_R3/src/main/java/net/citizensnpcs/nms/v1_8_R3/entity/MobEntityController.java b/v1_8_R3/src/main/java/net/citizensnpcs/nms/v1_8_R3/entity/MobEntityController.java index 9ebc761ad..78ef5d4bd 100644 --- a/v1_8_R3/src/main/java/net/citizensnpcs/nms/v1_8_R3/entity/MobEntityController.java +++ b/v1_8_R3/src/main/java/net/citizensnpcs/nms/v1_8_R3/entity/MobEntityController.java @@ -3,14 +3,13 @@ package net.citizensnpcs.nms.v1_8_R3.entity; import java.lang.reflect.Constructor; import java.lang.reflect.Field; import java.util.Map; +import java.util.WeakHashMap; import org.bukkit.Location; import org.bukkit.block.BlockFace; import org.bukkit.craftbukkit.v1_8_R3.CraftWorld; import org.bukkit.entity.Entity; -import com.google.common.collect.Maps; - import net.citizensnpcs.api.npc.NPC; import net.citizensnpcs.npc.AbstractEntityController; import net.citizensnpcs.util.NMS; @@ -58,12 +57,13 @@ public abstract class MobEntityController extends AbstractEntityController { if (constructor != null) return constructor; try { - return clazz.getConstructor(World.class, NPC.class); + CONSTRUCTOR_CACHE.put(clazz, constructor = clazz.getConstructor(World.class, NPC.class)); + return constructor; } catch (Exception ex) { throw new IllegalStateException("unable to find an entity constructor"); } } - private static final Map, Constructor> CONSTRUCTOR_CACHE = Maps.newHashMap(); + private static final Map, Constructor> CONSTRUCTOR_CACHE = new WeakHashMap, Constructor>(); private static final Field UUID_FIELD = NMS.getField(net.minecraft.server.v1_8_R3.Entity.class, "uniqueID"); } \ No newline at end of file