From e9959f81fb34c8c73532eff594b60c915c824113 Mon Sep 17 00:00:00 2001 From: kashike Date: Fri, 10 Aug 2018 18:32:27 -0700 Subject: [PATCH] use fastutil's Object2IntOpenHashMap --- proxy/build.gradle | 43 +++++++++++++++++++ .../proxy/protocol/StateRegistry.java | 9 ++-- 2 files changed, 49 insertions(+), 3 deletions(-) diff --git a/proxy/build.gradle b/proxy/build.gradle index e977e03fc..af559e9ec 100644 --- a/proxy/build.gradle +++ b/proxy/build.gradle @@ -37,10 +37,53 @@ dependencies { runtime 'net.java.dev.jna:jna:4.5.2' // Needed for JLine runtime 'com.lmax:disruptor:3.4.2' // Async loggers + compile 'it.unimi.dsi:fastutil:8.2.1' + testCompile "org.junit.jupiter:junit-jupiter-api:${junitVersion}" testCompile "org.junit.jupiter:junit-jupiter-engine:${junitVersion}" } +shadowJar { + exclude 'it/unimi/dsi/fastutil/booleans/**' + exclude 'it/unimi/dsi/fastutil/bytes/**' + exclude 'it/unimi/dsi/fastutil/chars/**' + exclude 'it/unimi/dsi/fastutil/doubles/**' + exclude 'it/unimi/dsi/fastutil/floats/**' + exclude 'it/unimi/dsi/fastutil/ints/*Int2*' + exclude 'it/unimi/dsi/fastutil/ints/IntAVL*' + exclude 'it/unimi/dsi/fastutil/ints/IntArray*' + exclude 'it/unimi/dsi/fastutil/ints/IntBi*' + exclude 'it/unimi/dsi/fastutil/ints/IntList*' + exclude 'it/unimi/dsi/fastutil/ints/IntOpen*' + exclude 'it/unimi/dsi/fastutil/ints/IntRB*' + exclude 'it/unimi/dsi/fastutil/ints/IntSet*' + exclude 'it/unimi/dsi/fastutil/ints/IntSorted*' + exclude 'it/unimi/dsi/fastutil/io/**' + exclude 'it/unimi/dsi/fastutil/longs/**' + exclude 'it/unimi/dsi/fastutil/objects/*ObjectArray*' + exclude 'it/unimi/dsi/fastutil/objects/*ObjectAVL*' + exclude 'it/unimi/dsi/fastutil/objects/*Object*Big*' + exclude 'it/unimi/dsi/fastutil/objects/*Object2Boolean*' + exclude 'it/unimi/dsi/fastutil/objects/*Object2Byte*' + exclude 'it/unimi/dsi/fastutil/objects/*Object2Char*' + exclude 'it/unimi/dsi/fastutil/objects/*Object2Double*' + exclude 'it/unimi/dsi/fastutil/objects/*Object2Float*' + exclude 'it/unimi/dsi/fastutil/objects/*Object2IntArray*' + exclude 'it/unimi/dsi/fastutil/objects/*Object2IntAVL*' + exclude 'it/unimi/dsi/fastutil/objects/*Object2IntLinked*' + exclude 'it/unimi/dsi/fastutil/objects/*Object*OpenCustom*' + exclude 'it/unimi/dsi/fastutil/objects/*Object2IntRB*' + exclude 'it/unimi/dsi/fastutil/objects/*Object2IntSorted*' + exclude 'it/unimi/dsi/fastutil/objects/*Object2Long*' + exclude 'it/unimi/dsi/fastutil/objects/*Object2Object*' + exclude 'it/unimi/dsi/fastutil/objects/*Object2Reference*' + exclude 'it/unimi/dsi/fastutil/objects/*Object2Short*' + exclude 'it/unimi/dsi/fastutil/objects/*ObjectRB*' + exclude 'it/unimi/dsi/fastutil/objects/*ObjectSorted*' + exclude 'it/unimi/dsi/fastutil/objects/*Reference*' + exclude 'it/unimi/dsi/fastutil/shorts/**' +} + artifacts { archives shadowJar } diff --git a/proxy/src/main/java/com/velocitypowered/proxy/protocol/StateRegistry.java b/proxy/src/main/java/com/velocitypowered/proxy/protocol/StateRegistry.java index 79cfc4942..dc534f058 100644 --- a/proxy/src/main/java/com/velocitypowered/proxy/protocol/StateRegistry.java +++ b/proxy/src/main/java/com/velocitypowered/proxy/protocol/StateRegistry.java @@ -4,6 +4,8 @@ import com.google.common.primitives.ImmutableIntArray; import com.velocitypowered.proxy.protocol.packet.*; import io.netty.util.collection.IntObjectHashMap; import io.netty.util.collection.IntObjectMap; +import it.unimi.dsi.fastutil.objects.Object2IntMap; +import it.unimi.dsi.fastutil.objects.Object2IntOpenHashMap; import java.util.*; import java.util.function.Supplier; @@ -217,10 +219,11 @@ public enum StateRegistry { public class ProtocolVersion { public final int id; final IntObjectMap> packetIdToSupplier = new IntObjectHashMap<>(16, 0.5f); - final Map, Integer> packetClassToId = new HashMap<>(16, 0.5f); + final Object2IntMap> packetClassToId = new Object2IntOpenHashMap<>(16, 0.5f); ProtocolVersion(final int id) { this.id = id; + this.packetClassToId.defaultReturnValue(Integer.MIN_VALUE); } public MinecraftPacket createPacket(final int id) { @@ -232,8 +235,8 @@ public enum StateRegistry { } public int getPacketId(final MinecraftPacket packet) { - final Integer id = this.packetClassToId.get(packet.getClass()); - if (id == null) { + final int id = this.packetClassToId.getInt(packet.getClass()); + if (id == Integer.MIN_VALUE) { throw new IllegalArgumentException(String.format( "Unable to find id for packet of type %s in %s protocol %s", packet.getClass().getName(), PacketRegistry.this.direction, this.id