From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001 From: Aikar Date: Sun, 26 Aug 2018 20:49:50 -0400 Subject: [PATCH] Optimize RegistryMaterials Use larger initial sizes to increase bucket capacity on the BiMap BiMap.get was seen to be using a good bit of CPU time. diff --git a/src/main/java/net/minecraft/core/RegistryMaterials.java b/src/main/java/net/minecraft/core/RegistryMaterials.java index f3f6ed83d509d228944d15fc2b2b4cb85b05e366..18f78a5cb03dd9c8349c28b99d013752e32c9167 100644 --- a/src/main/java/net/minecraft/core/RegistryMaterials.java +++ b/src/main/java/net/minecraft/core/RegistryMaterials.java @@ -30,6 +30,7 @@ import net.minecraft.SystemUtils; import net.minecraft.resources.MinecraftKey; import net.minecraft.resources.RegistryDataPackCodec; import net.minecraft.resources.ResourceKey; +import it.unimi.dsi.fastutil.objects.Reference2IntOpenHashMap; // Paper import org.apache.commons.lang3.Validate; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; @@ -38,7 +39,7 @@ public class RegistryMaterials extends IRegistryWritable { protected static final Logger LOGGER = LogManager.getLogger(); private final ObjectList bf = new ObjectArrayList(256); - private final Object2IntMap bg = new Object2IntOpenCustomHashMap(SystemUtils.k()); + private final Reference2IntOpenHashMap bg = new Reference2IntOpenHashMap(2048);// Paper - use bigger expected size to reduce collisions and direct intent for FastUtil to be identity map private final BiMap bh; private final BiMap, T> bi; private final Map bj; @@ -49,9 +50,9 @@ public class RegistryMaterials extends IRegistryWritable { public RegistryMaterials(ResourceKey> resourcekey, Lifecycle lifecycle) { super(resourcekey, lifecycle); this.bg.defaultReturnValue(-1); - this.bh = HashBiMap.create(); - this.bi = HashBiMap.create(); - this.bj = Maps.newIdentityHashMap(); + this.bh = HashBiMap.create(2048); // Paper - use bigger expected size to reduce collisions + this.bi = HashBiMap.create(2048); // Paper - use bigger expected size to reduce collisions + this.bj = new java.util.IdentityHashMap<>(2048); // Paper - use bigger expected size to reduce collisions this.bk = lifecycle; } @@ -195,7 +196,7 @@ public class RegistryMaterials extends IRegistryWritable { this.b = collection.toArray(new Object[collection.size()]); } - return SystemUtils.a(this.b, random); + return (T) SystemUtils.a(this.b, random); // Paper - Decompile fix } public static Codec> a(ResourceKey> resourcekey, Lifecycle lifecycle, Codec codec) { @@ -215,7 +216,7 @@ public class RegistryMaterials extends IRegistryWritable { Iterator iterator = registrymaterials.iterator(); while (iterator.hasNext()) { - T t0 = iterator.next(); + T t0 = (T) iterator.next(); // Paper - Decompile fix builder.add(new RegistryMaterials.a<>((ResourceKey) registrymaterials.c(t0).get(), registrymaterials.a(t0), t0)); }