From b2964a1ae7b8a85d9d8a2219c17892de2fe43f32 Mon Sep 17 00:00:00 2001 From: LexManos Date: Tue, 17 Jan 2017 13:07:22 -0800 Subject: [PATCH] Cache entity constructors for a small performance optimization (cherry picked from commit 50bf03b82b72866bd54e796a37707d45bcbf3d80) --- .../minecraft/entity/EntityList.java.patch | 48 +++++++++++++++---- .../world/WorldEntitySpawner.java.patch | 18 +++++++ .../minecraft/world/biome/Biome.java.patch | 37 ++++++++++++++ 3 files changed, 93 insertions(+), 10 deletions(-) diff --git a/patches/minecraft/net/minecraft/entity/EntityList.java.patch b/patches/minecraft/net/minecraft/entity/EntityList.java.patch index c306cf7b04..dea7b76d68 100644 --- a/patches/minecraft/net/minecraft/entity/EntityList.java.patch +++ b/patches/minecraft/net/minecraft/entity/EntityList.java.patch @@ -1,6 +1,10 @@ --- ../src-base/minecraft/net/minecraft/entity/EntityList.java +++ ../src-work/minecraft/net/minecraft/entity/EntityList.java -@@ -93,6 +93,7 @@ +@@ -90,9 +90,11 @@ + private static final Map < Class , Integer > field_75624_e = Maps. < Class , Integer > newHashMap(); + private static final Map field_180126_g = Maps.newHashMap(); + public static final Map field_75627_a = Maps.newLinkedHashMap(); ++ private static final Map> CACHED_CONSTRUCTORS = Maps.newHashMap(); public static void func_75618_a(Class p_75618_0_, String p_75618_1_, int p_75618_2_) { @@ -8,19 +12,43 @@ if (field_75625_b.containsKey(p_75618_1_)) { throw new IllegalArgumentException("ID is already registered: " + p_75618_1_); -@@ -152,9 +153,10 @@ - { - Entity entity = null; +@@ -132,12 +134,20 @@ -+ Class oclass = null; try { -- Class oclass = (Class)field_75625_b.get(p_75615_0_.func_74779_i("id")); -+ oclass = field_75625_b.get(p_75615_0_.func_74779_i("id")); ++ java.lang.reflect.Constructor ctr = CACHED_CONSTRUCTORS.get(p_75620_0_); ++ if (ctr == null) ++ { + Class oclass = (Class)field_75625_b.get(p_75620_0_); if (oclass != null) { -@@ -168,7 +170,17 @@ +- entity = (Entity)oclass.getConstructor(new Class[] {World.class}).newInstance(new Object[] {p_75620_1_}); ++ ctr = oclass.getConstructor(World.class); ++ CACHED_CONSTRUCTORS.put(p_75620_0_, ctr); + } ++ } ++ ++ if (ctr != null) ++ entity = ctr.newInstance(p_75620_1_); + } + catch (Exception exception) + { +@@ -154,12 +164,7 @@ + + try + { +- Class oclass = (Class)field_75625_b.get(p_75615_0_.func_74779_i("id")); +- +- if (oclass != null) +- { +- entity = (Entity)oclass.getConstructor(new Class[] {World.class}).newInstance(new Object[] {p_75615_1_}); +- } ++ entity = func_75620_a(p_75615_0_.func_74779_i("id"), p_75615_1_); + } + catch (Exception exception) + { +@@ -168,7 +173,17 @@ if (entity != null) { @@ -32,13 +60,13 @@ + { + net.minecraftforge.fml.common.FMLLog.log(org.apache.logging.log4j.Level.ERROR, e, + "An Entity %s(%s) has thrown an exception during loading, its state cannot be restored. Report this to the mod author", -+ p_75615_0_.func_74779_i("id"), oclass.getName()); ++ p_75615_0_.func_74779_i("id"), field_75625_b.get(p_75615_0_.func_74779_i("id")).getName()); + entity = null; + } } else { -@@ -208,7 +220,8 @@ +@@ -208,7 +223,8 @@ @Nullable public static Entity func_188429_b(String p_188429_0_, World p_188429_1_) { diff --git a/patches/minecraft/net/minecraft/world/WorldEntitySpawner.java.patch b/patches/minecraft/net/minecraft/world/WorldEntitySpawner.java.patch index 81719f7c69..b3fabb43f3 100644 --- a/patches/minecraft/net/minecraft/world/WorldEntitySpawner.java.patch +++ b/patches/minecraft/net/minecraft/world/WorldEntitySpawner.java.patch @@ -20,6 +20,15 @@ { BlockPos blockpos = func_180621_a(p_77192_1_, chunkpos1.field_77276_a, chunkpos1.field_77275_b); int k1 = blockpos.func_177958_n(); +@@ -135,7 +137,7 @@ + + try + { +- entityliving = (EntityLiving)biome$spawnlistentry.field_76300_b.getConstructor(new Class[] {World.class}).newInstance(new Object[] {p_77192_1_}); ++ entityliving = (EntityLiving)biome$spawnlistentry.newInstance(p_77192_1_); + } + catch (Exception exception) + { @@ -145,8 +147,10 @@ entityliving.func_70012_b((double)f, (double)i3, (double)f1, p_77192_1_.field_73012_v.nextFloat() * 360.0F, 0.0F); @@ -52,3 +61,12 @@ { return false; } +@@ -257,7 +262,7 @@ + + try + { +- entityliving = (EntityLiving)biome$spawnlistentry.field_76300_b.getConstructor(new Class[] {World.class}).newInstance(new Object[] {p_77191_0_}); ++ entityliving = (EntityLiving)biome$spawnlistentry.newInstance(p_77191_0_); + } + catch (Exception exception) + { diff --git a/patches/minecraft/net/minecraft/world/biome/Biome.java.patch b/patches/minecraft/net/minecraft/world/biome/Biome.java.patch index d79e8c14a3..a8918976db 100644 --- a/patches/minecraft/net/minecraft/world/biome/Biome.java.patch +++ b/patches/minecraft/net/minecraft/world/biome/Biome.java.patch @@ -134,3 +134,40 @@ public static void func_185358_q() { func_185354_a(0, "ocean", new BiomeOcean((new Biome.BiomeProperties("Ocean")).func_185398_c(-1.0F).func_185400_d(0.1F))); +@@ -547,6 +625,7 @@ + public Class field_76300_b; + public int field_76301_c; + public int field_76299_d; ++ private final java.lang.reflect.Constructor ctr; + + public SpawnListEntry(Class p_i1970_1_, int p_i1970_2_, int p_i1970_3_, int p_i1970_4_) + { +@@ -554,12 +633,28 @@ + this.field_76300_b = p_i1970_1_; + this.field_76301_c = p_i1970_3_; + this.field_76299_d = p_i1970_4_; ++ ++ java.lang.reflect.Constructor tmp = null; ++ try ++ { ++ tmp = p_i1970_1_.getConstructor(World.class); ++ } ++ catch (NoSuchMethodException e) ++ { ++ com.google.common.base.Throwables.propagate(e); ++ } ++ ctr = tmp; + } + + public String toString() + { + return this.field_76300_b.getSimpleName() + "*(" + this.field_76301_c + "-" + this.field_76299_d + "):" + this.field_76292_a; + } ++ ++ public EntityLiving newInstance(World world) throws Exception ++ { ++ return (EntityLiving)ctr.newInstance(world); ++ } + } + + public static enum TempCategory